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