xref: /titanic_50/usr/src/lib/libproc/common/pr_getsockname.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stream.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include "libproc.h"
37 
38 static int
39 get_sock_peer_name(struct ps_prochandle *Pr,
40 	int syscall, int sock, struct sockaddr *name, socklen_t *namelen)
41 {
42 	sysret_t rval;		/* return value from get{sock|peer}name() */
43 	argdes_t argd[4];	/* arg descriptors for get{sock|peer}name() */
44 	argdes_t *adp;
45 	int error;
46 
47 	adp = &argd[0];		/* sock argument */
48 	adp->arg_value = sock;
49 	adp->arg_object = NULL;
50 	adp->arg_type = AT_BYVAL;
51 	adp->arg_inout = AI_INPUT;
52 	adp->arg_size = 0;
53 
54 	adp++;			/* name argument */
55 	adp->arg_value = 0;
56 	adp->arg_object = name;
57 	adp->arg_type = AT_BYREF;
58 	adp->arg_inout = AI_OUTPUT;
59 	adp->arg_size = *namelen;
60 
61 	adp++;			/* namelen argument */
62 	adp->arg_value = 0;
63 	adp->arg_object = namelen;
64 	adp->arg_type = AT_BYREF;
65 	adp->arg_inout = AI_INOUT;
66 	adp->arg_size = sizeof (*namelen);
67 
68 	adp++;			/* version argument */
69 	adp->arg_value = SOV_DEFAULT;
70 	adp->arg_object = NULL;
71 	adp->arg_type = AT_BYVAL;
72 	adp->arg_inout = AI_INPUT;
73 	adp->arg_size = 0;
74 
75 	error = Psyscall(Pr, &rval, syscall, 4, &argd[0]);
76 
77 	if (error) {
78 		errno = (error > 0)? error : ENOSYS;
79 		return (-1);
80 	}
81 	return (0);
82 }
83 
84 /* libc system call interface */
85 extern int _so_getsockname(int, struct sockaddr *, socklen_t *, int);
86 extern int _so_getpeername(int, struct sockaddr *, socklen_t *, int);
87 extern int _so_getsockopt(int, int, int, void *, int *);
88 
89 /*
90  * getsockname() system call -- executed by subject process
91  */
92 int
93 pr_getsockname(struct ps_prochandle *Pr,
94 	int sock, struct sockaddr *name, socklen_t *namelen)
95 {
96 	if (Pr == NULL)		/* no subject process */
97 		return (_so_getsockname(sock, name, namelen, SOV_DEFAULT));
98 
99 	return (get_sock_peer_name(Pr, SYS_getsockname, sock, name, namelen));
100 }
101 
102 /*
103  * getpeername() system call -- executed by subject process
104  */
105 int
106 pr_getpeername(struct ps_prochandle *Pr,
107 	int sock, struct sockaddr *name, socklen_t *namelen)
108 {
109 	if (Pr == NULL)		/* no subject process */
110 		return (_so_getpeername(sock, name, namelen, SOV_DEFAULT));
111 
112 	return (get_sock_peer_name(Pr, SYS_getpeername, sock, name, namelen));
113 }
114 
115 int
116 pr_getsockopt(struct ps_prochandle *Pr,
117 	int sock, int level, int optname, void *optval, int *optlen)
118 {
119 	sysret_t rval;		/* return value from getsockopt() */
120 	argdes_t argd[5];	/* arg descriptors for getsockopt() */
121 	argdes_t *adp;
122 	int error;
123 
124 	if (Pr == NULL)		/* no subject process */
125 		return (_so_getsockopt(sock, level, optname, optval, optlen));
126 
127 	adp = &argd[0];		/* sock argument */
128 	adp->arg_value = sock;
129 	adp->arg_object = NULL;
130 	adp->arg_type = AT_BYVAL;
131 	adp->arg_inout = AI_INPUT;
132 	adp->arg_size = 0;
133 
134 	adp++;			/* level argument */
135 	adp->arg_value = level;
136 	adp->arg_object = NULL;
137 	adp->arg_type = AT_BYVAL;
138 	adp->arg_inout = AI_INPUT;
139 	adp->arg_size = 0;
140 
141 	adp++;			/* optname argument */
142 	adp->arg_value = optname;
143 	adp->arg_object = NULL;
144 	adp->arg_type = AT_BYVAL;
145 	adp->arg_inout = AI_INPUT;
146 	adp->arg_size = 0;
147 
148 	adp++;			/* optval argument */
149 	adp->arg_value = 0;
150 	adp->arg_object = optval;
151 	adp->arg_type = AT_BYREF;
152 	adp->arg_inout = AI_OUTPUT;
153 	adp->arg_size = optlen == NULL ? 0 : *optlen;
154 
155 	adp++;			/* optlen argument */
156 	adp->arg_value = 0;
157 	adp->arg_object = optlen;
158 	adp->arg_type = AT_BYREF;
159 	adp->arg_inout = AI_INOUT;
160 	adp->arg_size = sizeof (*optlen);
161 
162 	error = Psyscall(Pr, &rval, SYS_getsockopt, 5, &argd[0]);
163 
164 	if (error) {
165 		errno = (error > 0)? error : ENOSYS;
166 		return (-1);
167 	}
168 	return (0);
169 }
170