grpkill initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
grpkill
|
||||
grpkill-*
|
||||
22
Makefile
Normal file
22
Makefile
Normal file
@@ -0,0 +1,22 @@
|
||||
CC = gcc
|
||||
|
||||
# compiler flags:
|
||||
# -g adds debugging information to the executable file
|
||||
# -Wall turns on most, but not all, compiler warnings
|
||||
CFLAGS = -g -Wall
|
||||
CFLAGS_PROD = -s -Wall
|
||||
|
||||
# the build target executable:
|
||||
TARGET = grpkill
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(TARGET).c
|
||||
$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c
|
||||
|
||||
$(TARGET)-prod: $(TARGET).c
|
||||
$(CC) $(CFLAGS_PROD) -o $(TARGET)-prod $(TARGET).c
|
||||
|
||||
clean:
|
||||
$(RM) $(TARGET)
|
||||
|
||||
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