On Wed, 5 Jul 2023 17:35:14 +0800 Kaisen You <kaisenx....@intel.com> wrote:
> > +/* get cmdline form PID. Read process info form /proc/$PID. */ > +static char *get_cmdline_from_pid(pid_t pid, char *buf, int len) > +{ > + char filename[PATH_MAX]; > + char *name = NULL; > + int fd; > + int ret; > + > + if (pid < 1 || buf == NULL || len < 0) { > + printf("%s: illegal param\n", __func__); > + return NULL; > + } > + > + snprintf(filename, PATH_MAX, "/proc/%d/cmdline", pid); > + memset(buf, 0, len); > + fd = open(filename, O_RDONLY); > + if (fd < 0) { > + perror("open:"); > + return NULL; > + } > + ret = read(fd, buf, len); > + close(fd); > + > + if (ret < 0) > + return NULL; > + > + if (buf[ret-1] == '\n') > + buf[--ret] = 0; > + > + name = buf; > + while (ret) { > + if (((unsigned char)*name) < ' ') > + *name = ' '; > + name++; > + ret--; > + } > + *name = 0; > + > + if (buf[0]) > + return buf; > + > + return NULL; > +} Bad idea, this is not portable, and easily broken by system changes and container environments. Please use some form of explicit communication like is done for primary / secondary process support.