grpkill initial commit
This commit is contained in:
48
grpkill.c
Normal file
48
grpkill.c
Normal file
@@ -0,0 +1,48 @@
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <grp.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
|
||||
int main(int argc, char * argv[]){
|
||||
int targetpid;
|
||||
int targetgid;
|
||||
char* procplace;
|
||||
struct stat st;
|
||||
if (argc != 2){
|
||||
fprintf(stderr, "error - require exactly 1 argument\n");
|
||||
return 1;
|
||||
}
|
||||
if (sscanf(argv[1], "%i", &targetpid) != 1) {
|
||||
fprintf(stderr, "error - argv[1] not an integer\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (group_member(getegid()) == 0) {
|
||||
fprintf(stderr, "error - User is not part of the egid group %d\n", getegid());
|
||||
return 3;
|
||||
}
|
||||
// todo check if pid exists
|
||||
|
||||
procplace = (char*) malloc(16*sizeof(char));
|
||||
|
||||
sprintf(procplace, "/proc/%d", targetpid);
|
||||
stat(procplace, &st);
|
||||
targetgid = st.st_gid;
|
||||
free(procplace);
|
||||
|
||||
printf("pid: %d gid: %d\n", targetpid, targetgid);
|
||||
procplace = NULL;
|
||||
|
||||
if (targetgid != getegid()){
|
||||
fprintf(stderr, "error - target gid is not equal to our egid. %d != %d\n", targetgid, getegid());
|
||||
return 4;
|
||||
}
|
||||
printf("Killing %d\n", targetpid);
|
||||
kill(targetpid, SIGTERM);
|
||||
}
|
||||
Reference in New Issue
Block a user