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