xref: /illumos-gate/usr/src/cmd/pgrep/psexp.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <string.h>
30 #include <stdlib.h>
31 #include <alloca.h>
32 #include <libuutil.h>
33 
34 #include "idtab.h"
35 #include "psexp.h"
36 
37 void
38 psexp_create(psexp_t *psexp)
39 {
40 	idtab_create(&psexp->ps_euids);
41 	idtab_create(&psexp->ps_ruids);
42 	idtab_create(&psexp->ps_rgids);
43 	idtab_create(&psexp->ps_ppids);
44 	idtab_create(&psexp->ps_pgids);
45 	idtab_create(&psexp->ps_sids);
46 	idtab_create(&psexp->ps_ttys);
47 	idtab_create(&psexp->ps_projids);
48 	idtab_create(&psexp->ps_taskids);
49 	idtab_create(&psexp->ps_zoneids);
50 	idtab_create(&psexp->ps_ctids);
51 
52 	psexp->ps_pat = NULL;
53 }
54 
55 void
56 psexp_destroy(psexp_t *psexp)
57 {
58 	idtab_destroy(&psexp->ps_euids);
59 	idtab_destroy(&psexp->ps_ruids);
60 	idtab_destroy(&psexp->ps_rgids);
61 	idtab_destroy(&psexp->ps_ppids);
62 	idtab_destroy(&psexp->ps_pgids);
63 	idtab_destroy(&psexp->ps_sids);
64 	idtab_destroy(&psexp->ps_ttys);
65 	idtab_destroy(&psexp->ps_projids);
66 	idtab_destroy(&psexp->ps_taskids);
67 	idtab_destroy(&psexp->ps_zoneids);
68 	idtab_destroy(&psexp->ps_ctids);
69 
70 	if (psexp->ps_pat)
71 		regfree(&psexp->ps_reg);
72 }
73 
74 int
75 psexp_compile(psexp_t *psexp)
76 {
77 	size_t nbytes;
78 	char *buf;
79 	int err;
80 
81 	idtab_sort(&psexp->ps_euids);
82 	idtab_sort(&psexp->ps_ruids);
83 	idtab_sort(&psexp->ps_rgids);
84 	idtab_sort(&psexp->ps_ppids);
85 	idtab_sort(&psexp->ps_pgids);
86 	idtab_sort(&psexp->ps_sids);
87 	idtab_sort(&psexp->ps_ttys);
88 	idtab_sort(&psexp->ps_projids);
89 	idtab_sort(&psexp->ps_taskids);
90 	idtab_sort(&psexp->ps_zoneids);
91 	idtab_sort(&psexp->ps_ctids);
92 
93 	if (psexp->ps_pat != NULL) {
94 		if ((err = regcomp(&psexp->ps_reg, psexp->ps_pat,
95 		    REG_EXTENDED)) != 0) {
96 
97 			nbytes = regerror(err, &psexp->ps_reg, NULL, 0);
98 			buf = alloca(nbytes + 1);
99 			(void) regerror(err, &psexp->ps_reg, buf, nbytes);
100 			(void) strcat(buf, "\n");
101 			uu_warn(buf);
102 			return (-1);
103 		}
104 	}
105 
106 	return (0);
107 }
108 
109 #define	NOMATCH(__f1, __f2) \
110 	psexp->__f1.id_data && !idtab_search(&psexp->__f1, psinfo->__f2)
111 
112 int
113 psexp_match(psexp_t *psexp, psinfo_t *psinfo, int flags)
114 {
115 	regmatch_t pmatch;
116 	const char *s;
117 
118 	if (NOMATCH(ps_euids, pr_euid))
119 		return (0);
120 	if (NOMATCH(ps_ruids, pr_uid))
121 		return (0);
122 	if (NOMATCH(ps_rgids, pr_gid))
123 		return (0);
124 	if (NOMATCH(ps_ppids, pr_ppid))
125 		return (0);
126 	if (NOMATCH(ps_pgids, pr_pgid))
127 		return (0);
128 	if (NOMATCH(ps_sids, pr_sid))
129 		return (0);
130 	if (NOMATCH(ps_ttys, pr_ttydev))
131 		return (0);
132 	if (NOMATCH(ps_projids, pr_projid))
133 		return (0);
134 	if (NOMATCH(ps_taskids, pr_taskid))
135 		return (0);
136 	if (NOMATCH(ps_zoneids, pr_zoneid))
137 		return (0);
138 	if (NOMATCH(ps_ctids, pr_contract))
139 		return (0);
140 
141 	if (psexp->ps_pat != NULL) {
142 		s = (flags & PSEXP_PSARGS) ?
143 		    psinfo->pr_psargs : psinfo->pr_fname;
144 
145 		if (regexec(&psexp->ps_reg, s, 1, &pmatch, 0) != 0)
146 			return (0);
147 
148 		if ((flags & PSEXP_EXACT) &&
149 		    (pmatch.rm_so != 0 || s[pmatch.rm_eo] != '\0'))
150 			return (0);
151 	}
152 
153 	return (1);
154 }
155