Ejemplo n.º 1
0
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_proc p;
        int               i;

        kd = kvm_openfiles (NULL, NULL, 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_getenvv (kd, &p, 0);
        if (penv == NULL) {
		g_warning ("kvm_getenvv failed: %s", kvm_geterr (kd));
		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;
}
Ejemplo n.º 2
0
void
command(const struct kinfo_proc *kp, VARENT *ve)
{
	VAR *v;
	int left, wantspace = 0;
	char **argv, **p;

	/*
	 * Determine the available number of display columns.
	 * Always decrement and check after writing.
	 * No check is needed before mbswprint()
	 * and after writing the last data, though.
	 */

	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 = INT_MAX;

	if (needenv && kd != NULL) {
		argv = kvm_getenvv(kd, kp, termwidth);
		if ((p = argv) != NULL) {
			while (*p) {
				if (wantspace) {
					putchar(' ');
					left--;
				}
				left -= mbswprint(*p, left, 0);
				if (left == 0)
					return;
				p++;
				wantspace = 1;
			}
		}
	} else
		argv = NULL;

	if (needcomm) {
		if (!commandonly) {
			if (kd != NULL) {
				argv = kvm_getargv(kd, kp, termwidth);
				if ((p = argv) != NULL) {
					while (*p) {
						if (wantspace) {
							putchar(' ');
							left--;
						}
						left -= mbswprint(*p, left, 0);
						if (left == 0)
							return;
						p++;
						wantspace = 1;
					}
				}
			}
			if (argv == NULL || argv[0] == '\0' ||
			    strcmp(cmdpart(argv[0]), kp->p_comm)) {
				if (wantspace) {
					putchar(' ');
					if (--left == 0)
						return;
				}
				putchar('(');
				left--;
				left -= mbswprint(kp->p_comm, left, 0);
				if (left == 0)
					return;
				putchar(')');
				left--;
			}
		} else {
			if (wantspace) {
				putchar(' ');
				left--;
			}
			left -= mbswprint(kp->p_comm, left, 0);
		}
	}
	if (ve->next != NULL)
		while (left-- > 0)
			putchar(' ');
}
Ejemplo n.º 3
0
void
command(const struct kinfo_proc *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_getenvv(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_getargv(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, "");
	}
}