Created
January 25, 2010 21:46
-
-
Save bdrewery/286285 to your computer and use it in GitHub Desktop.
get_parent()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static char parent[500] = ""; | |
static char parent_args[500] = ""; | |
void get_parent() { | |
int i = 0; | |
int ppid; | |
parent[0] = parent_args[0] = 0; | |
ppid = getppid(); | |
for (i = 0; i < 2; ++i) { | |
char *parent_p; | |
size_t len; | |
static char args[500]; | |
int error, name[4]; | |
parent_p = i == 0 ? parent : parent_args; | |
name[0] = CTL_KERN; | |
name[1] = KERN_PROC; | |
name[2] = i == 0 ? KERN_PROC_PATHNAME : KERN_PROC_ARGS; | |
name[3] = ppid; | |
len = sizeof(args); | |
error = sysctl(name, 4, args, &len, NULL, 0); | |
if (!(error < 0 && errno != ESRCH) && !(error < 0)) { | |
if (len == 0 || strlen(args) == 0) { | |
strlcpy(args, "-", sizeof(args)); | |
len = strlen(args) + 1; | |
} | |
strlcat(parent_p, args, sizeof(parent)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment