syscall messaging 2: Change the standard return value storage for system
[dragonfly.git] / sys / dev / misc / streams / streams.c
1 /*
2  * Copyright (c) 1998 Mark Newton
3  * Copyright (c) 1994 Christos Zoulas
4  * Copyright (c) 1997 Todd Vierling
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The names of the authors may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Stolen from NetBSD /sys/compat/svr4/svr4_net.c.  Pseudo-device driver
30  * skeleton produced from /usr/share/examples/drivers/make_pseudo_driver.sh
31  * in 3.0-980524-SNAP then hacked a bit (but probably not enough :-).
32  *
33  * $FreeBSD: src/sys/dev/streams/streams.c,v 1.16.2.1 2001/02/26 04:23:07 jlemon Exp $
34  * $DragonFly: src/sys/dev/misc/streams/Attic/streams.c,v 1.6 2003/07/26 18:12:42 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>         /* SYSINIT stuff */
40 #include <sys/conf.h>           /* cdevsw stuff */
41 #include <sys/malloc.h>         /* malloc region definitions */
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/unistd.h>
45 #include <sys/fcntl.h>
46 #include <sys/socket.h>
47 #include <sys/protosw.h>
48 #include <sys/socketvar.h>
49 #include <sys/un.h>
50 #include <sys/domain.h>
51 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <sys/proc.h>
54 #include <sys/uio.h>
55
56 #include <sys/sysproto.h>
57
58 #include <svr4/svr4_types.h>
59 #include <svr4/svr4_util.h>
60 #include <svr4/svr4_signal.h>
61 #include <svr4/svr4_ioctl.h>
62 #include <svr4/svr4_stropts.h>
63 #include <svr4/svr4_socket.h>
64
65 static int svr4_soo_close __P((struct file *, struct thread *));
66 static int svr4_ptm_alloc __P((struct thread *));
67 static  d_open_t        streamsopen;
68
69 struct svr4_sockcache_entry {
70         struct thread *td;      /* Thread for the socket                */
71         void *cookie;           /* Internal cookie used for matching    */
72         struct sockaddr_un sock;/* Pathname for the socket              */
73         dev_t dev;              /* Device where the socket lives on     */
74         ino_t ino;              /* Inode where the socket lives on      */
75         TAILQ_ENTRY(svr4_sockcache_entry) entries;
76 };
77
78 TAILQ_HEAD(svr4_sockcache_head, svr4_sockcache_entry) svr4_head;
79
80 /* Initialization flag (set/queried by svr4_mod LKM) */
81 int svr4_str_initialized = 0;
82
83 /*
84  * Device minor numbers
85  */
86 enum {
87         dev_ptm                 = 10,
88         dev_arp                 = 26,
89         dev_icmp                = 27,
90         dev_ip                  = 28,
91         dev_tcp                 = 35,
92         dev_udp                 = 36,
93         dev_rawip               = 37,
94         dev_unix_dgram          = 38,
95         dev_unix_stream         = 39,
96         dev_unix_ord_stream     = 40
97 };
98
99 dev_t dt_ptm, dt_arp, dt_icmp, dt_ip, dt_tcp, dt_udp, dt_rawip,
100         dt_unix_dgram, dt_unix_stream, dt_unix_ord_stream;
101
102 static struct fileops svr4_netops = {
103         soo_read, soo_write, soo_ioctl, soo_poll, sokqfilter,
104         soo_stat, svr4_soo_close
105 };
106  
107 #define CDEV_MAJOR 103
108 static struct cdevsw streams_cdevsw = {
109         /* name */      "streams",
110         /* maj */       CDEV_MAJOR,
111         /* flags */     0,
112         /* port */      NULL,
113         /* autoq */     0,
114
115         /* open */      streamsopen,
116         /* close */     noclose,
117         /* read */      noread,
118         /* write */     nowrite,
119         /* ioctl */     noioctl,
120         /* poll */      nopoll,
121         /* mmap */      nommap,
122         /* strategy */  nostrategy,
123         /* dump */      nodump,
124         /* psize */     nopsize
125 };
126  
127 struct streams_softc {
128         struct isa_device *dev;
129 } ;
130
131 #define UNIT(dev) minor(dev)    /* assume one minor number per unit */
132
133 typedef struct streams_softc *sc_p;
134
135 static  int
136 streams_modevent(module_t mod, int type, void *unused)
137 {
138         switch (type) {
139         case MOD_LOAD:
140                 /* XXX should make sure it isn't already loaded first */
141                 dt_ptm = make_dev(&streams_cdevsw, dev_ptm, 0, 0, 0666,
142                         "ptm");
143                 dt_arp = make_dev(&streams_cdevsw, dev_arp, 0, 0, 0666,
144                         "arp");
145                 dt_icmp = make_dev(&streams_cdevsw, dev_icmp, 0, 0, 0666,
146                         "icmp");
147                 dt_ip = make_dev(&streams_cdevsw, dev_ip, 0, 0, 0666,
148                         "ip");
149                 dt_tcp = make_dev(&streams_cdevsw, dev_tcp, 0, 0, 0666,
150                         "tcp");
151                 dt_udp = make_dev(&streams_cdevsw, dev_udp, 0, 0, 0666,
152                         "udp");
153                 dt_rawip = make_dev(&streams_cdevsw, dev_rawip, 0, 0, 0666,
154                         "rawip");
155                 dt_unix_dgram = make_dev(&streams_cdevsw, dev_unix_dgram,
156                         0, 0, 0666, "ticlts");
157                 dt_unix_stream = make_dev(&streams_cdevsw, dev_unix_stream,
158                         0, 0, 0666, "ticots");
159                 dt_unix_ord_stream = make_dev(&streams_cdevsw,
160                         dev_unix_ord_stream, 0, 0, 0666, "ticotsord");
161
162                 if (! (dt_ptm && dt_arp && dt_icmp && dt_ip && dt_tcp &&
163                                 dt_udp && dt_rawip && dt_unix_dgram &&
164                                 dt_unix_stream && dt_unix_ord_stream)) {
165                         printf("WARNING: device config for STREAMS failed\n");
166                         printf("Suggest unloading streams KLD\n");
167                 }
168                 return 0;
169         case MOD_UNLOAD:
170                 /* XXX should check to see if it's busy first */
171                 destroy_dev(dt_ptm);
172                 destroy_dev(dt_arp);
173                 destroy_dev(dt_icmp);
174                 destroy_dev(dt_ip);
175                 destroy_dev(dt_tcp);
176                 destroy_dev(dt_udp);
177                 destroy_dev(dt_rawip);
178                 destroy_dev(dt_unix_dgram);
179                 destroy_dev(dt_unix_stream);
180                 destroy_dev(dt_unix_ord_stream);
181
182                 return 0;
183         default:
184                 break;
185         }
186         return 0;
187 }
188
189 static moduledata_t streams_mod = {
190         "streams",
191         streams_modevent,
192         0
193 };
194 DECLARE_MODULE(streams, streams_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
195
196 /*
197  * We only need open() and close() routines.  open() calls socreate()
198  * to allocate a "real" object behind the stream and mallocs some state
199  * info for use by the svr4 emulator;  close() deallocates the state
200  * information and passes the underlying object to the normal socket close
201  * routine.
202  */
203 static  int
204 streamsopen(dev_t dev, int oflags, int devtype, d_thread_t *td)
205 {
206         struct proc *p = td->td_proc;
207         int type, protocol;
208         int fd;
209         struct file *fp;
210         struct socket *so;
211         int error;
212         int family;
213
214         KKASSERT(p != NULL);
215         
216         if (p->p_dupfd >= 0)
217           return ENODEV;
218
219         switch (minor(dev)) {
220         case dev_udp:
221           family = AF_INET;
222           type = SOCK_DGRAM;
223           protocol = IPPROTO_UDP;
224           break;
225
226         case dev_tcp:
227           family = AF_INET;
228           type = SOCK_STREAM;
229           protocol = IPPROTO_TCP;
230           break;
231
232         case dev_ip:
233         case dev_rawip:
234           family = AF_INET;
235           type = SOCK_RAW;
236           protocol = IPPROTO_IP;
237           break;
238
239         case dev_icmp:
240           family = AF_INET;
241           type = SOCK_RAW;
242           protocol = IPPROTO_ICMP;
243           break;
244
245         case dev_unix_dgram:
246           family = AF_LOCAL;
247           type = SOCK_DGRAM;
248           protocol = 0;
249           break;
250
251         case dev_unix_stream:
252         case dev_unix_ord_stream:
253           family = AF_LOCAL;
254           type = SOCK_STREAM;
255           protocol = 0;
256           break;
257
258         case dev_ptm:
259           return svr4_ptm_alloc(td);
260
261         default:
262           return EOPNOTSUPP;
263         }
264
265         if ((error = falloc(p, &fp, &fd)) != 0)
266           return error;
267
268         if ((error = socreate(family, &so, type, protocol, td)) != 0) {
269           p->p_fd->fd_ofiles[fd] = 0;
270           ffree(fp);
271           return error;
272         }
273
274         fp->f_data = (caddr_t)so;
275         fp->f_flag = FREAD|FWRITE;
276         fp->f_ops = &svr4_netops;
277         fp->f_type = DTYPE_SOCKET;
278
279         (void)svr4_stream_get(fp);
280         p->p_dupfd = fd;
281         return ENXIO;
282 }
283
284 static int
285 svr4_ptm_alloc(struct thread *td)
286 {
287         /*
288          * XXX this is very, very ugly.  But I can't find a better
289          * way that won't duplicate a big amount of code from
290          * sys_open().  Ho hum...
291          *
292          * Fortunately for us, Solaris (at least 2.5.1) makes the
293          * /dev/ptmx open automatically just open a pty, that (after
294          * STREAMS I_PUSHes), is just a plain pty.  fstat() is used
295          * to get the minor device number to map to a tty.
296          * 
297          * Cycle through the names. If sys_open() returns ENOENT (or
298          * ENXIO), short circuit the cycle and exit.
299          */
300         static char ptyname[] = "/dev/ptyXX";
301         static char ttyletters[] = "pqrstuwxyzPQRST";
302         static char ttynumbers[] = "0123456789abcdef";
303         caddr_t sg = stackgap_init();
304         char *path = stackgap_alloc(&sg, sizeof(ptyname));
305         struct open_args oa;
306         int l = 0, n = 0;
307         register_t fd = -1;
308         int error;
309         struct proc *p = td->td_proc;
310
311         KKASSERT(p);
312
313         SCARG(&oa, path) = path;
314         SCARG(&oa, flags) = O_RDWR;
315         SCARG(&oa, mode) = 0;
316
317         while (fd == -1) {
318                 ptyname[8] = ttyletters[l];
319                 ptyname[9] = ttynumbers[n];
320
321                 if ((error = copyout(ptyname, path, sizeof(ptyname))) != 0)
322                         return error;
323
324                 switch (error = open(&oa)) {
325                 case ENOENT:
326                 case ENXIO:
327                         return error;
328                 case 0:
329                         p->p_dupfd = oa.lmsg.u.ms_result;
330                         return ENXIO;
331                 default:
332                         if (ttynumbers[++n] == '\0') {
333                                 if (ttyletters[++l] == '\0')
334                                         break;
335                                 n = 0;
336                         }
337                 }
338         }
339         return ENOENT;
340 }
341
342
343 struct svr4_strm *
344 svr4_stream_get(fp)
345         struct file *fp;
346 {
347         struct socket *so;
348         struct svr4_strm *st;
349
350         if (fp == NULL || fp->f_type != DTYPE_SOCKET)
351                 return NULL;
352
353         so = (struct socket *) fp->f_data;
354
355         if (so->so_emuldata)
356                 return so->so_emuldata;
357
358         /* Allocate a new one. */
359         st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK);
360         st->s_family = so->so_proto->pr_domain->dom_family;
361         st->s_cmd = ~0;
362         st->s_afd = -1;
363         st->s_eventmask = 0;
364         so->so_emuldata = st;
365         fp->f_ops = &svr4_netops;
366
367         return st;
368 }
369
370 void
371 svr4_delete_socket(struct thread *td, struct file *fp)
372 {
373         struct svr4_sockcache_entry *e;
374         void *cookie = ((struct socket *) fp->f_data)->so_emuldata;
375
376         if (!svr4_str_initialized) {
377                 TAILQ_INIT(&svr4_head);
378                 svr4_str_initialized = 1;
379                 return;
380         }
381
382         for (e = svr4_head.tqh_first; e != NULL; e = e->entries.tqe_next)
383                 if (e->td == td && e->cookie == cookie) {
384                         TAILQ_REMOVE(&svr4_head, e, entries);
385                         DPRINTF(("svr4_delete_socket: %s [%p,%d,%d]\n",
386                                  e->sock.sun_path, td, (int)e->dev,
387                                  (int)e->ino));
388                         free(e, M_TEMP);
389                         return;
390                 }
391 }
392
393 static int
394 svr4_soo_close(struct file *fp, struct thread *td)
395 {
396         struct socket *so = (struct socket *)fp->f_data;
397         
398         /*      CHECKUNIT_DIAG(ENXIO);*/
399
400         svr4_delete_socket(td, fp);
401         free(so->so_emuldata, M_TEMP);
402         return soo_close(fp, td);
403         return (0);
404 }