proc->thread stage 3.5: Add an IO_CORE flag so coda doesn't have to dig
[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
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/sysproto.h>
29 #include <sys/kernel.h>
30 #include <sys/sysctl.h>
31
32 #include <i386/ibcs2/ibcs2_socksys.h>
33 #include <i386/ibcs2/ibcs2_util.h>
34
35 /* Local structures */
36 struct getipdomainname_args {
37         char    *ipdomainname;
38         int     len;
39 };
40
41 struct setipdomainname_args {
42         char    *ipdomainname;
43         int     len;
44 };
45
46 /* Local prototypes */
47 static int ibcs2_getipdomainname __P((struct getipdomainname_args *));
48 static int ibcs2_setipdomainname __P((struct setipdomainname_args *));
49
50 /*
51  * iBCS2 socksys calls.
52  */
53
54 int
55 ibcs2_socksys(struct ibcs2_socksys_args *uap)
56 {
57         int error;
58         int realargs[7]; /* 1 for command, 6 for recvfrom */
59         void *passargs;
60
61         /*
62          * SOCKET should only be legal on /dev/socksys.
63          * GETIPDOMAINNAME should only be legal on /dev/socksys ?
64          * The others are (and should be) only legal on sockets.
65          */
66
67         if ((error = copyin(uap->argsp, (caddr_t)realargs, sizeof(realargs))) != 0)
68                 return error;
69         DPRINTF(("ibcs2_socksys: %08x %08x %08x %08x %08x %08x %08x\n",
70                realargs[0], realargs[1], realargs[2], realargs[3], 
71                realargs[4], realargs[5], realargs[6]));
72
73         passargs = (void *)(realargs + 1);
74         switch (realargs[0]) {
75         case SOCKSYS_ACCEPT:
76                 return accept(passargs);
77         case SOCKSYS_BIND:
78                 return bind(passargs);
79         case SOCKSYS_CONNECT:
80                 return connect(passargs);
81         case SOCKSYS_GETPEERNAME:
82                 return getpeername(passargs);
83         case SOCKSYS_GETSOCKNAME:
84                 return getsockname(passargs);
85         case SOCKSYS_GETSOCKOPT:
86                 return getsockopt(passargs);
87         case SOCKSYS_LISTEN:
88                 return listen(passargs);
89         case SOCKSYS_RECV:
90                 realargs[5] = realargs[6] = 0;
91                 /* FALLTHROUGH */
92         case SOCKSYS_RECVFROM:
93                 return recvfrom(passargs);
94         case SOCKSYS_SEND:
95                 realargs[5] = realargs[6] = 0;
96                 /* FALLTHROUGH */
97         case SOCKSYS_SENDTO:
98                 return sendto(passargs);
99         case SOCKSYS_SETSOCKOPT:
100                 return setsockopt(passargs);
101         case SOCKSYS_SHUTDOWN:
102                 return shutdown(passargs);
103         case SOCKSYS_SOCKET:
104                 return socket(passargs);
105         case SOCKSYS_SELECT:
106                 return select(passargs);
107         case SOCKSYS_GETIPDOMAIN:
108                 return ibcs2_getipdomainname(passargs);
109         case SOCKSYS_SETIPDOMAIN:
110                 return ibcs2_setipdomainname(passargs);
111         case SOCKSYS_ADJTIME:
112                 return adjtime(passargs);
113         case SOCKSYS_SETREUID:
114                 return setreuid(passargs);
115         case SOCKSYS_SETREGID:
116                 return setregid(passargs);
117         case SOCKSYS_GETTIME:
118                 return gettimeofday(passargs);
119         case SOCKSYS_SETTIME:
120                 return settimeofday(passargs);
121         case SOCKSYS_GETITIMER:
122                 return getitimer(passargs);
123         case SOCKSYS_SETITIMER:
124                 return setitimer(passargs);
125
126         default:
127                 printf("socksys unknown %08x %08x %08x %08x %08x %08x %08x\n",
128                        realargs[0], realargs[1], realargs[2], realargs[3], 
129                        realargs[4], realargs[5], realargs[6]);
130                 return EINVAL;
131         }
132         /* NOTREACHED */
133 }
134
135 /* ARGSUSED */
136 static int
137 ibcs2_getipdomainname(struct getipdomainname_args *uap)
138 {
139         char hname[MAXHOSTNAMELEN], *dptr;
140         int len;
141
142         /* Get the domain name */
143         snprintf(hname, sizeof(hname), "%s", hostname);
144         dptr = index(hname, '.');
145         if ( dptr )
146                 dptr++;
147         else
148                 /* Make it effectively an empty string */
149                 dptr = hname + strlen(hname);
150         
151         len = strlen(dptr) + 1;
152         if ((u_int)uap->len > len + 1)
153                 uap->len = len + 1;
154         return (copyout((caddr_t)dptr, (caddr_t)uap->ipdomainname, uap->len));
155 }
156
157 /* ARGSUSED */
158 static int
159 ibcs2_setipdomainname(struct setipdomainname_args *uap)
160 {
161         char hname[MAXHOSTNAMELEN], *ptr;
162         int error, sctl[2], hlen;
163
164         if ((error = suser()))
165                 return (error);
166
167         /* W/out a hostname a domain-name is nonsense */
168         if ( strlen(hostname) == 0 )
169                 return EINVAL;
170
171         /* Get the host's unqualified name (strip off the domain) */
172         snprintf(hname, sizeof(hname), "%s", hostname);
173         ptr = index(hname, '.');
174         if ( ptr != NULL ) {
175                 ptr++;
176                 *ptr = '\0';
177         } else
178                 strcat(hname, ".");
179
180         /* Set ptr to the end of the string so we can append to it */
181         hlen = strlen(hname);
182         ptr = hname + hlen;
183         if ((u_int)uap->len > (sizeof (hname) - hlen - 1))
184                 return EINVAL;
185
186         /* Append the ipdomain to the end */
187         error = copyin((caddr_t)uap->ipdomainname, ptr, uap->len);
188         if (error)
189                 return (error);
190
191         /* 'sethostname' with the new information */
192         sctl[0] = CTL_KERN;
193         sctl[1] = KERN_HOSTNAME;
194         hlen = strlen(hname) + 1;
195         return (kernel_sysctl(sctl, 2, 0, 0, hname, hlen, 0));
196 }