8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libproc / common / pr_getsockname.c
blobce86b11468aa3cc4862b10aab57b5f5b5fdf627e
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 1998-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
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"
39 static int
40 get_sock_peer_name(struct ps_prochandle *Pr,
41 int syscall, int sock, struct sockaddr *name, socklen_t *namelen)
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;
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;
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;
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);
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;
76 error = Psyscall(Pr, &rval, syscall, 4, &argd[0]);
78 if (error) {
79 errno = (error > 0)? error : ENOSYS;
80 return (-1);
82 return (0);
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 *);
91 * getsockname() system call -- executed by subject process
93 int
94 pr_getsockname(struct ps_prochandle *Pr,
95 int sock, struct sockaddr *name, socklen_t *namelen)
97 if (Pr == NULL) /* no subject process */
98 return (_so_getsockname(sock, name, namelen, SOV_DEFAULT));
100 return (get_sock_peer_name(Pr, SYS_getsockname, sock, name, namelen));
104 * getpeername() system call -- executed by subject process
107 pr_getpeername(struct ps_prochandle *Pr,
108 int sock, struct sockaddr *name, socklen_t *namelen)
110 if (Pr == NULL) /* no subject process */
111 return (_so_getpeername(sock, name, namelen, SOV_DEFAULT));
113 return (get_sock_peer_name(Pr, SYS_getpeername, sock, name, namelen));
117 pr_getsockopt(struct ps_prochandle *Pr,
118 int sock, int level, int optname, void *optval, int *optlen)
120 sysret_t rval; /* return value from getsockopt() */
121 argdes_t argd[5]; /* arg descriptors for getsockopt() */
122 argdes_t *adp;
123 int error;
125 if (Pr == NULL) /* no subject process */
126 return (_so_getsockopt(sock, level, optname, optval, optlen));
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;
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;
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;
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;
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);
163 error = Psyscall(Pr, &rval, SYS_getsockopt, 5, &argd[0]);
165 if (error) {
166 errno = (error > 0)? error : ENOSYS;
167 return (-1);
169 return (0);
173 * getpeerucred() system call -- executed by subject process
176 pr_getpeerucred(struct ps_prochandle *Pr, int fd, ucred_t **ucp)
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;
184 if (Pr == NULL) /* no subject process */
185 return (getpeerucred(fd, ucp));
187 if (uc == NULL) {
188 uc = _ucred_alloc();
189 if (uc == NULL)
190 return (-1);
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;
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;
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();
214 error = Psyscall(Pr, &rval, SYS_ucredsys, 3, &argd[0]);
216 if (error) {
217 errno = (error > 0)? error : ENOSYS;
218 if (*ucp == NULL)
219 ucred_free(uc);
221 return (-1);
223 *ucp = uc;
224 return (0);