Files
grpkill/grpkill.c
2018-03-16 13:50:36 +01:00

49 lines
1.1 KiB
C

#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);
}