|
| 1 | +/*- |
| 2 | + * Copyright (c) 2024 Ricardo Branco <[email protected]> |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer |
| 10 | + * in this position and unchanged. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR |
| 16 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <sys/types.h> |
| 28 | +#include <sys/param.h> |
| 29 | +#include <sys/queue.h> |
| 30 | +#include <sys/socket.h> |
| 31 | +#include <sys/sysctl.h> |
| 32 | +#include <sys/user.h> |
| 33 | + |
| 34 | +#include <err.h> |
| 35 | +#include <errno.h> |
| 36 | +#include <getopt.h> |
| 37 | +#include <libprocstat.h> |
| 38 | +#include <limits.h> |
| 39 | +#include <stdbool.h> |
| 40 | +#include <stdio.h> |
| 41 | +#include <vis.h> |
| 42 | + |
| 43 | +#include <pkg.h> |
| 44 | + |
| 45 | +#include "pkgcli.h" |
| 46 | + |
| 47 | +static char *safe_string(char *); |
| 48 | +static void print_argv(struct procstat *, struct kinfo_proc *); |
| 49 | +static void print_proc(struct procstat *, struct kinfo_proc *, int); |
| 50 | + |
| 51 | +void |
| 52 | +usage_ps(void) |
| 53 | +{ |
| 54 | + fprintf(stderr, |
| 55 | + "Usage: pkg ps [-v]\n\n"); |
| 56 | + fprintf(stderr, "For more information see 'pkg help install'.\n"); |
| 57 | +} |
| 58 | + |
| 59 | +int |
| 60 | +exec_ps(int argc, char **argv) |
| 61 | +{ |
| 62 | + struct kinfo_proc *procs; |
| 63 | + struct procstat *ps; |
| 64 | + int ch; |
| 65 | + unsigned int count; |
| 66 | + bool verbose = false; |
| 67 | + |
| 68 | + struct option longopts[] = { |
| 69 | + { "verbose", no_argument, NULL, 'v' }, |
| 70 | + { NULL, 0, NULL, 0 }, |
| 71 | + }; |
| 72 | + |
| 73 | + while ((ch = getopt_long(argc, argv, "v", longopts, NULL)) != -1) { |
| 74 | + switch (ch) { |
| 75 | + case 'v': |
| 76 | + verbose = true; |
| 77 | + break; |
| 78 | + default: |
| 79 | + usage_install(); |
| 80 | + return (EXIT_FAILURE); |
| 81 | + } |
| 82 | + } |
| 83 | + argc -= optind; |
| 84 | + argv += optind; |
| 85 | + |
| 86 | + if (argc > 0) { |
| 87 | + usage_ps(); |
| 88 | + return (EXIT_FAILURE); |
| 89 | + } |
| 90 | + |
| 91 | + /* Will fail if security.bsd.unprivileged_proc_debug=0 */ |
| 92 | + ps = procstat_open_sysctl(); |
| 93 | + if (ps == NULL) |
| 94 | + err(1, "procstat_open_sysctl"); |
| 95 | + |
| 96 | + procs = procstat_getprocs(ps, KERN_PROC_PROC, 0, &count); |
| 97 | + if (procs == NULL) { |
| 98 | + procstat_close(ps); |
| 99 | + err(1, "procstat_getprocs"); |
| 100 | + } |
| 101 | + |
| 102 | + printf("PID\tPPID\tUID\tUser\tCommand\n"); |
| 103 | + for (unsigned int i = 0; i < count; i++) |
| 104 | + if (procs[i].ki_pid != 0) |
| 105 | + print_proc(ps, &procs[i], verbose); |
| 106 | + |
| 107 | + procstat_freeprocs(ps, procs); |
| 108 | + procstat_close(ps); |
| 109 | + return (0); |
| 110 | +} |
| 111 | + |
| 112 | +static void |
| 113 | +print_proc(struct procstat *ps, struct kinfo_proc *kp, int verbose) |
| 114 | +{ |
| 115 | + unsigned int count; |
| 116 | + |
| 117 | + struct kinfo_vmentry *vmmap = procstat_getvmmap(ps, kp, &count); |
| 118 | + if (vmmap == NULL) { |
| 119 | + if (errno != EPERM && errno != ENOENT) |
| 120 | + err(1, "procstat_getvmmap: %d", kp->ki_pid); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + for (unsigned int i = 0; i < count; i++) |
| 125 | + /* Print executable mappings with no path */ |
| 126 | + if (vmmap[i].kve_type == KVME_TYPE_VNODE && |
| 127 | + vmmap[i].kve_protection & KVME_PROT_EXEC && |
| 128 | + vmmap[i].kve_path[0] == '\0') { |
| 129 | + printf("%d\t%d\t%d\t%s\t%s\n", kp->ki_pid, kp->ki_ppid, |
| 130 | + kp->ki_ruid, kp->ki_login, kp->ki_comm); |
| 131 | + if (verbose) |
| 132 | + print_argv(ps, kp); |
| 133 | + } |
| 134 | + |
| 135 | + procstat_freevmmap(ps, vmmap); |
| 136 | +} |
| 137 | + |
| 138 | +static void |
| 139 | +print_argv(struct procstat *ps, struct kinfo_proc *kp) |
| 140 | +{ |
| 141 | + char **argv = procstat_getargv(ps, kp, 0); |
| 142 | + if (argv == NULL) { |
| 143 | + warn("procstat_getargv: %d", kp->ki_pid); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + printf("\t"); |
| 148 | + do { |
| 149 | + printf(" %s", safe_string(*argv)); |
| 150 | + } while (*++argv); |
| 151 | + printf("\n"); |
| 152 | + |
| 153 | + procstat_freeargv(ps); |
| 154 | +} |
| 155 | + |
| 156 | +static char * |
| 157 | +safe_string(char *arg) { |
| 158 | + static char *vis = NULL; |
| 159 | + |
| 160 | + if (vis == NULL) { |
| 161 | + vis = malloc(PATH_MAX * 4 + 1); |
| 162 | + if (vis == NULL) |
| 163 | + err(1, "malloc"); |
| 164 | + } |
| 165 | + (void)strvis(vis, arg, VIS_TAB | VIS_NL | VIS_CSTYLE); |
| 166 | + |
| 167 | + return vis; |
| 168 | +} |
0 commit comments