dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libproc / common / pr_getsockname.c
blobe7387dee71cf8d370e8bdfa6125b8833d2b1a6ae
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 error = Psyscall(Pr, &rval, syscall, 4, &argd[0]);
71 if (error) {
72 errno = (error > 0)? error : ENOSYS;
73 return (-1);
75 return (0);
78 /* libc system call interface */
79 extern int _so_getsockname(int, struct sockaddr *, socklen_t *);
80 extern int _so_getpeername(int, struct sockaddr *, socklen_t *);
81 extern int _so_getsockopt(int, int, int, void *, int *);
84 * getsockname() system call -- executed by subject process
86 int
87 pr_getsockname(struct ps_prochandle *Pr,
88 int sock, struct sockaddr *name, socklen_t *namelen)
90 if (Pr == NULL) /* no subject process */
91 return (_so_getsockname(sock, name, namelen));
93 return (get_sock_peer_name(Pr, SYS_getsockname, sock, name, namelen));
97 * getpeername() system call -- executed by subject process
99 int
100 pr_getpeername(struct ps_prochandle *Pr,
101 int sock, struct sockaddr *name, socklen_t *namelen)
103 if (Pr == NULL) /* no subject process */
104 return (_so_getpeername(sock, name, namelen));
106 return (get_sock_peer_name(Pr, SYS_getpeername, sock, name, namelen));
110 pr_getsockopt(struct ps_prochandle *Pr,
111 int sock, int level, int optname, void *optval, int *optlen)
113 sysret_t rval; /* return value from getsockopt() */
114 argdes_t argd[5]; /* arg descriptors for getsockopt() */
115 argdes_t *adp;
116 int error;
118 if (Pr == NULL) /* no subject process */
119 return (_so_getsockopt(sock, level, optname, optval, optlen));
121 adp = &argd[0]; /* sock argument */
122 adp->arg_value = sock;
123 adp->arg_object = NULL;
124 adp->arg_type = AT_BYVAL;
125 adp->arg_inout = AI_INPUT;
126 adp->arg_size = 0;
128 adp++; /* level argument */
129 adp->arg_value = level;
130 adp->arg_object = NULL;
131 adp->arg_type = AT_BYVAL;
132 adp->arg_inout = AI_INPUT;
133 adp->arg_size = 0;
135 adp++; /* optname argument */
136 adp->arg_value = optname;
137 adp->arg_object = NULL;
138 adp->arg_type = AT_BYVAL;
139 adp->arg_inout = AI_INPUT;
140 adp->arg_size = 0;
142 adp++; /* optval argument */
143 adp->arg_value = 0;
144 adp->arg_object = optval;
145 adp->arg_type = AT_BYREF;
146 adp->arg_inout = AI_OUTPUT;
147 adp->arg_size = optlen == NULL ? 0 : *optlen;
149 adp++; /* optlen argument */
150 adp->arg_value = 0;
151 adp->arg_object = optlen;
152 adp->arg_type = AT_BYREF;
153 adp->arg_inout = AI_INOUT;
154 adp->arg_size = sizeof (*optlen);
156 error = Psyscall(Pr, &rval, SYS_getsockopt, 5, &argd[0]);
158 if (error) {
159 errno = (error > 0)? error : ENOSYS;
160 return (-1);
162 return (0);
166 * getpeerucred() system call -- executed by subject process
169 pr_getpeerucred(struct ps_prochandle *Pr, int fd, ucred_t **ucp)
171 sysret_t rval; /* return value from getpeerucred() */
172 argdes_t argd[3]; /* arg descriptors for getpeerucred() */
173 argdes_t *adp;
174 int error;
175 ucred_t *uc = *ucp;
177 if (Pr == NULL) /* no subject process */
178 return (getpeerucred(fd, ucp));
180 if (uc == NULL) {
181 uc = _ucred_alloc();
182 if (uc == NULL)
183 return (-1);
186 adp = &argd[0]; /* code argument */
187 adp->arg_value = UCREDSYS_GETPEERUCRED;
188 adp->arg_object = NULL;
189 adp->arg_type = AT_BYVAL;
190 adp->arg_inout = AI_INPUT;
191 adp->arg_size = 0;
193 adp++; /* fd argument */
194 adp->arg_value = fd;
195 adp->arg_object = NULL;
196 adp->arg_type = AT_BYVAL;
197 adp->arg_inout = AI_INPUT;
198 adp->arg_size = 0;
200 adp++; /* ucred argument */
201 adp->arg_value = 0;
202 adp->arg_object = uc;
203 adp->arg_type = AT_BYREF;
204 adp->arg_inout = AI_OUTPUT;
205 adp->arg_size = ucred_size();
207 error = Psyscall(Pr, &rval, SYS_ucredsys, 3, &argd[0]);
209 if (error) {
210 errno = (error > 0)? error : ENOSYS;
211 if (*ucp == NULL)
212 ucred_free(uc);
214 return (-1);
216 *ucp = uc;
217 return (0);