summaryrefslogtreecommitdiff
path: root/usr/src/cmd/pgrep
diff options
context:
space:
mode:
Diffstat (limited to 'usr/src/cmd/pgrep')
-rw-r--r--usr/src/cmd/pgrep/Makefile9
-rw-r--r--usr/src/cmd/pgrep/idtab.c7
-rw-r--r--usr/src/cmd/pgrep/pgrep.c112
-rw-r--r--usr/src/cmd/pgrep/psexp.c14
-rw-r--r--usr/src/cmd/pgrep/psexp.h15
5 files changed, 105 insertions, 52 deletions
diff --git a/usr/src/cmd/pgrep/Makefile b/usr/src/cmd/pgrep/Makefile
index 67993f0ff2..09fb900da7 100644
--- a/usr/src/cmd/pgrep/Makefile
+++ b/usr/src/cmd/pgrep/Makefile
@@ -22,6 +22,8 @@
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
+# Copyright 2019 Joyent, Inc.
+#
PROG = pgrep
PKILLFILE = pkill
@@ -31,13 +33,14 @@ ROOTLINKS = $(ROOTBIN)/$(PKILLFILE)
OBJS = pgrep.o idtab.o psexp.o
SRCS = $(OBJS:.o=.c)
POFILES = $(OBJS:.o=.po)
-lint := LINTFLAGS = -ux
include ../Makefile.cmd
CLOBBERFILES += $(PKILLFILE)
+
CFLAGS += $(CCVERBOSE)
-CERRWARN += -_gcc=-Wno-parentheses
+CSTD = $(CSTD_GNU99)
+
LDLIBS += -luutil -lproject -lcontract
POFILE = ppgrep.po
@@ -69,6 +72,4 @@ $(ROOTLINKS): $(ROOTPROG)
clean:
$(RM) $(OBJS)
-lint: lint_SRCS
-
include ../Makefile.targ
diff --git a/usr/src/cmd/pgrep/idtab.c b/usr/src/cmd/pgrep/idtab.c
index 52d6e88cdb..56db6eab91 100644
--- a/usr/src/cmd/pgrep/idtab.c
+++ b/usr/src/cmd/pgrep/idtab.c
@@ -24,7 +24,9 @@
* Use is subject to license terms.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
+/*
+ * Copyright 2019 Joyent, Inc.
+ */
#include <libintl.h>
#include <string.h>
@@ -61,7 +63,8 @@ idtab_append(idtab_t *idt, idkey_t id)
if (idt->id_nelems >= idt->id_size) {
size = idt->id_size ? idt->id_size * IDTAB_GROW : IDTAB_DEFSIZE;
- if (data = realloc(idt->id_data, sizeof (idkey_t) * size)) {
+ if ((data = realloc(idt->id_data,
+ sizeof (idkey_t) * size)) != NULL) {
idt->id_data = data;
idt->id_size = size;
} else {
diff --git a/usr/src/cmd/pgrep/pgrep.c b/usr/src/cmd/pgrep/pgrep.c
index 4531f11267..0fda6733fb 100644
--- a/usr/src/cmd/pgrep/pgrep.c
+++ b/usr/src/cmd/pgrep/pgrep.c
@@ -24,6 +24,10 @@
*/
/* Copyright (c) 2012 by Delphix. All rights reserved */
+/*
+ * Copyright 2019 Joyent, Inc.
+ */
+
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
@@ -59,11 +63,11 @@
#define TEXT_DOMAIN "SYS_TEST"
#endif
-#define OPT_SETB 0x0001 /* Set the bits specified by o_bits */
-#define OPT_CLRB 0x0002 /* Clear the bits specified by o_bits */
-#define OPT_FUNC 0x0004 /* Call the function specified by o_func */
-#define OPT_STR 0x0008 /* Set the string specified by o_ptr */
-#define OPT_CRIT 0x0010 /* Option is part of selection criteria */
+#define OPT_SETB 0x0001 /* Set the bits specified by o_bits */
+#define OPT_CLRB 0x0002 /* Clear the bits specified by o_bits */
+#define OPT_FUNC 0x0004 /* Call the function specified by o_func */
+#define OPT_STR 0x0008 /* Set the string specified by o_ptr */
+#define OPT_CRIT 0x0010 /* Option is part of selection criteria */
#define F_LONG_FMT 0x0001 /* Match against long format cmd */
#define F_NEWEST 0x0002 /* Match only newest pid */
@@ -138,7 +142,7 @@ static optdesc_t g_optdtab[] = {
{ 0, 0, 0, 0 }, /* 'k' */
{ OPT_SETB, F_LONG_OUT, 0, &g_flags }, /* 'l' */
{ 0, 0, 0, 0 }, /* 'm' */
- { OPT_SETB, F_NEWEST, 0, &g_flags }, /* -n */
+ { OPT_SETB, F_NEWEST, 0, &g_flags }, /* -n */
{ OPT_SETB, F_OLDEST, 0, &g_flags }, /* -o */
{ 0, 0, 0, 0 }, /* 'p' */
{ 0, 0, 0, 0 }, /* 'q' */
@@ -173,7 +177,7 @@ static pid_t g_pid; /* Current pid */
static int g_signal = SIGTERM; /* Signal to send */
static void
-print_proc(psinfo_t *psinfo)
+print_proc(psinfo_t *psinfo, char *argv __unused, size_t len __unused)
{
if (g_flags & F_OUTPUT)
(void) printf("%s%d", g_delim, (int)psinfo->pr_pid);
@@ -183,7 +187,7 @@ print_proc(psinfo_t *psinfo)
}
}
-static char *
+static void
mbstrip(char *buf, size_t nbytes)
{
wchar_t wc;
@@ -191,6 +195,7 @@ mbstrip(char *buf, size_t nbytes)
int n;
buf[nbytes - 1] = '\0';
+
p = buf;
while (*p != '\0') {
@@ -212,30 +217,23 @@ mbstrip(char *buf, size_t nbytes)
p += n;
}
}
-
- return (buf);
}
static void
-print_proc_long(psinfo_t *psinfo)
+print_proc_long(psinfo_t *psinfo, char *argv, size_t len)
{
- char *name;
-
- if (g_flags & F_LONG_FMT)
- name = mbstrip(psinfo->pr_psargs, PRARGSZ);
- else
- name = psinfo->pr_fname;
+ mbstrip(argv, len);
if (g_flags & F_OUTPUT)
- (void) printf("%s%5d %s", g_delim, (int)psinfo->pr_pid, name);
+ (void) printf("%s%5d %s", g_delim, (int)psinfo->pr_pid, argv);
else {
- (void) printf("%5d %s", (int)psinfo->pr_pid, name);
+ (void) printf("%5d %s", (int)psinfo->pr_pid, argv);
g_flags |= F_OUTPUT;
}
}
static void
-kill_proc(psinfo_t *psinfo)
+kill_proc(psinfo_t *psinfo, char *argv __unused, size_t len __unused)
{
if (psinfo->pr_pid > 0 && kill(psinfo->pr_pid, g_signal) == -1)
uu_warn(gettext("Failed to signal pid %d"),
@@ -268,6 +266,46 @@ open_proc_dir(const char *dirpath)
return (dirp);
}
+static void
+get_argv(int flags, psinfo_t *ps, char *buf, size_t bufsize)
+{
+ char *path = NULL;
+ ssize_t size = 0;
+ int fd;
+
+ if (!(flags & F_LONG_FMT)) {
+ (void) strlcpy(buf, ps->pr_fname, bufsize);
+ return;
+ }
+
+ if (getenv("SHORT_PSARGS") != NULL) {
+ (void) strlcpy(buf, ps->pr_psargs, bufsize);
+ return;
+ }
+
+ if (asprintf(&path, "%s/%d/cmdline", g_procdir,
+ (int)ps->pr_pid) != -1 && (fd = open(path, O_RDONLY)) != -1) {
+ size = read(fd, buf, bufsize);
+ (void) close(fd);
+ }
+
+ free(path);
+
+ if (size <= 0) {
+ (void) strlcpy(buf, ps->pr_psargs, bufsize);
+ } else {
+ buf[bufsize - 1] = '\0';
+ for (char *cp = buf; cp - buf < size; cp++) {
+ if (*cp == '\0' && (cp - buf) + 1 < size)
+ *cp = ' ';
+ }
+ }
+
+ for (ssize_t i = strlen(buf) - 1; i >= 0 && isspace(buf[i]); i--) {
+ buf[i] = '\0';
+ }
+}
+
#define NEWER(ps1, ps2) \
((ps1.pr_start.tv_sec > ps2.pr_start.tv_sec) || \
(ps1.pr_start.tv_sec == ps2.pr_start.tv_sec && \
@@ -275,9 +313,10 @@ open_proc_dir(const char *dirpath)
static int
scan_proc_dir(const char *dirpath, DIR *dirp, psexp_t *psexp,
- void (*funcp)(psinfo_t *))
+ void (*funcp)(psinfo_t *, char *, size_t))
{
char procpath[MAXPATHLEN];
+ char argv[PRMAXARGVLEN] = "";
psinfo_t ps, ops;
dirent_t *dent;
int procfd;
@@ -285,9 +324,6 @@ scan_proc_dir(const char *dirpath, DIR *dirp, psexp_t *psexp,
int reverse = (g_flags & F_REVERSE) ? 1 : 0;
int ovalid = 0, nmatches = 0, flags = 0;
- if (g_flags & F_LONG_FMT)
- flags |= PSEXP_PSARGS;
-
if (g_flags & F_EXACT_MATCH)
flags |= PSEXP_EXACT;
@@ -302,12 +338,17 @@ scan_proc_dir(const char *dirpath, DIR *dirp, psexp_t *psexp,
if ((procfd = open(procpath, O_RDONLY)) == -1)
continue;
- if ((read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) &&
- (ps.pr_nlwp != 0) && (ps.pr_pid != g_pid) &&
- (psexp_match(psexp, &ps, flags) ^ reverse)) {
+ if (read(procfd, &ps, sizeof (ps)) != sizeof (psinfo_t)) {
+ (void) close(procfd);
+ continue;
+ }
+
+ get_argv(g_flags, &ps, argv, sizeof (argv));
+
+ if ((ps.pr_nlwp != 0) && (ps.pr_pid != g_pid) &&
+ (psexp_match(psexp, &ps, argv, flags) ^ reverse)) {
if (g_flags & F_NEWEST) {
- /* LINTED - opsinfo use ok */
if (!ovalid || NEWER(ps, ops)) {
(void) memcpy(&ops, &ps,
sizeof (psinfo_t));
@@ -320,7 +361,7 @@ scan_proc_dir(const char *dirpath, DIR *dirp, psexp_t *psexp,
ovalid = 1;
}
} else {
- (*funcp)(&ps);
+ (*funcp)(&ps, argv, sizeof (argv));
nmatches++;
}
}
@@ -329,7 +370,8 @@ scan_proc_dir(const char *dirpath, DIR *dirp, psexp_t *psexp,
}
if ((g_flags & (F_NEWEST | F_OLDEST)) && ovalid) {
- (*funcp)(&ops);
+ (*funcp)(&ops, argv, sizeof (argv));
+
nmatches++;
}
@@ -592,11 +634,13 @@ print_usage(FILE *stream)
int
main(int argc, char *argv[])
{
- void (*funcp)(psinfo_t *);
+ void (*funcp)(psinfo_t *, char *, size_t);
const char *optstr;
optdesc_t *optd;
int nmatches, c;
+ const char *zroot;
+ char buf[PATH_MAX];
DIR *dirp;
@@ -626,6 +670,12 @@ main(int argc, char *argv[])
opterr = 0;
+ zroot = zone_get_nroot();
+ if (zroot != NULL) {
+ (void) snprintf(buf, sizeof (buf), "%s/%s", zroot, g_procdir);
+ g_procdir = buf;
+ }
+
while (optind < argc) {
while ((c = getopt(argc, argv, optstr)) != (int)EOF) {
diff --git a/usr/src/cmd/pgrep/psexp.c b/usr/src/cmd/pgrep/psexp.c
index be14393bd1..abb8699523 100644
--- a/usr/src/cmd/pgrep/psexp.c
+++ b/usr/src/cmd/pgrep/psexp.c
@@ -24,7 +24,9 @@
* Use is subject to license terms.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
+/*
+ * Copyright 2019 Joyent, Inc.
+ */
#include <string.h>
#include <stdlib.h>
@@ -110,10 +112,9 @@ psexp_compile(psexp_t *psexp)
psexp->__f1.id_data && !idtab_search(&psexp->__f1, psinfo->__f2)
int
-psexp_match(psexp_t *psexp, psinfo_t *psinfo, int flags)
+psexp_match(psexp_t *psexp, psinfo_t *psinfo, const char *argv, int flags)
{
regmatch_t pmatch;
- const char *s;
if (NOMATCH(ps_euids, pr_euid))
return (0);
@@ -139,14 +140,11 @@ psexp_match(psexp_t *psexp, psinfo_t *psinfo, int flags)
return (0);
if (psexp->ps_pat != NULL) {
- s = (flags & PSEXP_PSARGS) ?
- psinfo->pr_psargs : psinfo->pr_fname;
-
- if (regexec(&psexp->ps_reg, s, 1, &pmatch, 0) != 0)
+ if (regexec(&psexp->ps_reg, argv, 1, &pmatch, 0) != 0)
return (0);
if ((flags & PSEXP_EXACT) &&
- (pmatch.rm_so != 0 || s[pmatch.rm_eo] != '\0'))
+ (pmatch.rm_so != 0 || argv[pmatch.rm_eo] != '\0'))
return (0);
}
diff --git a/usr/src/cmd/pgrep/psexp.h b/usr/src/cmd/pgrep/psexp.h
index 741050cbe4..7de6742311 100644
--- a/usr/src/cmd/pgrep/psexp.h
+++ b/usr/src/cmd/pgrep/psexp.h
@@ -24,11 +24,13 @@
* Use is subject to license terms.
*/
+/*
+ * Copyright 2019 Joyent, Inc.
+ */
+
#ifndef _PSEXP_H
#define _PSEXP_H
-#pragma ident "%Z%%M% %I% %E% SMI"
-
#include <sys/types.h>
#include <procfs.h>
#include <regex.h>
@@ -39,8 +41,7 @@ extern "C" {
#include "idtab.h"
-#define PSEXP_PSARGS 0x1 /* Match against psargs rather than fname */
-#define PSEXP_EXACT 0x2 /* Match must be exact (entire string) */
+#define PSEXP_EXACT 0x1 /* Match must be exact (entire string) */
typedef struct psexp {
idtab_t ps_euids; /* Table of effective uids to match */
@@ -54,14 +55,14 @@ typedef struct psexp {
idtab_t ps_taskids; /* Table of task ids to match */
idtab_t ps_zoneids; /* Table of zone ids to match */
idtab_t ps_ctids; /* Table of contract ids to match */
- const char *ps_pat; /* Uncompiled fname/psargs regexp pattern */
- regex_t ps_reg; /* Compiled fname/psargs regexp */
+ const char *ps_pat; /* Uncompiled fname/argv regexp pattern */
+ regex_t ps_reg; /* Compiled fname/argv regexp */
} psexp_t;
extern void psexp_create(psexp_t *);
extern void psexp_destroy(psexp_t *);
extern int psexp_compile(psexp_t *);
-extern int psexp_match(psexp_t *, psinfo_t *, int);
+extern int psexp_match(psexp_t *, psinfo_t *, const char *, int);
#ifdef __cplusplus
}