xref: /illumos-gate/usr/src/lib/libproc/common/proc_set.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 #include "libproc.h"
28 #include <alloca.h>
29 #include <string.h>
30 
31 /*
32  * Convenience wrapper to set the cred attributes of a victim process
33  * to a set of new values. Caller must supply a prochandle and a
34  * fully populated prcred structure.
35  */
36 int
37 Psetcred(struct ps_prochandle *Pr, const prcred_t *credp)
38 {
39 	int ngrp;
40 	int ctlsize;
41 	struct {
42 		long cmd;
43 		prcred_t cred;
44 	} *ctlp;
45 
46 	if (Pr == NULL || credp == NULL)
47 		return (-1);
48 
49 	ngrp = credp->pr_ngroups;
50 	ctlsize = sizeof (prcred_t) + (ngrp - 1) * sizeof (gid_t);
51 	ctlp = alloca(ctlsize + sizeof (long));
52 
53 	ctlp->cmd = PCSCREDX;
54 	(void) memcpy(&ctlp->cred, credp, ctlsize);
55 
56 	if (write(Pctlfd(Pr), ctlp, sizeof (long) + ctlsize) < 0)
57 		return (-1);
58 
59 	return (0);
60 }
61 
62 /*
63  * Convenience wrapper to set the zoneid attribute of a victim process to a new
64  * value (only to and from GLOBAL_ZONEID makes sense).  Caller must supply a
65  * prochandle and a valid zoneid.
66  */
67 int
68 Psetzoneid(struct ps_prochandle *Pr, zoneid_t zoneid)
69 {
70 	struct {
71 		long cmd;
72 		long zoneid;
73 	} ctl;
74 
75 	if (Pr == NULL)
76 		return (-1);
77 
78 	ctl.zoneid = zoneid;
79 	ctl.cmd = PCSZONE;
80 
81 	if (write(Pctlfd(Pr), &ctl, sizeof (ctl)) < 0)
82 		return (-1);
83 	return (0);
84 }
85