GHashTable * ck_unix_pid_get_env_hash (pid_t pid) { GHashTable *hash = NULL; char **penv; char errbuf[_POSIX2_LINE_MAX]; kvm_t *kd; struct kinfo_proc2 p; int i; kd = kvm_openfiles (_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, errbuf); if (kd == NULL) { g_warning ("kvm_openfiles failed: %s", errbuf); return NULL; } if (! get_kinfo_proc (pid, &p)) { g_warning ("get_kinfo_proc failed: %s", g_strerror (errno)); goto fail; } penv = kvm_getenvv2 (kd, &p, 0); if (penv == NULL) { g_warning ("kvm_getenvv2 failed: %s", g_strerror (errno)); goto fail; } hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); for (i = 0; penv[i] != NULL; i++) { char **vals; if (!penv[i][0]) continue; vals = g_strsplit (penv[i], "=", 2); if (vals != NULL) { g_hash_table_insert (hash, g_strdup (vals[0]), g_strdup (vals[1])); g_strfreev (vals); } } fail: kvm_close (kd); return hash; }
void command(const struct kinfo_proc2 *kp, VARENT *ve) { VAR *v; int left, wantspace = 0; char **argv, **p; v = ve->var; if (ve->next != NULL || termwidth != UNLIMITED) { if (ve->next == NULL) { left = termwidth - (totwidth - v->width); if (left < 1) /* already wrapped, just use std width */ left = v->width; } else left = v->width; } else left = -1; if (needenv && kd != NULL) { argv = kvm_getenvv2(kd, kp, termwidth); if ((p = argv) != NULL) { while (*p) { fmt_puts(*p, &left); p++; if (*p) fmt_putc(' ', &left); else wantspace = 1; } } } else argv = NULL; if (needcomm) { if (!commandonly) { if (kd != NULL) { argv = kvm_getargv2(kd, kp, termwidth); if ((p = argv) != NULL) { if (wantspace) { fmt_putc(' ', &left); wantspace = 0; } while (*p) { fmt_puts(*p, &left); p++; if (*p) fmt_putc(' ', &left); else wantspace = 1; } } } if (argv == NULL || argv[0] == '\0' || strcmp(cmdpart(argv[0]), kp->p_comm)) { if (wantspace) { fmt_putc(' ', &left); wantspace = 0; } fmt_putc('(', &left); fmt_puts(kp->p_comm, &left); fmt_putc(')', &left); } } else { if (wantspace) { fmt_putc(' ', &left); wantspace = 0; } fmt_puts(kp->p_comm, &left); } } if (ve->next && left > 0) { if (wantspace) { fmt_putc(' ', &left); wantspace = 0; } printf("%*s", left, ""); } }