more safety checks in noshellinject; bugfix (perl string-comparision) in notashell...
[hband-tools.git] / compiled-tools / grandparentexe.c
blob01de6125a3af17ca61e00f82e205c35c9590bb82
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <err.h>
10 #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
12 int main()
14 char *path;
15 FILE *fh;
16 char lbuf[32];
17 pid_t gppid = 0;
18 char linktarget[256];
19 size_t len;
21 if(asprintf(&path, "/proc/%d/status", getppid()) == -1) return(EXIT_FAILURE);
22 fh = fopen(path, "r");
23 if(!fh) { perror("fopen"); return(EXIT_FAILURE); }
24 while(fgets(lbuf, ARRAYSIZE(lbuf), fh) != NULL)
26 if(sscanf(lbuf, "PPid: %d", &gppid) == 1)
28 break;
31 fclose(fh);
33 if(gppid == 0) { errx(EXIT_FAILURE, "Grandparent Pid not found"); }
35 if(asprintf(&path, "/proc/%d/exe", gppid) == -1) return(EXIT_FAILURE);
36 len = readlink(path, linktarget, ARRAYSIZE(linktarget));
37 if(len == -1) { perror("readlink"); return(EXIT_FAILURE); }
38 printf("%.*s\n", len, linktarget);
40 return 0;