This source file includes following definitions.
- unix_mkname
- unix_remove_socket
- unix_insert_socket
- unix_find_socket
- unix_destroy_timer
- unix_delayed_delete
- unix_destroy_socket
- unix_fcntl
- unix_setsockopt
- unix_getsockopt
- unix_listen
- def_callback1
- def_callback2
- def_callback3
- unix_create
- unix_dup
- unix_release
- unix_find_other
- unix_bind
- unix_connect
- unix_socketpair
- unix_accept
- unix_getname
- unix_sendmsg
- unix_recvmsg
- unix_shutdown
- unix_select
- unix_ioctl
- unix_get_info
- unix_recvfrom
- unix_read
- unix_recv
- unix_sendto
- unix_write
- unix_send
- unix_proto_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include <linux/config.h>
24 #include <linux/kernel.h>
25 #include <linux/major.h>
26 #include <linux/signal.h>
27 #include <linux/sched.h>
28 #include <linux/errno.h>
29 #include <linux/string.h>
30 #include <linux/stat.h>
31 #include <linux/socket.h>
32 #include <linux/un.h>
33 #include <linux/fcntl.h>
34 #include <linux/termios.h>
35 #include <linux/socket.h>
36 #include <linux/sockios.h>
37 #include <linux/net.h>
38 #include <linux/in.h>
39 #include <linux/fs.h>
40 #include <linux/malloc.h>
41 #include <asm/segment.h>
42 #include <linux/skbuff.h>
43 #include <linux/netdevice.h>
44 #include <net/sock.h>
45 #include <net/tcp.h>
46 #include <net/af_unix.h>
47 #include <linux/proc_fs.h>
48
49 static unix_socket *volatile unix_socket_list=NULL;
50
51 #define min(a,b) (((a)<(b))?(a):(b))
52
53
54
55
56 static inline void unix_mkname(struct sockaddr_un * sun, unsigned long len)
57 {
58 if (len >= sizeof(*sun))
59 len = sizeof(*sun)-1;
60 ((char *)sun)[len]=0;
61 }
62
63
64
65
66
67
68
69 static void unix_remove_socket(unix_socket *sk)
70 {
71 unix_socket *s;
72
73 cli();
74 s=unix_socket_list;
75 if(s==sk)
76 {
77 unix_socket_list=s->next;
78 sti();
79 return;
80 }
81 while(s && s->next)
82 {
83 if(s->next==sk)
84 {
85 s->next=sk->next;
86 sti();
87 return;
88 }
89 s=s->next;
90 }
91 sti();
92 }
93
94 static void unix_insert_socket(unix_socket *sk)
95 {
96 cli();
97 sk->next=unix_socket_list;
98 unix_socket_list=sk;
99 sti();
100 }
101
102 static unix_socket *unix_find_socket(struct inode *i)
103 {
104 unix_socket *s;
105 cli();
106 s=unix_socket_list;
107 while(s)
108 {
109 if(s->protinfo.af_unix.inode==i)
110 {
111 sti();
112 return(s);
113 }
114 s=s->next;
115 }
116 sti();
117 return(NULL);
118 }
119
120
121
122
123
124 static void unix_destroy_timer(unsigned long data)
125 {
126 unix_socket *sk=(unix_socket *)data;
127 if(sk->protinfo.af_unix.locks==0 && sk->wmem_alloc==0)
128 {
129 if(sk->protinfo.af_unix.name)
130 kfree(sk->protinfo.af_unix.name);
131 kfree_s(sk,sizeof(*sk));
132 return;
133 }
134
135
136
137
138
139 init_timer(&sk->timer);
140 sk->timer.expires=jiffies+10*HZ;
141 add_timer(&sk->timer);
142 }
143
144
145 static void unix_delayed_delete(unix_socket *sk)
146 {
147 init_timer(&sk->timer);
148 sk->timer.data=(unsigned long)sk;
149 sk->timer.expires=jiffies+HZ;
150 sk->timer.function=unix_destroy_timer;
151 add_timer(&sk->timer);
152 }
153
154 static void unix_destroy_socket(unix_socket *sk)
155 {
156 struct sk_buff *skb;
157 unix_remove_socket(sk);
158
159 while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
160 {
161 if(sk->state==TCP_LISTEN)
162 {
163 unix_socket *osk=skb->sk;
164 osk->state=TCP_CLOSE;
165 kfree_skb(skb, FREE_WRITE);
166 osk->state_change(osk);
167
168 }
169 else
170 {
171
172 kfree_skb(skb,FREE_WRITE);
173 }
174 }
175
176 if(sk->protinfo.af_unix.inode!=NULL)
177 {
178 iput(sk->protinfo.af_unix.inode);
179 sk->protinfo.af_unix.inode=NULL;
180 }
181
182 if(--sk->protinfo.af_unix.locks==0 && sk->wmem_alloc==0)
183 {
184 if(sk->protinfo.af_unix.name)
185 kfree(sk->protinfo.af_unix.name);
186 kfree_s(sk,sizeof(*sk));
187 }
188 else
189 {
190 sk->dead=1;
191 unix_delayed_delete(sk);
192 }
193 }
194
195
196
197
198
199 static int unix_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
200 {
201 return -EINVAL;
202 }
203
204
205
206
207
208 static int unix_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
209 {
210 unix_socket *sk=sock->data;
211 if(level!=SOL_SOCKET)
212 return -EOPNOTSUPP;
213 return sock_setsockopt(sk,level,optname,optval,optlen);
214 }
215
216 static int unix_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
217 {
218 unix_socket *sk=sock->data;
219 if(level!=SOL_SOCKET)
220 return -EOPNOTSUPP;
221 return sock_getsockopt(sk,level,optname,optval,optlen);
222 }
223
224 static int unix_listen(struct socket *sock, int backlog)
225 {
226 unix_socket *sk=sock->data;
227 if(sk->type!=SOCK_STREAM)
228 return -EOPNOTSUPP;
229 sk->max_ack_backlog=backlog;
230 sk->state=TCP_LISTEN;
231 return 0;
232 }
233
234 static void def_callback1(struct sock *sk)
235 {
236 if(!sk->dead)
237 wake_up_interruptible(sk->sleep);
238 }
239
240 static void def_callback2(struct sock *sk, int len)
241 {
242 if(!sk->dead)
243 {
244 wake_up_interruptible(sk->sleep);
245 sock_wake_async(sk->socket, 1);
246 }
247 }
248
249 static void def_callback3(struct sock *sk)
250 {
251 if(!sk->dead)
252 {
253 wake_up_interruptible(sk->sleep);
254 sock_wake_async(sk->socket, 2);
255 }
256 }
257
258 static int unix_create(struct socket *sock, int protocol)
259 {
260 unix_socket *sk;
261
262 if(protocol && protocol != PF_UNIX)
263 return -EPROTONOSUPPORT;
264 sk=(unix_socket *)kmalloc(sizeof(*sk),GFP_KERNEL);
265 if(sk==NULL)
266 return -ENOMEM;
267 sk->type=sock->type;
268 switch(sock->type)
269 {
270 case SOCK_STREAM:
271 break;
272 case SOCK_DGRAM:
273 break;
274 default:
275 kfree_s(sk,sizeof(*sk));
276 return -ESOCKTNOSUPPORT;
277 }
278 skb_queue_head_init(&sk->write_queue);
279 skb_queue_head_init(&sk->receive_queue);
280 skb_queue_head_init(&sk->back_log);
281 sk->protinfo.af_unix.family=AF_UNIX;
282 sk->protinfo.af_unix.inode=NULL;
283 sk->protinfo.af_unix.locks=1;
284 sk->protinfo.af_unix.readsem=MUTEX;
285 sk->protinfo.af_unix.name=NULL;
286 sk->protinfo.af_unix.other=NULL;
287 sk->protocol=0;
288 sk->rmem_alloc=0;
289 sk->wmem_alloc=0;
290 sk->dead=0;
291 sk->next=NULL;
292 sk->broadcast=0;
293 sk->rcvbuf=SK_RMEM_MAX;
294 sk->sndbuf=SK_WMEM_MAX;
295 sk->inuse=0;
296 sk->debug=0;
297 sk->prot=NULL;
298 sk->err=0;
299 sk->localroute=0;
300 sk->send_head=NULL;
301 sk->state=TCP_CLOSE;
302 sk->priority=SOPRI_NORMAL;
303 sk->ack_backlog=0;
304 sk->shutdown=0;
305 sk->state_change=def_callback1;
306 sk->data_ready=def_callback2;
307 sk->write_space=def_callback3;
308 sk->error_report=def_callback1;
309 sk->mtu=4096;
310 sk->socket=sock;
311 sock->data=(void *)sk;
312 sk->sleep=sock->wait;
313 sk->zapped=0;
314 unix_insert_socket(sk);
315 return 0;
316 }
317
318 static int unix_dup(struct socket *newsock, struct socket *oldsock)
319 {
320 return unix_create(newsock,0);
321 }
322
323 static int unix_release(struct socket *sock, struct socket *peer)
324 {
325 unix_socket *sk=sock->data;
326 unix_socket *skpair;
327
328
329
330 if(sk==NULL)
331 return 0;
332
333 sk->state_change(sk);
334 sk->dead=1;
335 skpair=(unix_socket *)sk->protinfo.af_unix.other;
336 if(sk->type==SOCK_STREAM && skpair!=NULL && skpair->state!=TCP_LISTEN)
337 {
338 skpair->shutdown=SHUTDOWN_MASK;
339 skpair->state_change(skpair);
340 }
341 if(skpair!=NULL)
342 skpair->protinfo.af_unix.locks--;
343 sk->protinfo.af_unix.other=NULL;
344 unix_destroy_socket(sk);
345 return 0;
346 }
347
348
349 static unix_socket *unix_find_other(char *path, int *error)
350 {
351 int old_fs;
352 int err;
353 struct inode *inode;
354 unix_socket *u;
355
356 old_fs=get_fs();
357 set_fs(get_ds());
358 err = open_namei(path, 2, S_IFSOCK, &inode, NULL);
359 set_fs(old_fs);
360 if(err<0)
361 {
362 *error=err;
363 return NULL;
364 }
365 u=unix_find_socket(inode);
366 iput(inode);
367 if(u==NULL)
368 {
369 *error=-ECONNREFUSED;
370 return NULL;
371 }
372 return u;
373 }
374
375
376 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
377 {
378 struct sockaddr_un *sun=(struct sockaddr_un *)uaddr;
379 unix_socket *sk=sock->data;
380 int old_fs;
381 int err;
382
383 if(addr_len>sizeof(struct sockaddr_un) || addr_len<3 || sun->sun_family!=AF_UNIX)
384 return -EINVAL;
385 unix_mkname(sun, addr_len);
386
387
388
389 if(sk->protinfo.af_unix.inode!=NULL)
390 return -EINVAL;
391
392 sk->protinfo.af_unix.name=kmalloc(addr_len+1, GFP_KERNEL);
393 if(sk->protinfo.af_unix.name==NULL)
394 return -ENOMEM;
395 memcpy(sk->protinfo.af_unix.name, sun->sun_path, addr_len+1);
396
397 old_fs=get_fs();
398 set_fs(get_ds());
399
400 err=do_mknod(sk->protinfo.af_unix.name,S_IFSOCK|S_IRWXUGO,0);
401 if(err==0)
402 err=open_namei(sk->protinfo.af_unix.name, 2, S_IFSOCK, &sk->protinfo.af_unix.inode, NULL);
403
404 set_fs(old_fs);
405
406 if(err<0)
407 {
408 kfree_s(sk->protinfo.af_unix.name,addr_len+1);
409 sk->protinfo.af_unix.name=NULL;
410 if(err==-EEXIST)
411 return -EADDRINUSE;
412 else
413 return err;
414 }
415
416 return 0;
417
418 }
419
420 static int unix_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
421 {
422 unix_socket *sk=sock->data;
423 struct sockaddr_un *sun=(struct sockaddr_un *)uaddr;
424 unix_socket *other;
425 struct sk_buff *skb;
426 int err;
427
428 if(sk->type==SOCK_STREAM && sk->protinfo.af_unix.other)
429 {
430 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
431 {
432 sock->state=SS_CONNECTED;
433 return 0;
434 }
435 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
436 {
437 sock->state=SS_UNCONNECTED;
438 return -ECONNREFUSED;
439 }
440 if(sock->state==SS_CONNECTING)
441 return -EALREADY;
442 return -EISCONN;
443 }
444
445 if(addr_len < sizeof(sun->sun_family)+1 || sun->sun_family!=AF_UNIX)
446 return -EINVAL;
447
448 unix_mkname(sun, addr_len);
449
450 if(sk->type==SOCK_DGRAM && sk->protinfo.af_unix.other)
451 {
452 sk->protinfo.af_unix.other->protinfo.af_unix.locks--;
453 sk->protinfo.af_unix.other=NULL;
454 sock->state=SS_UNCONNECTED;
455 }
456
457 if(sock->type==SOCK_DGRAM)
458 {
459 other=unix_find_other(sun->sun_path, &err);
460 if(other==NULL)
461 return err;
462 other->protinfo.af_unix.locks++;
463 sk->protinfo.af_unix.other=other;
464 sock->state=SS_CONNECTED;
465 sk->state=TCP_ESTABLISHED;
466 return 0;
467 }
468
469
470 if(sock->state==SS_UNCONNECTED)
471 {
472
473
474
475
476 skb=sock_alloc_send_skb(sk, 0, 0, &err);
477 if(skb==NULL)
478 return err;
479 skb->sk=sk;
480 skb->free=1;
481 sk->state=TCP_CLOSE;
482 unix_mkname(sun, addr_len);
483 other=unix_find_other(sun->sun_path, &err);
484 if(other==NULL)
485 {
486 kfree_skb(skb, FREE_WRITE);
487 return err;
488 }
489 other->protinfo.af_unix.locks++;
490 other->ack_backlog++;
491 sk->protinfo.af_unix.other=other;
492 skb_queue_tail(&other->receive_queue,skb);
493 sk->state=TCP_SYN_SENT;
494 sock->state=SS_CONNECTING;
495 sti();
496 other->data_ready(other,0);
497 }
498
499
500
501
502 cli();
503 while(sk->state==TCP_SYN_SENT)
504 {
505 if(flags&O_NONBLOCK)
506 {
507 sti();
508 return -EINPROGRESS;
509 }
510 interruptible_sleep_on(sk->sleep);
511 if(current->signal & ~current->blocked)
512 {
513 sti();
514 return -ERESTARTSYS;
515 }
516 }
517
518
519
520
521
522 if(sk->state==TCP_CLOSE)
523 {
524 sk->protinfo.af_unix.other->protinfo.af_unix.locks--;
525 sk->protinfo.af_unix.other=NULL;
526 sock->state=SS_UNCONNECTED;
527 return -ECONNREFUSED;
528 }
529
530
531
532
533
534 sock->state=SS_CONNECTED;
535 return 0;
536
537 }
538
539 static int unix_socketpair(struct socket *a, struct socket *b)
540 {
541 int err;
542 unix_socket *ska,*skb;
543
544 err=unix_create(a, 0);
545 if(err)
546 return err;
547 err=unix_create(b, 0);
548 if(err)
549 {
550 unix_release(a, NULL);
551 a->data=NULL;
552 return err;
553 }
554
555 ska=a->data;
556 skb=b->data;
557
558
559 ska->protinfo.af_unix.locks++;
560 skb->protinfo.af_unix.locks++;
561 ska->protinfo.af_unix.other=skb;
562 skb->protinfo.af_unix.other=ska;
563 ska->state=TCP_ESTABLISHED;
564 skb->state=TCP_ESTABLISHED;
565 return 0;
566 }
567
568 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
569 {
570 unix_socket *sk=sock->data;
571 unix_socket *newsk, *tsk;
572 struct sk_buff *skb;
573
574 if(sk->type!=SOCK_STREAM)
575 {
576 return -EOPNOTSUPP;
577 }
578 if(sk->state!=TCP_LISTEN)
579 {
580 return -EINVAL;
581 }
582
583 newsk=newsock->data;
584 if(sk->protinfo.af_unix.name!=NULL)
585 {
586 newsk->protinfo.af_unix.name=kmalloc(strlen(sk->protinfo.af_unix.name)+1, GFP_KERNEL);
587 if(newsk->protinfo.af_unix.name==NULL)
588 return -ENOMEM;
589 strcpy(newsk->protinfo.af_unix.name, sk->protinfo.af_unix.name);
590 }
591
592 do
593 {
594 cli();
595 skb=skb_dequeue(&sk->receive_queue);
596 if(skb==NULL)
597 {
598 if(flags&O_NONBLOCK)
599 {
600 sti();
601 return -EAGAIN;
602 }
603 interruptible_sleep_on(sk->sleep);
604 if(current->signal & ~current->blocked)
605 {
606 sti();
607 return -ERESTARTSYS;
608 }
609 sti();
610 }
611 }
612 while(skb==NULL);
613 tsk=skb->sk;
614 kfree_skb(skb, FREE_WRITE);
615 sk->ack_backlog--;
616 newsk->protinfo.af_unix.other=tsk;
617 tsk->protinfo.af_unix.other=newsk;
618 tsk->state=TCP_ESTABLISHED;
619 newsk->state=TCP_ESTABLISHED;
620 newsk->protinfo.af_unix.locks++;
621 sk->protinfo.af_unix.locks--;
622 tsk->protinfo.af_unix.locks++;
623 sti();
624 tsk->state_change(tsk);
625 sock_wake_async(tsk->socket, 0);
626 return 0;
627 }
628
629 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
630 {
631 unix_socket *sk=sock->data;
632 struct sockaddr_un *sun=(struct sockaddr_un *)uaddr;
633
634 if(peer)
635 {
636 if(sk->protinfo.af_unix.other==NULL)
637 return -ENOTCONN;
638 sk=sk->protinfo.af_unix.other;
639 }
640 sun->sun_family=AF_UNIX;
641 if(sk->protinfo.af_unix.name==NULL)
642 {
643 *sun->sun_path=0;
644 *uaddr_len=sizeof(sun->sun_family)+1;
645 return 0;
646 }
647 *uaddr_len=sizeof(sun->sun_family)+strlen(sk->protinfo.af_unix.name)+1;
648 strcpy(sun->sun_path,sk->protinfo.af_unix.name);
649 return 0;
650 }
651
652 static int unix_sendmsg(struct socket *sock, struct msghdr *msg, int len, int nonblock, int flags)
653 {
654 unix_socket *sk=sock->data;
655 unix_socket *other;
656 struct sockaddr_un *sun=msg->msg_name;
657 int err,size;
658 struct sk_buff *skb;
659
660 if(sk->err)
661 {
662 cli();
663 err=sk->err;
664 sk->err=0;
665 sti();
666 return -err;
667 }
668
669 if(flags || msg->msg_accrights)
670 return -EINVAL;
671
672 if(sun!=NULL)
673 {
674 if(sock->type==SOCK_STREAM)
675 {
676 if(sk->state==TCP_ESTABLISHED)
677 return -EISCONN;
678 else
679 return -EOPNOTSUPP;
680 }
681 }
682 if(sun==NULL)
683 {
684 if(sk->protinfo.af_unix.other==NULL)
685 return -ENOTCONN;
686 }
687
688
689
690
691
692
693 if(len>(sk->sndbuf-sizeof(struct sk_buff))/2)
694 {
695 if(sock->type==SOCK_DGRAM)
696 return -EMSGSIZE;
697 len=(sk->sndbuf-sizeof(struct sk_buff))/2;
698
699
700
701
702
703 if(len > 4000 && sock->type!=SOCK_DGRAM)
704 len = 4000;
705 }
706
707 size=len;
708 skb=sock_alloc_send_skb(sk,size,nonblock, &err);
709 if(skb==NULL)
710 return err;
711
712 skb->sk=sk;
713 skb->free=1;
714 memcpy_fromiovec(skb_put(skb,len),msg->msg_iov, len);
715
716 cli();
717 if(sun==NULL)
718 {
719 other=sk->protinfo.af_unix.other;
720 if(sock->type==SOCK_DGRAM && other->dead)
721 {
722 other->protinfo.af_unix.locks--;
723 sk->protinfo.af_unix.other=NULL;
724 sock->state=SS_UNCONNECTED;
725 return -ECONNRESET;
726 }
727 }
728 else
729 {
730 unix_mkname(sun, msg->msg_namelen);
731 other=unix_find_other(sun->sun_path, &err);
732 if(other==NULL)
733 {
734 kfree_skb(skb, FREE_WRITE);
735 return err;
736 }
737 }
738 skb_queue_tail(&other->receive_queue, skb);
739 sti();
740 other->data_ready(other,len);
741 return len;
742 }
743
744 static int unix_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, int flags, int *addr_len)
745 {
746 unix_socket *sk=sock->data;
747 struct sockaddr_un *sun=msg->msg_name;
748 int err;
749 struct sk_buff *skb;
750 int copied=0;
751 unsigned char *sp;
752 int len;
753 int num;
754 struct iovec *iov=msg->msg_iov;
755 int ct=msg->msg_iovlen;
756
757 if(addr_len)
758 *addr_len=0;
759
760 if(sk->err)
761 {
762 cli();
763 err=sk->err;
764 sk->err=0;
765 sti();
766 return -err;
767 }
768
769
770 down(&sk->protinfo.af_unix.readsem);
771
772
773 while(ct--)
774 {
775 int done=0;
776 sp=iov->iov_base;
777 len=iov->iov_len;
778 iov++;
779
780 while(done<len)
781 {
782 if(copied & (flags&MSG_PEEK))
783 {
784 up(&sk->protinfo.af_unix.readsem);
785 return copied;
786 }
787 cli();
788 skb=skb_peek(&sk->receive_queue);
789 if(skb==NULL)
790 {
791 up(&sk->protinfo.af_unix.readsem);
792 if(sk->shutdown & RCV_SHUTDOWN)
793 return copied;
794 if(copied)
795 return copied;
796 if(noblock)
797 {
798 return -EAGAIN;
799 }
800 sk->socket->flags |= SO_WAITDATA;
801 interruptible_sleep_on(sk->sleep);
802 sk->socket->flags &= ~SO_WAITDATA;
803 if( current->signal & ~current->blocked)
804 {
805 sti();
806 if(copied)
807 return copied;
808 return -ERESTARTSYS;
809 }
810 sti();
811 down(&sk->protinfo.af_unix.readsem);
812 continue;
813 }
814 if(msg->msg_name!=NULL)
815 {
816 sun->sun_family=AF_UNIX;
817 if(skb->sk->protinfo.af_unix.name)
818 {
819 memcpy(sun->sun_path, skb->sk->protinfo.af_unix.name, 108);
820 if(addr_len)
821 *addr_len=strlen(sun->sun_path)+sizeof(short);
822 }
823 else
824 if(addr_len)
825 *addr_len=sizeof(short);
826 }
827 num=min(skb->len,size-copied);
828 copied+=num;
829 done+=num;
830 if(flags&MSG_PEEK)
831 {
832 memcpy_tofs(sp, skb->data, num);
833 break;
834 }
835 else
836 {
837 memcpy_tofs(sp, skb->data,num);
838 skb_pull(skb,num);
839 sp+=num;
840 if(skb->len==0)
841 {
842 skb_unlink(skb);
843 kfree_skb(skb, FREE_WRITE);
844 if(sock->type==SOCK_DGRAM)
845 break;
846 }
847 }
848 }
849 }
850 up(&sk->protinfo.af_unix.readsem);
851 return copied;
852 }
853
854 static int unix_shutdown(struct socket *sock, int mode)
855 {
856 unix_socket *sk=(unix_socket *)sock->data;
857 unix_socket *other=sk->protinfo.af_unix.other;
858 if(mode&SEND_SHUTDOWN)
859 {
860 sk->shutdown|=SEND_SHUTDOWN;
861 sk->state_change(sk);
862 if(other)
863 {
864 other->shutdown|=RCV_SHUTDOWN;
865 other->state_change(other);
866 }
867 }
868 other=sk->protinfo.af_unix.other;
869 if(mode&RCV_SHUTDOWN)
870 {
871 sk->shutdown|=RCV_SHUTDOWN;
872 sk->state_change(sk);
873 if(other)
874 {
875 other->shutdown|=SEND_SHUTDOWN;
876 other->state_change(other);
877 }
878 }
879 return 0;
880 }
881
882
883 static int unix_select(struct socket *sock, int sel_type, select_table *wait)
884 {
885 return datagram_select(sock->data,sel_type,wait);
886 }
887
888 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
889 {
890 unix_socket *sk=sock->data;
891 int err;
892 long amount=0;
893
894 switch(cmd)
895 {
896
897 case TIOCOUTQ:
898 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
899 if(err)
900 return err;
901 amount=sk->sndbuf-sk->wmem_alloc;
902 if(amount<0)
903 amount=0;
904 put_fs_long(amount,(unsigned long *)arg);
905 return 0;
906 case TIOCINQ:
907 {
908 struct sk_buff *skb;
909 if(sk->state==TCP_LISTEN)
910 return -EINVAL;
911
912 if((skb=skb_peek(&sk->receive_queue))!=NULL)
913 amount=skb->len;
914 err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
915 put_fs_long(amount,(unsigned long *)arg);
916 return 0;
917 }
918
919 default:
920 return -EINVAL;
921 }
922
923 return(0);
924 }
925
926 static int unix_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
927 {
928 off_t pos=0;
929 off_t begin=0;
930 int len=0;
931 unix_socket *s=unix_socket_list;
932
933 len+= sprintf(buffer,"Num RefCount Protocol Flags Type St Path\n");
934
935 while(s!=NULL)
936 {
937 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X",
938 s,
939 s->protinfo.af_unix.locks,
940 0,
941 s->socket->flags,
942 s->socket->type,
943 s->socket->state);
944 if(s->protinfo.af_unix.name!=NULL)
945 len+=sprintf(buffer+len, " %s\n", s->protinfo.af_unix.name);
946 else
947 buffer[len++]='\n';
948
949 pos=begin+len;
950 if(pos<offset)
951 {
952 len=0;
953 begin=pos;
954 }
955 if(pos>offset+length)
956 break;
957 s=s->next;
958 }
959 *start=buffer+(offset-begin);
960 len-=(offset-begin);
961 if(len>length)
962 len=length;
963 return len;
964 }
965
966
967
968
969
970
971 static int unix_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, unsigned flags,
972 struct sockaddr *sa, int *addr_len)
973 {
974 struct iovec iov;
975 struct msghdr msg;
976 iov.iov_base=ubuf;
977 iov.iov_len=size;
978 msg.msg_name=(void *)sa;
979 msg.msg_namelen=0;
980 if (addr_len)
981 msg.msg_namelen = *addr_len;
982 msg.msg_accrights=NULL;
983 msg.msg_iov=&iov;
984 msg.msg_iovlen=1;
985 return unix_recvmsg(sock,&msg,size,noblock,flags,addr_len);
986 }
987
988 static int unix_read(struct socket *sock, char *ubuf, int size, int noblock)
989 {
990 return unix_recvfrom(sock,ubuf,size,noblock,0,NULL,NULL);
991 }
992
993 static int unix_recv(struct socket *sock, void *ubuf, int size, int noblock, unsigned int flags)
994 {
995 return unix_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
996 }
997
998 static int unix_sendto(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags,
999 struct sockaddr *sa, int addr_len)
1000 {
1001 struct iovec iov;
1002 struct msghdr msg;
1003 iov.iov_base=(void *)ubuf;
1004 iov.iov_len=size;
1005 msg.msg_name=(void *)sa;
1006 msg.msg_namelen=addr_len;
1007 msg.msg_accrights=NULL;
1008 msg.msg_iov=&iov;
1009 msg.msg_iovlen=1;
1010 return unix_sendmsg(sock,&msg,size,noblock,flags);
1011 }
1012
1013 static int unix_write(struct socket *sock, const char *ubuf, int size, int noblock)
1014 {
1015 return unix_sendto(sock,ubuf,size,noblock, 0, NULL, 0);
1016 }
1017
1018 static int unix_send(struct socket *sock, const void *ubuf, int size, int noblock, unsigned int flags)
1019 {
1020 return unix_sendto(sock,ubuf,size,noblock, flags, NULL, 0);
1021 }
1022
1023
1024 static struct proto_ops unix_proto_ops = {
1025 AF_UNIX,
1026
1027 unix_create,
1028 unix_dup,
1029 unix_release,
1030 unix_bind,
1031 unix_connect,
1032 unix_socketpair,
1033 unix_accept,
1034 unix_getname,
1035 unix_read,
1036 unix_write,
1037 unix_select,
1038 unix_ioctl,
1039 unix_listen,
1040 unix_send,
1041 unix_recv,
1042 unix_sendto,
1043 unix_recvfrom,
1044 unix_shutdown,
1045 unix_setsockopt,
1046 unix_getsockopt,
1047 unix_fcntl,
1048 unix_sendmsg,
1049 unix_recvmsg
1050 };
1051
1052
1053 void unix_proto_init(struct net_proto *pro)
1054 {
1055 printk("NET3: Unix domain sockets 0.09 BETA for Linux NET3.030.\n");
1056 sock_register(unix_proto_ops.family, &unix_proto_ops);
1057 proc_net_register(&(struct proc_dir_entry) {
1058 PROC_NET_UNIX, 4, "unix",
1059 S_IFREG | S_IRUGO, 1, 0, 0,
1060 0, &proc_net_inode_operations,
1061 unix_get_info
1062 });
1063 }
1064
1065
1066
1067
1068