Merge branch 'ab/config-based-hooks-base' into seen
[git] / compat / procinfo.c
1 #include "cache.h"
2
3 #include "strbuf.h"
4 #include "strvec.h"
5 #include "trace2.h"
6
7 static void get_ancestry_names(struct strvec *names)
8 {
9 #ifdef HAVE_PROCFS_LINUX
10         /*
11          * NEEDSWORK: We could gather the entire pstree into an array to match
12          * functionality with compat/win32/trace2_win32_process_info.c.
13          * To do so, we may want to examine /proc/<pid>/stat. For now, just
14          * gather the immediate parent name which is readily accessible from
15          * /proc/$(getppid())/comm.
16          */
17         struct strbuf procfs_path = STRBUF_INIT;
18         struct strbuf name = STRBUF_INIT;
19
20         /* try to use procfs if it's present. */
21         strbuf_addf(&procfs_path, "/proc/%d/comm", getppid());
22         if (strbuf_read_file(&name, procfs_path.buf, 0)) {
23                 strbuf_release(&procfs_path);
24                 strbuf_trim_trailing_newline(&name);
25                 strvec_push(names, strbuf_detach(&name, NULL));
26         }
27
28         return;
29 #endif
30         /* NEEDSWORK: add non-procfs-linux implementations here */
31 }
32
33 void trace2_collect_process_info(enum trace2_process_info_reason reason)
34 {
35         if (!trace2_is_enabled())
36                 return;
37
38         /* someday we may want to write something extra here, but not today */
39         if (reason == TRACE2_PROCESS_INFO_EXIT)
40                 return;
41
42         if (reason == TRACE2_PROCESS_INFO_STARTUP) {
43                 /*
44                  * NEEDSWORK: we could do the entire ptree in an array instead,
45                  * see compat/win32/trace2_win32_process_info.c.
46                  */
47                 struct strvec names = STRVEC_INIT;
48
49                 get_ancestry_names(&names);
50
51                 if (names.nr == 0) {
52                         strvec_clear(&names);
53                         return;
54                 }
55
56                 trace2_cmd_ancestry(names.v);
57
58                 strvec_clear(&names);
59         }
60
61         return;
62 }