xref: /titanic_44/usr/src/lib/libproc/common/pr_getsockname.c (revision a154f012db8e83f5f1b49be92c91827d1868a12a)
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 1998-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stream.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <procfs.h>
35 #include <ucred.h>
36 #include <sys/ucred.h>
37 #include "libproc.h"
38 
39 static int
get_sock_peer_name(struct ps_prochandle * Pr,int syscall,int sock,struct sockaddr * name,socklen_t * namelen)40 get_sock_peer_name(struct ps_prochandle *Pr,
41 	int syscall, int sock, struct sockaddr *name, socklen_t *namelen)
42 {
43 	sysret_t rval;		/* return value from get{sock|peer}name() */
44 	argdes_t argd[4];	/* arg descriptors for get{sock|peer}name() */
45 	argdes_t *adp;
46 	int error;
47 
48 	adp = &argd[0];		/* sock argument */
49 	adp->arg_value = sock;
50 	adp->arg_object = NULL;
51 	adp->arg_type = AT_BYVAL;
52 	adp->arg_inout = AI_INPUT;
53 	adp->arg_size = 0;
54 
55 	adp++;			/* name argument */
56 	adp->arg_value = 0;
57 	adp->arg_object = name;
58 	adp->arg_type = AT_BYREF;
59 	adp->arg_inout = AI_OUTPUT;
60 	adp->arg_size = *namelen;
61 
62 	adp++;			/* namelen argument */
63 	adp->arg_value = 0;
64 	adp->arg_object = namelen;
65 	adp->arg_type = AT_BYREF;
66 	adp->arg_inout = AI_INOUT;
67 	adp->arg_size = sizeof (*namelen);
68 
69 	adp++;			/* version argument */
70 	adp->arg_value = SOV_DEFAULT;
71 	adp->arg_object = NULL;
72 	adp->arg_type = AT_BYVAL;
73 	adp->arg_inout = AI_INPUT;
74 	adp->arg_size = 0;
75 
76 	error = Psyscall(Pr, &rval, syscall, 4, &argd[0]);
77 
78 	if (error) {
79 		errno = (error > 0)? error : ENOSYS;
80 		return (-1);
81 	}
82 	return (0);
83 }
84 
85 /* libc system call interface */
86 extern int _so_getsockname(int, struct sockaddr *, socklen_t *, int);
87 extern int _so_getpeername(int, struct sockaddr *, socklen_t *, int);
88 extern int _so_getsockopt(int, int, int, void *, int *);
89 
90 /*
91  * getsockname() system call -- executed by subject process
92  */
93 int
pr_getsockname(struct ps_prochandle * Pr,int sock,struct sockaddr * name,socklen_t * namelen)94 pr_getsockname(struct ps_prochandle *Pr,
95 	int sock, struct sockaddr *name, socklen_t *namelen)
96 {
97 	if (Pr == NULL)		/* no subject process */
98 		return (_so_getsockname(sock, name, namelen, SOV_DEFAULT));
99 
100 	return (get_sock_peer_name(Pr, SYS_getsockname, sock, name, namelen));
101 }
102 
103 /*
104  * getpeername() system call -- executed by subject process
105  */
106 int
pr_getpeername(struct ps_prochandle * Pr,int sock,struct sockaddr * name,socklen_t * namelen)107 pr_getpeername(struct ps_prochandle *Pr,
108 	int sock, struct sockaddr *name, socklen_t *namelen)
109 {
110 	if (Pr == NULL)		/* no subject process */
111 		return (_so_getpeername(sock, name, namelen, SOV_DEFAULT));
112 
113 	return (get_sock_peer_name(Pr, SYS_getpeername, sock, name, namelen));
114 }
115 
116 int
pr_getsockopt(struct ps_prochandle * Pr,int sock,int level,int optname,void * optval,int * optlen)117 pr_getsockopt(struct ps_prochandle *Pr,
118 	int sock, int level, int optname, void *optval, int *optlen)
119 {
120 	sysret_t rval;		/* return value from getsockopt() */
121 	argdes_t argd[5];	/* arg descriptors for getsockopt() */
122 	argdes_t *adp;
123 	int error;
124 
125 	if (Pr == NULL)		/* no subject process */
126 		return (_so_getsockopt(sock, level, optname, optval, optlen));
127 
128 	adp = &argd[0];		/* sock argument */
129 	adp->arg_value = sock;
130 	adp->arg_object = NULL;
131 	adp->arg_type = AT_BYVAL;
132 	adp->arg_inout = AI_INPUT;
133 	adp->arg_size = 0;
134 
135 	adp++;			/* level argument */
136 	adp->arg_value = level;
137 	adp->arg_object = NULL;
138 	adp->arg_type = AT_BYVAL;
139 	adp->arg_inout = AI_INPUT;
140 	adp->arg_size = 0;
141 
142 	adp++;			/* optname argument */
143 	adp->arg_value = optname;
144 	adp->arg_object = NULL;
145 	adp->arg_type = AT_BYVAL;
146 	adp->arg_inout = AI_INPUT;
147 	adp->arg_size = 0;
148 
149 	adp++;			/* optval argument */
150 	adp->arg_value = 0;
151 	adp->arg_object = optval;
152 	adp->arg_type = AT_BYREF;
153 	adp->arg_inout = AI_OUTPUT;
154 	adp->arg_size = optlen == NULL ? 0 : *optlen;
155 
156 	adp++;			/* optlen argument */
157 	adp->arg_value = 0;
158 	adp->arg_object = optlen;
159 	adp->arg_type = AT_BYREF;
160 	adp->arg_inout = AI_INOUT;
161 	adp->arg_size = sizeof (*optlen);
162 
163 	error = Psyscall(Pr, &rval, SYS_getsockopt, 5, &argd[0]);
164 
165 	if (error) {
166 		errno = (error > 0)? error : ENOSYS;
167 		return (-1);
168 	}
169 	return (0);
170 }
171 
172 /*
173  * getpeerucred() system call -- executed by subject process
174  */
175 int
pr_getpeerucred(struct ps_prochandle * Pr,int fd,ucred_t ** ucp)176 pr_getpeerucred(struct ps_prochandle *Pr, int fd, ucred_t **ucp)
177 {
178 	sysret_t rval;		/* return value from getpeerucred() */
179 	argdes_t argd[3];	/* arg descriptors for getpeerucred() */
180 	argdes_t *adp;
181 	int error;
182 	ucred_t *uc = *ucp;
183 
184 	if (Pr == NULL)		/* no subject process */
185 		return (getpeerucred(fd, ucp));
186 
187 	if (uc == NULL) {
188 		uc = _ucred_alloc();
189 		if (uc == NULL)
190 			return (-1);
191 	}
192 
193 	adp = &argd[0];		/* code argument */
194 	adp->arg_value = UCREDSYS_GETPEERUCRED;
195 	adp->arg_object = NULL;
196 	adp->arg_type = AT_BYVAL;
197 	adp->arg_inout = AI_INPUT;
198 	adp->arg_size = 0;
199 
200 	adp++;			/* fd argument */
201 	adp->arg_value = fd;
202 	adp->arg_object = NULL;
203 	adp->arg_type = AT_BYVAL;
204 	adp->arg_inout = AI_INPUT;
205 	adp->arg_size = 0;
206 
207 	adp++;			/* ucred argument */
208 	adp->arg_value = 0;
209 	adp->arg_object = uc;
210 	adp->arg_type = AT_BYREF;
211 	adp->arg_inout = AI_OUTPUT;
212 	adp->arg_size = ucred_size();
213 
214 	error = Psyscall(Pr, &rval, SYS_ucredsys, 3, &argd[0]);
215 
216 	if (error) {
217 		errno = (error > 0)? error : ENOSYS;
218 		if (*ucp == NULL)
219 			ucred_free(uc);
220 
221 		return (-1);
222 	}
223 	*ucp = uc;
224 	return (0);
225 }
226