2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
12 /* Copyright 2015, Richard Lowe. */
24 #include <sys/secflags.h>
25 #include <sys/types.h>
28 #include <libzonecfg.h>
30 extern const char *__progname
;
33 print_flags(const char *set
, secflagset_t flags
)
37 secflags_to_str(flags
, buf
, sizeof (buf
));
38 (void) printf("\t%s:\t%s\n", set
, buf
);
42 * Structure defining idtypes known to the priocntl command
43 * along with the corresponding names.
44 * The idtype values themselves are defined in <sys/procset.h>.
46 static struct idtypes
{
51 { P_CTID
, "contract" },
58 { P_PROJID
, "project" },
59 { P_PROJID
, "projid" },
60 { P_SID
, "session", },
63 { P_TASKID
, "taskid" },
67 { P_ZONEID
, "zoneid" },
72 str2idtype(char *idtypnm
, idtype_t
*idtypep
)
76 for (curp
= idtypes
; curp
->name
!= NULL
; curp
++) {
77 if (strncasecmp(curp
->name
, idtypnm
,
78 strlen(curp
->name
)) == 0) {
79 *idtypep
= curp
->type
;
87 getid(idtype_t type
, char *value
)
96 if ((pwd
= getpwnam(value
)) != NULL
)
100 if ((grp
= getgrnam(value
)) != NULL
)
101 return (grp
->gr_gid
);
104 if ((ret
= getprojidbyname(value
)) != (id_t
)-1)
108 if (zone_get_id(value
, &ret
) == 0)
117 ret
= (id_t
)strtoul(value
, &endp
, 10);
119 if ((errno
!= 0) || (*endp
!= '\0'))
126 main(int argc
, char **argv
)
129 psecflagwhich_t which
= PSF_INHERIT
;
131 int pgrab_flags
= PGRAB_RDONLY
;
133 char *idtypename
= NULL
;
134 idtype_t idtype
= P_PID
;
135 boolean_t usage
= B_FALSE
;
136 boolean_t e_flag
= B_FALSE
;
137 boolean_t l_flag
= B_FALSE
;
138 boolean_t s_flag
= B_FALSE
;
141 while ((opt
= getopt(argc
, argv
, "eFi:ls:")) != -1) {
147 pgrab_flags
|= PGRAB_FORCE
;
154 if ((strlen(optarg
) >= 2) &&
155 ((optarg
[1] == '='))) {
167 errx(1, "the effective flags cannot "
168 "be changed", optarg
[0]);
170 errx(1, "unknown security flag "
171 "set: '%c'", optarg
[0]);
177 if (secflags_parse(NULL
, optarg
, &act
) == -1)
178 errx(1, "couldn't parse security flags: %s",
193 if (l_flag
&& ((idtypename
!= NULL
) || s_flag
|| (argc
!= 0)))
195 if ((idtypename
!= NULL
) && !s_flag
)
197 if (e_flag
&& !s_flag
)
199 if (!l_flag
&& argc
<= 0)
203 (void) fprintf(stderr
,
204 gettext("usage:\t%s [-F] { pid | core } ...\n"),
206 (void) fprintf(stderr
,
207 gettext("\t%s -s spec [-i idtype] id ...\n"),
209 (void) fprintf(stderr
,
210 gettext("\t%s -s spec -e command [arg]...\n"),
212 (void) fprintf(stderr
, gettext("\t%s -l\n"), __progname
);
220 for (i
= 0; (name
= secflag_to_str(i
)) != NULL
; i
++)
221 (void) printf("%s\n", name
);
223 } else if (s_flag
&& e_flag
) {
225 * Don't use the strerror() message for EPERM, "Not Owner"
226 * which is misleading.
228 errc
= psecflags(P_PID
, P_MYID
, which
, &act
);
233 errx(1, gettext("failed setting "
234 "security-flags: Permission denied"));
237 err(1, gettext("failed setting security-flags"));
240 (void) execvp(argv
[0], &argv
[0]);
241 err(1, "%s", argv
[0]);
246 if (idtypename
!= NULL
)
247 if (str2idtype(idtypename
, &idtype
) == -1)
248 errx(1, gettext("No such id type: '%s'"),
251 for (i
= 0; i
< argc
; i
++) {
252 if ((id
= getid(idtype
, argv
[i
])) == (id_t
)-1) {
253 errx(1, gettext("invalid or non-existent "
254 "identifier: '%s'"), argv
[i
]);
258 * Don't use the strerror() message for EPERM, "Not
259 * Owner" which is misleading.
261 if (psecflags(idtype
, id
, which
, &act
) != 0) {
264 errx(1, gettext("failed setting "
266 "Permission denied"));
269 err(1, gettext("failed setting "
278 /* Display the flags for the given pids */
280 struct ps_prochandle
*Pr
;
286 if ((Pr
= proc_arg_grab(arg
= *argv
++, PR_ARG_ANY
,
287 pgrab_flags
, &gcode
)) == NULL
) {
288 warnx(gettext("cannot examine %s: %s"),
289 arg
, Pgrab_error(gcode
));
294 (void) memcpy(&psinfo
, Ppsinfo(Pr
), sizeof (psinfo_t
));
295 proc_unctrl_psinfo(&psinfo
);
297 if (Pstate(Pr
) == PS_DEAD
) {
298 (void) printf(gettext("core '%s' of %d:\t%.70s\n"),
299 arg
, (int)psinfo
.pr_pid
, psinfo
.pr_psargs
);
301 (void) printf("%d:\t%.70s\n",
302 (int)psinfo
.pr_pid
, psinfo
.pr_psargs
);
305 if (Psecflags(Pr
, &psf
) != 0)
306 err(1, gettext("cannot read secflags of %s"), arg
);
308 print_flags("E", psf
->pr_effective
);
309 print_flags("I", psf
->pr_inherit
);
310 print_flags("L", psf
->pr_lower
);
311 print_flags("U", psf
->pr_upper
);