kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / emulation / ibcs2 / i386 / ibcs2_socksys.c
1 /*
2  * Copyright (c) 1994, 1995 Scott Bartram
3  * Copyright (c) 1994 Arne H Juul
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $DragonFly: src/sys/emulation/ibcs2/i386/Attic/ibcs2_socksys.c,v 1.4 2003/08/07 21:17:17 dillon Exp $
26  */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysproto.h>
31 #include <sys/kernel.h>
32 #include <sys/sysctl.h>
33
34 #include "ibcs2_socksys.h"
35 #include "ibcs2_util.h"
36
37 /* Local structures */
38 struct getipdomainname_args {
39         char    *ipdomainname;
40         int     len;
41 };
42
43 struct setipdomainname_args {
44         char    *ipdomainname;
45         int     len;
46 };
47
48 /* Local prototypes */
49 static int ibcs2_getipdomainname __P((struct getipdomainname_args *));
50 static int ibcs2_setipdomainname __P((struct setipdomainname_args *));
51
52 /*
53  * iBCS2 socksys calls.
54  */
55
56 int
57 ibcs2_socksys(struct ibcs2_socksys_args *uap)
58 {
59         int error;
60         int realargs[7]; /* 1 for command, 6 for recvfrom */
61         void *passargs;
62
63         /*
64          * SOCKET should only be legal on /dev/socksys.
65          * GETIPDOMAINNAME should only be legal on /dev/socksys ?
66          * The others are (and should be) only legal on sockets.
67          */
68
69         if ((error = copyin(uap->argsp, (caddr_t)realargs, sizeof(realargs))) != 0)
70                 return error;
71         DPRINTF(("ibcs2_socksys: %08x %08x %08x %08x %08x %08x %08x\n",
72                realargs[0], realargs[1], realargs[2], realargs[3], 
73                realargs[4], realargs[5], realargs[6]));
74
75         passargs = (void *)(realargs + 1);
76         switch (realargs[0]) {
77         case SOCKSYS_ACCEPT:
78                 return accept(passargs);
79         case SOCKSYS_BIND:
80                 return bind(passargs);
81         case SOCKSYS_CONNECT:
82                 return connect(passargs);
83         case SOCKSYS_GETPEERNAME:
84                 return getpeername(passargs);
85         case SOCKSYS_GETSOCKNAME:
86                 return getsockname(passargs);
87         case SOCKSYS_GETSOCKOPT:
88                 return getsockopt(passargs);
89         case SOCKSYS_LISTEN:
90                 return listen(passargs);
91         case SOCKSYS_RECV:
92                 realargs[5] = realargs[6] = 0;
93                 /* FALLTHROUGH */
94         case SOCKSYS_RECVFROM:
95                 return recvfrom(passargs);
96         case SOCKSYS_SEND:
97                 realargs[5] = realargs[6] = 0;
98                 /* FALLTHROUGH */
99         case SOCKSYS_SENDTO:
100                 return sendto(passargs);
101         case SOCKSYS_SETSOCKOPT:
102                 return setsockopt(passargs);
103         case SOCKSYS_SHUTDOWN:
104                 return shutdown(passargs);
105         case SOCKSYS_SOCKET:
106                 return socket(passargs);
107         case SOCKSYS_SELECT:
108                 return select(passargs);
109         case SOCKSYS_GETIPDOMAIN:
110                 return ibcs2_getipdomainname(passargs);
111         case SOCKSYS_SETIPDOMAIN:
112                 return ibcs2_setipdomainname(passargs);
113         case SOCKSYS_ADJTIME:
114                 return adjtime(passargs);
115         case SOCKSYS_SETREUID:
116                 return setreuid(passargs);
117         case SOCKSYS_SETREGID:
118                 return setregid(passargs);
119         case SOCKSYS_GETTIME:
120                 return gettimeofday(passargs);
121         case SOCKSYS_SETTIME:
122                 return settimeofday(passargs);
123         case SOCKSYS_GETITIMER:
124                 return getitimer(passargs);
125         case SOCKSYS_SETITIMER:
126                 return setitimer(passargs);
127
128         default:
129                 printf("socksys unknown %08x %08x %08x %08x %08x %08x %08x\n",
130                        realargs[0], realargs[1], realargs[2], realargs[3], 
131                        realargs[4], realargs[5], realargs[6]);
132                 return EINVAL;
133         }
134         /* NOTREACHED */
135 }
136
137 /* ARGSUSED */
138 static int
139 ibcs2_getipdomainname(struct getipdomainname_args *uap)
140 {
141         char hname[MAXHOSTNAMELEN], *dptr;
142         int len;
143
144         /* Get the domain name */
145         snprintf(hname, sizeof(hname), "%s", hostname);
146         dptr = index(hname, '.');
147         if ( dptr )
148                 dptr++;
149         else
150                 /* Make it effectively an empty string */
151                 dptr = hname + strlen(hname);
152         
153         len = strlen(dptr) + 1;
154         if ((u_int)uap->len > len + 1)
155                 uap->len = len + 1;
156         return (copyout((caddr_t)dptr, (caddr_t)uap->ipdomainname, uap->len));
157 }
158
159 /* ARGSUSED */
160 static int
161 ibcs2_setipdomainname(struct setipdomainname_args *uap)
162 {
163         char hname[MAXHOSTNAMELEN], *ptr;
164         int error, sctl[2], hlen;
165
166         if ((error = suser(curthread)))
167                 return (error);
168
169         /* W/out a hostname a domain-name is nonsense */
170         if ( strlen(hostname) == 0 )
171                 return EINVAL;
172
173         /* Get the host's unqualified name (strip off the domain) */
174         snprintf(hname, sizeof(hname), "%s", hostname);
175         ptr = index(hname, '.');
176         if ( ptr != NULL ) {
177                 ptr++;
178                 *ptr = '\0';
179         } else
180                 strcat(hname, ".");
181
182         /* Set ptr to the end of the string so we can append to it */
183         hlen = strlen(hname);
184         ptr = hname + hlen;
185         if ((u_int)uap->len > (sizeof (hname) - hlen - 1))
186                 return EINVAL;
187
188         /* Append the ipdomain to the end */
189         error = copyin((caddr_t)uap->ipdomainname, ptr, uap->len);
190         if (error)
191                 return (error);
192
193         /* 'sethostname' with the new information */
194         sctl[0] = CTL_KERN;
195         sctl[1] = KERN_HOSTNAME;
196         hlen = strlen(hname) + 1;
197         return (kernel_sysctl(sctl, 2, 0, 0, hname, hlen, 0));
198 }