Skip to content

Instantly share code, notes, and snippets.

@lubomir
Created June 26, 2011 08:53
Show Gist options
  • Save lubomir/1047416 to your computer and use it in GitHub Desktop.
Save lubomir/1047416 to your computer and use it in GitHub Desktop.
#define _POSIX_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#define CMDLINE "NEJAKY_PRIKAZ"
int main (int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s PID\n", argv[0]);
return 1;
}
char *endptr = NULL;
int pid = strtol(argv[1], &endptr, 10);
if (argv[1] == endptr) {
fprintf(stderr, "PID is not a number\n");
return 2;
}
char filename[128];
snprintf(filename, 128, "/proc/%d/cmdline", pid);
FILE *fh = fopen(filename, "r");
if (!fh) {
fprintf(stderr, "Error opening '%s': ", filename);
perror("");
return 3;
}
char buffer[128];
fgets(buffer, 128, fh);
if (strcmp(buffer, CMDLINE) != 0) {
fprintf(stderr, "You can not kill this process\n");
return 4;
}
fclose(fh);
int retval = kill(pid, SIGTERM);
if (retval != 0) {
fprintf(stderr, "Failed to kill PID %d: ", pid);
perror("");
return 5;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment