| 90 | } |
| 91 | |
| 92 | static int stat_parent_pid(pid_t pid, struct strbuf *name, int *statppid) |
| 93 | { |
| 94 | struct strbuf procfs_path = STRBUF_INIT; |
| 95 | struct strbuf sb = STRBUF_INIT; |
| 96 | FILE *fp; |
| 97 | int ret = -1; |
| 98 | |
| 99 | /* try to use procfs if it's present. */ |
| 100 | strbuf_addf(&procfs_path, "/proc/%d/stat", pid); |
| 101 | fp = fopen(procfs_path.buf, "r"); |
| 102 | if (!fp) |
| 103 | goto cleanup; |
| 104 | |
| 105 | /* |
| 106 | * We could be more strict here and assert that we read at |
| 107 | * least STAT_PARENT_PID_READ_N. My reading of procfs(5) is |
| 108 | * that on any modern kernel (at least since 2.6.0 released in |
| 109 | * 2003) even if all the mandatory numeric fields were zero'd |
| 110 | * out we'd get at least 100 bytes, but let's just check that |
| 111 | * we got anything at all and trust the parse_proc_stat() |
| 112 | * function to handle its "Bad Kernel?" error checking. |
| 113 | */ |
| 114 | if (!strbuf_fread(&sb, STAT_PARENT_PID_READ_N, fp)) |
| 115 | goto cleanup; |
| 116 | if (parse_proc_stat(&sb, name, statppid) < 0) |
| 117 | goto cleanup; |
| 118 | |
| 119 | ret = 0; |
| 120 | cleanup: |
| 121 | if (fp) |
| 122 | fclose(fp); |
| 123 | strbuf_release(&procfs_path); |
| 124 | strbuf_release(&sb); |
| 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 129 | static void push_ancestry_name(struct strvec *names, pid_t pid) |
| 130 | { |
no test coverage detected