root/net/unix/af_unix.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. unix_mkname
  2. unix_remove_socket
  3. unix_insert_socket
  4. unix_find_socket
  5. unix_destroy_timer
  6. unix_delayed_delete
  7. unix_destroy_socket
  8. unix_fcntl
  9. unix_setsockopt
  10. unix_getsockopt
  11. unix_listen
  12. def_callback1
  13. def_callback2
  14. def_callback3
  15. unix_create
  16. unix_dup
  17. unix_release
  18. unix_find_other
  19. unix_bind
  20. unix_connect
  21. unix_socketpair
  22. unix_accept
  23. unix_getname
  24. unix_sendmsg
  25. unix_recvmsg
  26. unix_shutdown
  27. unix_select
  28. unix_ioctl
  29. unix_get_info
  30. unix_recvfrom
  31. unix_read
  32. unix_recv
  33. unix_sendto
  34. unix_write
  35. unix_send
  36. unix_proto_init

   1 /*
   2  * NET3:        Implementation of BSD Unix domain sockets.
   3  *
   4  * Authors:     Alan Cox, <alan@cymru.net>
   5  *
   6  *              Currently this contains all but the file descriptor passing code.
   7  *              Before that goes in the odd bugs in the iovec handlers need 
   8  *              fixing, and this bit testing. BSD fd passing is not a trivial part
   9  *              of the exercise it turns out. Anyone like writing garbage collectors.
  10  *
  11  *              This program is free software; you can redistribute it and/or
  12  *              modify it under the terms of the GNU General Public License
  13  *              as published by the Free Software Foundation; either version
  14  *              2 of the License, or (at your option) any later version.
  15  *
  16  * Fixes:
  17  *              Linus Torvalds  :       Assorted bug cures.
  18  *              Niibe Yutaka    :       async I/O support.
  19  *              Carsten Paeth   :       PF_UNIX check, address fixes.
  20  *              Alan Cox        :       Limit size of allocated blocks.
  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  * Make sure the unix name is null-terminated.
  55  */
  56 static inline void unix_mkname(struct sockaddr_un * sun, unsigned long len)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58         if (len >= sizeof(*sun))
  59                 len = sizeof(*sun)-1;
  60         ((char *)sun)[len]=0;
  61 }
  62 
  63 /*
  64  *      Note: Sockets may not be removed _during_ an interrupt or net_bh
  65  *      handler using this technique. They can be added although we do not
  66  *      use this facility.
  67  */
  68  
  69 static void unix_remove_socket(unix_socket *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  *      Delete a unix socket. We have to allow for deferring this on a timer.
 122  */
 123 
 124 static void unix_destroy_timer(unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
 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          *      Retry;
 137          */
 138          
 139         sk->timer.expires=jiffies+10*HZ;        /* No real hurry try it every 10 seconds or so */
 140         add_timer(&sk->timer);
 141 }
 142          
 143          
 144 static void unix_delayed_delete(unix_socket *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146         sk->timer.data=(unsigned long)sk;
 147         sk->timer.expires=jiffies+HZ;           /* Normally 1 second after will clean up. After that we try every 10 */
 148         sk->timer.function=unix_destroy_timer;
 149         add_timer(&sk->timer);
 150 }
 151         
 152 static void unix_destroy_socket(unix_socket *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154         struct sk_buff *skb;
 155         unix_remove_socket(sk);
 156         
 157         while((skb=skb_dequeue(&sk->receive_queue))!=NULL)
 158         {
 159                 if(sk->state==TCP_LISTEN)
 160                 {
 161                         unix_socket *osk=skb->sk;
 162                         osk->state=TCP_CLOSE;
 163                         kfree_skb(skb, FREE_WRITE);     /* Now surplus - free the skb first before the socket */
 164                         osk->state_change(osk);         /* So the connect wakes and cleans up (if any) */
 165                         /* osk will be destroyed when it gets to close or the timer fires */                    
 166                 }
 167                 else
 168                 {
 169 /*                      unix_kill_credentials(skb);     *//* Throw out any passed fd's */
 170                         kfree_skb(skb,FREE_WRITE);
 171                 }
 172         }
 173         
 174         if(sk->protinfo.af_unix.inode!=NULL)
 175         {
 176                 iput(sk->protinfo.af_unix.inode);
 177                 sk->protinfo.af_unix.inode=NULL;
 178         }
 179         
 180         if(--sk->protinfo.af_unix.locks==0 && sk->wmem_alloc==0)
 181         {
 182                 if(sk->protinfo.af_unix.name)
 183                         kfree(sk->protinfo.af_unix.name);
 184                 kfree_s(sk,sizeof(*sk));
 185         }
 186         else
 187         {
 188                 sk->dead=1;
 189                 unix_delayed_delete(sk);        /* Try every so often until buffers are all freed */
 190         }
 191 }
 192 
 193 /*
 194  *      Fixme: We need async I/O on AF_UNIX doing next.
 195  */
 196  
 197 static int unix_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199         return -EINVAL;
 200 }
 201 
 202 /*
 203  *      Yes socket options work with the new unix domain socketry!!!!!!!
 204  */
 205  
 206 static int unix_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208         unix_socket *sk=sock->data;
 209         if(level!=SOL_SOCKET)
 210                 return -EOPNOTSUPP;
 211         return sock_setsockopt(sk,level,optname,optval,optlen); 
 212 }
 213 
 214 static int unix_getsockopt(struct socket *sock, int level, int optname, char *optval, int *optlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216         unix_socket *sk=sock->data;
 217         if(level!=SOL_SOCKET)
 218                 return -EOPNOTSUPP;
 219         return sock_getsockopt(sk,level,optname,optval,optlen);
 220 }
 221 
 222 static int unix_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 223 {
 224         unix_socket *sk=sock->data;
 225         if(sk->type!=SOCK_STREAM)
 226                 return -EOPNOTSUPP;             /* Only stream sockets accept */
 227         sk->max_ack_backlog=backlog;
 228         sk->state=TCP_LISTEN;
 229         return 0;
 230 }
 231 
 232 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234         if(!sk->dead)
 235                 wake_up_interruptible(sk->sleep);
 236 }
 237 
 238 static void def_callback2(struct sock *sk, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240         if(!sk->dead)
 241         {
 242                 wake_up_interruptible(sk->sleep);
 243                 sock_wake_async(sk->socket, 1);
 244         }
 245 }
 246 
 247 static void def_callback3(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 248 {
 249         if(!sk->dead)
 250         {
 251                 wake_up_interruptible(sk->sleep);
 252                 sock_wake_async(sk->socket, 2);
 253         }
 254 }
 255 
 256 static int unix_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258         unix_socket *sk;
 259 /*      printk("Unix create\n");*/
 260         if(protocol && protocol != PF_UNIX)
 261                 return -EPROTONOSUPPORT;
 262         sk=(unix_socket *)kmalloc(sizeof(*sk),GFP_KERNEL);
 263         if(sk==NULL)
 264                 return -ENOMEM;
 265         sk->type=sock->type;
 266         switch(sock->type)
 267         {
 268                 case SOCK_STREAM:
 269                         break;
 270                 case SOCK_DGRAM:
 271                         break;
 272                 default:
 273                         kfree_s(sk,sizeof(*sk));
 274                         return -ESOCKTNOSUPPORT;
 275         }
 276         init_timer(&sk->timer);
 277         skb_queue_head_init(&sk->write_queue);
 278         skb_queue_head_init(&sk->receive_queue);
 279         skb_queue_head_init(&sk->back_log);
 280         sk->protinfo.af_unix.family=AF_UNIX;
 281         sk->protinfo.af_unix.inode=NULL;
 282         sk->protinfo.af_unix.locks=1;   /* Us */
 283         sk->protinfo.af_unix.readsem=MUTEX;     /* single task reading lock */
 284         sk->protinfo.af_unix.name=NULL;
 285         sk->protinfo.af_unix.other=NULL;
 286         sk->protocol=0;
 287         sk->rmem_alloc=0;
 288         sk->wmem_alloc=0;
 289         sk->dead=0;
 290         sk->next=NULL;
 291         sk->broadcast=0;
 292         sk->rcvbuf=SK_RMEM_MAX;
 293         sk->sndbuf=SK_WMEM_MAX;
 294         sk->inuse=0;
 295         sk->debug=0;
 296         sk->prot=NULL;
 297         sk->err=0;
 298         sk->localroute=0;
 299         sk->send_head=NULL;
 300         sk->state=TCP_CLOSE;
 301         sk->priority=SOPRI_NORMAL;
 302         sk->ack_backlog=0;
 303         sk->shutdown=0;
 304         sk->state_change=def_callback1;
 305         sk->data_ready=def_callback2;
 306         sk->write_space=def_callback3;
 307         sk->error_report=def_callback1;
 308         sk->mtu=4096;
 309         sk->socket=sock;
 310         sock->data=(void *)sk;
 311         sk->sleep=sock->wait;
 312         sk->zapped=0;
 313         unix_insert_socket(sk);
 314         return 0;
 315 }
 316 
 317 static int unix_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 318 {
 319         return unix_create(newsock,0);
 320 }
 321 
 322 static int unix_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 323 {
 324         unix_socket *sk=sock->data;
 325         unix_socket *skpair;
 326         
 327         /* May not have data attached */
 328         
 329         if(sk==NULL)
 330                 return 0;
 331                 
 332         sk->state_change(sk);
 333         sk->dead=1;
 334         skpair=(unix_socket *)sk->protinfo.af_unix.other;       /* Person we send to (default) */
 335         if(sk->type==SOCK_STREAM && skpair!=NULL && skpair->state!=TCP_LISTEN)
 336         {
 337                 skpair->shutdown=SHUTDOWN_MASK;         /* No more writes */
 338                 skpair->state_change(skpair);           /* Wake any blocked writes */
 339         }
 340         if(skpair!=NULL)
 341                 skpair->protinfo.af_unix.locks--;               /* It may now die */
 342         sk->protinfo.af_unix.other=NULL;                        /* No pair */
 343         unix_destroy_socket(sk);                        /* Try and flush out this socket. Throw our buffers at least */
 344         return 0;
 345 }
 346 
 347 
 348 static unix_socket *unix_find_other(char *path, int *error)
     /* [previous][next][first][last][top][bottom][index][help] */
 349 {
 350         int old_fs;
 351         int err;
 352         struct inode *inode;
 353         unix_socket *u;
 354         
 355         old_fs=get_fs();
 356         set_fs(get_ds());
 357         err = open_namei(path, 2, S_IFSOCK, &inode, NULL);
 358         set_fs(old_fs);
 359         if(err<0)
 360         {
 361                 *error=err;
 362                 return NULL;
 363         }
 364         u=unix_find_socket(inode);
 365         iput(inode);
 366         if(u==NULL)
 367         {
 368                 *error=-ECONNREFUSED;
 369                 return NULL;
 370         }
 371         return u;
 372 }
 373 
 374 
 375 static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377         struct sockaddr_un *sun=(struct sockaddr_un *)uaddr;
 378         unix_socket *sk=sock->data;
 379         int old_fs;
 380         int err;
 381         
 382         if(addr_len>sizeof(struct sockaddr_un) || addr_len<3 || sun->sun_family!=AF_UNIX)
 383                 return -EINVAL;
 384         unix_mkname(sun, addr_len);
 385         /*
 386          *      Put ourselves in the filesystem
 387          */
 388         if(sk->protinfo.af_unix.inode!=NULL)
 389                 return -EINVAL;
 390         
 391         sk->protinfo.af_unix.name=kmalloc(addr_len+1, GFP_KERNEL);
 392         if(sk->protinfo.af_unix.name==NULL)
 393                 return -ENOMEM;
 394         memcpy(sk->protinfo.af_unix.name, sun->sun_path, addr_len+1);
 395         
 396         old_fs=get_fs();
 397         set_fs(get_ds());
 398         
 399         err=do_mknod(sk->protinfo.af_unix.name,S_IFSOCK|S_IRWXUGO,0);
 400         if(err==0)
 401                 err=open_namei(sk->protinfo.af_unix.name, 2, S_IFSOCK, &sk->protinfo.af_unix.inode, NULL);
 402         
 403         set_fs(old_fs);
 404         
 405         if(err<0)
 406         {
 407                 kfree_s(sk->protinfo.af_unix.name,addr_len+1);
 408                 sk->protinfo.af_unix.name=NULL;
 409                 if(err==-EEXIST)
 410                         return -EADDRINUSE;
 411                 else
 412                         return err;
 413         }
 414         
 415         return 0;
 416         
 417 }
 418 
 419 static int unix_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 420 {
 421         unix_socket *sk=sock->data;
 422         struct sockaddr_un *sun=(struct sockaddr_un *)uaddr;
 423         unix_socket *other;
 424         struct sk_buff *skb;
 425         int err;
 426 
 427         if(sk->type==SOCK_STREAM && sk->protinfo.af_unix.other)
 428         {
 429                 if(sock->state==SS_CONNECTING && sk->state==TCP_ESTABLISHED)
 430                 {
 431                         sock->state=SS_CONNECTED;
 432                         return 0;
 433                 }
 434                 if(sock->state==SS_CONNECTING && sk->state == TCP_CLOSE)
 435                 {
 436                         sock->state=SS_UNCONNECTED;
 437                         return -ECONNREFUSED;
 438                 }
 439                 if(sock->state==SS_CONNECTING)
 440                         return -EALREADY;
 441                 return -EISCONN;
 442         }
 443         
 444         if(addr_len < sizeof(sun->sun_family)+1 || sun->sun_family!=AF_UNIX)
 445                 return -EINVAL;
 446                 
 447         unix_mkname(sun, addr_len);
 448                 
 449         if(sk->type==SOCK_DGRAM && sk->protinfo.af_unix.other)
 450         {
 451                 sk->protinfo.af_unix.other->protinfo.af_unix.locks--;
 452                 sk->protinfo.af_unix.other=NULL;
 453                 sock->state=SS_UNCONNECTED;
 454         }
 455 
 456         if(sock->type==SOCK_DGRAM)
 457         {
 458                 other=unix_find_other(sun->sun_path, &err);
 459                 if(other==NULL)
 460                         return err;
 461                 other->protinfo.af_unix.locks++;
 462                 sk->protinfo.af_unix.other=other;
 463                 sock->state=SS_CONNECTED;
 464                 sk->state=TCP_ESTABLISHED;
 465                 return 0;                       /* Done */
 466         }
 467         
 468 
 469         if(sock->state==SS_UNCONNECTED)
 470         {
 471                 /*
 472                  *      Now ready to connect
 473                  */
 474          
 475                 skb=sock_alloc_send_skb(sk, 0, 0, 0, &err); /* Marker object */
 476                 if(skb==NULL)
 477                         return err;
 478                 skb->sk=sk;                             /* So they know it is us */
 479                 skb->free=1;
 480                 sk->state=TCP_CLOSE;
 481                 unix_mkname(sun, addr_len);
 482                 other=unix_find_other(sun->sun_path, &err);
 483                 if(other==NULL)
 484                 {
 485                         kfree_skb(skb, FREE_WRITE);
 486                         return err;
 487                 }
 488                 other->protinfo.af_unix.locks++;                /* Lock the other socket so it doesn't run off for a moment */
 489                 other->ack_backlog++;
 490                 sk->protinfo.af_unix.other=other;
 491                 skb_queue_tail(&other->receive_queue,skb);
 492                 sk->state=TCP_SYN_SENT;
 493                 sock->state=SS_CONNECTING;
 494                 sti();
 495                 other->data_ready(other,0);             /* Wake up ! */         
 496         }
 497                         
 498         
 499         /* Wait for an accept */
 500         
 501         cli();
 502         while(sk->state==TCP_SYN_SENT)
 503         {
 504                 if(flags&O_NONBLOCK)
 505                 {
 506                         sti();
 507                         return -EINPROGRESS;
 508                 }
 509                 interruptible_sleep_on(sk->sleep);
 510                 if(current->signal & ~current->blocked)
 511                 {
 512                         sti();
 513                         return -ERESTARTSYS;
 514                 }
 515         }
 516         
 517         /*
 518          *      Has the other end closed on us ?
 519          */
 520          
 521         if(sk->state==TCP_CLOSE)
 522         {
 523                 sk->protinfo.af_unix.other->protinfo.af_unix.locks--;
 524                 sk->protinfo.af_unix.other=NULL;
 525                 sock->state=SS_UNCONNECTED;
 526                 sti();
 527                 return -ECONNREFUSED;
 528         }
 529         
 530         /*
 531          *      Amazingly it has worked
 532          */
 533          
 534         sock->state=SS_CONNECTED;
 535         sti();
 536         return 0;
 537         
 538 }
 539 
 540 static int unix_socketpair(struct socket *a, struct socket *b)
     /* [previous][next][first][last][top][bottom][index][help] */
 541 {
 542         int err;
 543         unix_socket *ska,*skb;  
 544         
 545         err=unix_create(a, 0);
 546         if(err)
 547                 return err;
 548         err=unix_create(b, 0);
 549         if(err)
 550         {
 551                 unix_release(a, NULL);
 552                 a->data=NULL;
 553                 return err;
 554         }
 555 
 556         ska=a->data;
 557         skb=b->data;
 558 
 559         /* Join our sockets back to back */
 560         ska->protinfo.af_unix.locks++;
 561         skb->protinfo.af_unix.locks++;
 562         ska->protinfo.af_unix.other=skb;
 563         skb->protinfo.af_unix.other=ska;
 564         ska->state=TCP_ESTABLISHED;
 565         skb->state=TCP_ESTABLISHED;
 566         return 0;
 567 }
 568 
 569 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 570 {
 571         unix_socket *sk=sock->data;
 572         unix_socket *newsk, *tsk;
 573         struct sk_buff *skb;
 574         
 575         if(sk->type!=SOCK_STREAM)
 576         {
 577                 return -EOPNOTSUPP;
 578         }
 579         if(sk->state!=TCP_LISTEN)
 580         {
 581                 return -EINVAL;
 582         }
 583                 
 584         newsk=newsock->data;
 585         if(sk->protinfo.af_unix.name!=NULL)
 586         {
 587                 newsk->protinfo.af_unix.name=kmalloc(strlen(sk->protinfo.af_unix.name)+1, GFP_KERNEL);
 588                 if(newsk->protinfo.af_unix.name==NULL)
 589                         return -ENOMEM;
 590                 strcpy(newsk->protinfo.af_unix.name, sk->protinfo.af_unix.name);
 591         }
 592                 
 593         do
 594         {
 595                 cli();
 596                 skb=skb_dequeue(&sk->receive_queue);
 597                 if(skb==NULL)
 598                 {
 599                         if(flags&O_NONBLOCK)
 600                         {
 601                                 sti();
 602                                 return -EAGAIN;
 603                         }
 604                         interruptible_sleep_on(sk->sleep);
 605                         if(current->signal & ~current->blocked)
 606                         {
 607                                 sti();
 608                                 return -ERESTARTSYS;
 609                         }
 610                         sti();
 611                 }
 612         }
 613         while(skb==NULL);
 614         tsk=skb->sk;
 615         kfree_skb(skb, FREE_WRITE);     /* The buffer is just used as a tag */
 616         sk->ack_backlog--;
 617         newsk->protinfo.af_unix.other=tsk;
 618         tsk->protinfo.af_unix.other=newsk;
 619         tsk->state=TCP_ESTABLISHED;
 620         newsk->state=TCP_ESTABLISHED;
 621         newsk->protinfo.af_unix.locks++;        /* Swap lock over */
 622         sk->protinfo.af_unix.locks--;   /* Locked to child socket not master */
 623         tsk->protinfo.af_unix.locks++;  /* Back lock */
 624         sti();
 625         tsk->state_change(tsk);         /* Wake up any sleeping connect */
 626         sock_wake_async(tsk->socket, 0);
 627         return 0;
 628 }
 629 
 630 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 631 {
 632         unix_socket *sk=sock->data;
 633         struct sockaddr_un *sun=(struct sockaddr_un *)uaddr;
 634         
 635         if(peer)
 636         {
 637                 if(sk->protinfo.af_unix.other==NULL)
 638                         return -ENOTCONN;
 639                 sk=sk->protinfo.af_unix.other;
 640         }
 641         sun->sun_family=AF_UNIX;
 642         if(sk->protinfo.af_unix.name==NULL)
 643         {
 644                 *sun->sun_path=0;
 645                 *uaddr_len=sizeof(sun->sun_family)+1;
 646                 return 0;               /* Not bound */
 647         }
 648         *uaddr_len=sizeof(sun->sun_family)+strlen(sk->protinfo.af_unix.name)+1;
 649         strcpy(sun->sun_path,sk->protinfo.af_unix.name);                /* 108 byte limited */
 650         return 0;
 651 }
 652 
 653 static int unix_sendmsg(struct socket *sock, struct msghdr *msg, int len, int nonblock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 654 {
 655         unix_socket *sk=sock->data;
 656         unix_socket *other;
 657         struct sockaddr_un *sun=msg->msg_name;
 658         int err,size;
 659         struct sk_buff *skb;
 660         int limit=0;
 661         int sent=0;
 662 
 663         if(sk->err)
 664         {
 665                 cli();
 666                 err=sk->err;
 667                 sk->err=0;
 668                 sti();
 669                 return -err;
 670         }
 671         
 672         if(flags || msg->msg_accrights) /* For now */
 673                 return -EINVAL;
 674                 
 675         if(sun!=NULL)
 676         {
 677                 if(sock->type==SOCK_STREAM)
 678                 {
 679                         if(sk->state==TCP_ESTABLISHED)
 680                                 return -EISCONN;
 681                         else
 682                                 return -EOPNOTSUPP;
 683                 }
 684         }
 685         if(sun==NULL)
 686         {
 687                 if(sk->protinfo.af_unix.other==NULL)
 688                         return -ENOTCONN;
 689         }
 690 
 691 
 692         while(sent < len)
 693         {
 694                 /*
 695                  *      Optimisation for the fact that under 0.01% of X messages typically
 696                  *      need breaking up.
 697                  */
 698                  
 699                 size=len-sent;
 700 
 701                 if(size>(sk->sndbuf-sizeof(struct sk_buff))/2)  /* Keep two messages in the pipe so it schedules better */
 702                 {
 703                         if(sock->type==SOCK_DGRAM)
 704                                 return -EMSGSIZE;
 705                         size=(sk->sndbuf-sizeof(struct sk_buff))/2;
 706                 }
 707                 /*
 708                  *      Keep to page sized kmalloc()'s as various people
 709                  *      have suggested. Big mallocs stress the vm too
 710                  *      much.
 711                  */
 712 
 713                 if(size > 4000 && sock->type!=SOCK_DGRAM)
 714                         limit = 4000;   /* Fall back to 4K if we can't grab a big buffer this instant */
 715                 else
 716                         limit = 0;      /* Otherwise just grab and wait */
 717 
 718                 /*
 719                  *      Grab a buffer
 720                  */
 721                  
 722                 skb=sock_alloc_send_skb(sk,size,limit,nonblock, &err);
 723                 
 724                 if(skb==NULL)
 725                 {
 726                         if(sent)
 727                         {
 728                                 sk->err=-err;
 729                                 return sent;
 730                         }
 731                         return err;
 732                 }
 733                 size=skb_tailroom(skb);         /* If we dropped back on a limit then our skb is smaller */
 734 
 735                 skb->sk=sk;
 736                 skb->free=1;
 737                 
 738                 memcpy_fromiovec(skb_put(skb,size),msg->msg_iov, size);
 739 
 740                 cli();
 741                 if(sun==NULL)
 742                 {
 743                         other=sk->protinfo.af_unix.other;
 744                         if(sock->type==SOCK_DGRAM && other->dead)
 745                         {
 746                                 other->protinfo.af_unix.locks--;
 747                                 sk->protinfo.af_unix.other=NULL;
 748                                 sock->state=SS_UNCONNECTED;
 749                                 sti();
 750                                 if(!sent)
 751                                         return -ECONNRESET;
 752                                 else
 753                                         return sent;
 754                         }
 755                 }
 756                 else
 757                 {
 758                         unix_mkname(sun, msg->msg_namelen);
 759                         other=unix_find_other(sun->sun_path, &err);
 760                         if(other==NULL)
 761                         {
 762                                 kfree_skb(skb, FREE_WRITE);
 763                                 sti();
 764                                 if(sent)
 765                                         return sent;
 766                                 else
 767                                         return err;
 768                         }
 769                 }
 770                 skb_queue_tail(&other->receive_queue, skb);
 771                 sti();
 772                 other->data_ready(other,size);
 773                 sent+=size;
 774         }
 775         return sent;
 776 }
 777                 
 778 static int unix_recvmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, int flags, int *addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780         unix_socket *sk=sock->data;
 781         struct sockaddr_un *sun=msg->msg_name;
 782         int err;
 783         struct sk_buff *skb;
 784         int copied=0;
 785         unsigned char *sp;
 786         int len;
 787         int num;
 788         struct iovec *iov=msg->msg_iov;
 789         int ct=msg->msg_iovlen;
 790         
 791         if(addr_len)
 792                 *addr_len=0;
 793                 
 794         if(sk->err)
 795         {
 796                 cli();
 797                 err=sk->err;
 798                 sk->err=0;
 799                 sti();
 800                 return -err;
 801         }
 802         
 803 /*      printk("get rcv sem\n");*/
 804         down(&sk->protinfo.af_unix.readsem);            /* Lock the socket */
 805 /*      printk("got rcv sem\n");*/
 806 
 807         while(ct--)
 808         {
 809                 int done=0;
 810                 sp=iov->iov_base;
 811                 len=iov->iov_len;
 812                 iov++;
 813                 
 814                 while(done<len)
 815                 {
 816                         if(copied & (flags&MSG_PEEK))
 817                         {
 818                                 up(&sk->protinfo.af_unix.readsem);
 819                                 return copied;
 820                         }
 821                         cli();
 822                         skb=skb_peek(&sk->receive_queue);
 823                         if(skb==NULL)
 824                         {
 825                                 up(&sk->protinfo.af_unix.readsem);
 826                                 if(sk->shutdown & RCV_SHUTDOWN)
 827                                 {
 828                                         sti();
 829                                         return copied;
 830                                 }
 831                                 if(copied)
 832                                 {
 833                                         sti();
 834                                         return copied;
 835                                 }
 836                                 if(noblock)
 837                                 {
 838                                         sti();
 839                                         return -EAGAIN;
 840                                 }
 841                                 sk->socket->flags |= SO_WAITDATA;
 842                                 interruptible_sleep_on(sk->sleep);
 843                                 sk->socket->flags &= ~SO_WAITDATA;
 844                                 if( current->signal & ~current->blocked)
 845                                 {
 846                                         sti();
 847                                         if(copied)
 848                                                 return copied;
 849                                         return -ERESTARTSYS;
 850                                 }
 851                                 sti();
 852                                 down(&sk->protinfo.af_unix.readsem);
 853                                 continue;
 854                         }
 855                         if(msg->msg_name!=NULL)
 856                         {
 857                                 sun->sun_family=AF_UNIX;
 858                                 if(skb->sk->protinfo.af_unix.name)
 859                                 {
 860                                         memcpy(sun->sun_path, skb->sk->protinfo.af_unix.name, 108);
 861                                         if(addr_len)
 862                                                 *addr_len=strlen(sun->sun_path)+sizeof(short);
 863                                 }
 864                                 else
 865                                         if(addr_len)
 866                                                 *addr_len=sizeof(short);
 867                         }
 868                         num=min(skb->len,size-copied);
 869                         copied+=num;
 870                         done+=num;
 871                         if(flags&MSG_PEEK)
 872                         {
 873                                 memcpy_tofs(sp, skb->data, num);
 874                                 break;
 875                         }
 876                         else
 877                         {
 878                                 memcpy_tofs(sp, skb->data,num);
 879                                 skb_pull(skb,num);
 880                                 sp+=num;
 881                                 if(skb->len==0)
 882                                 {
 883                                         skb_unlink(skb);
 884                                         kfree_skb(skb, FREE_WRITE);
 885                                         if(sock->type==SOCK_DGRAM)
 886                                                 break;
 887                                 }
 888                         }
 889                         sti();
 890                 }       
 891         }       
 892         up(&sk->protinfo.af_unix.readsem);
 893         return copied;
 894 }
 895 
 896 static int unix_shutdown(struct socket *sock, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 897 {
 898         unix_socket *sk=(unix_socket *)sock->data;
 899         unix_socket *other=sk->protinfo.af_unix.other;
 900         if(mode&SEND_SHUTDOWN)
 901         {
 902                 sk->shutdown|=SEND_SHUTDOWN;
 903                 sk->state_change(sk);
 904                 if(other)
 905                 {
 906                         other->shutdown|=RCV_SHUTDOWN;
 907                         other->state_change(other);
 908                 }
 909         }
 910         other=sk->protinfo.af_unix.other;
 911         if(mode&RCV_SHUTDOWN)
 912         {
 913                 sk->shutdown|=RCV_SHUTDOWN;
 914                 sk->state_change(sk);
 915                 if(other)
 916                 {
 917                         other->shutdown|=SEND_SHUTDOWN;
 918                         other->state_change(other);
 919                 }
 920         }
 921         return 0;
 922 }
 923 
 924                 
 925 static int unix_select(struct socket *sock,  int sel_type, select_table *wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 926 {
 927         return datagram_select(sock->data,sel_type,wait);
 928 }
 929 
 930 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 931 {
 932         unix_socket *sk=sock->data;
 933         int err;
 934         long amount=0;
 935                         
 936         switch(cmd)
 937         {
 938         
 939                 case TIOCOUTQ:
 940                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
 941                         if(err)
 942                                 return err;
 943                         amount=sk->sndbuf-sk->wmem_alloc;
 944                         if(amount<0)
 945                                 amount=0;
 946                         put_fs_long(amount,(unsigned long *)arg);
 947                         return 0;
 948                 case TIOCINQ:
 949                 {
 950                         struct sk_buff *skb;
 951                         if(sk->state==TCP_LISTEN)
 952                                 return -EINVAL;
 953                         /* These two are safe on a single CPU system as only user tasks fiddle here */
 954                         if((skb=skb_peek(&sk->receive_queue))!=NULL)
 955                                 amount=skb->len;
 956                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
 957                         put_fs_long(amount,(unsigned long *)arg);
 958                         return 0;
 959                 }
 960 
 961                 default:
 962                         return -EINVAL;
 963         }
 964         /*NOTREACHED*/
 965         return(0);
 966 }
 967 
 968 static int unix_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 969 {
 970         off_t pos=0;
 971         off_t begin=0;
 972         int len=0;
 973         unix_socket *s=unix_socket_list;
 974         
 975         len+= sprintf(buffer,"Num       RefCount Protocol Flags    Type St Path\n");
 976         
 977         while(s!=NULL)
 978         {
 979                 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X",
 980                         s,
 981                         s->protinfo.af_unix.locks,
 982                         0,
 983                         s->socket->flags,
 984                         s->socket->type,
 985                         s->socket->state);
 986                 if(s->protinfo.af_unix.name!=NULL)
 987                         len+=sprintf(buffer+len, " %s\n", s->protinfo.af_unix.name);
 988                 else
 989                         buffer[len++]='\n';
 990                 
 991                 pos=begin+len;
 992                 if(pos<offset)
 993                 {
 994                         len=0;
 995                         begin=pos;
 996                 }
 997                 if(pos>offset+length)
 998                         break;
 999                 s=s->next;
1000         }
1001         *start=buffer+(offset-begin);
1002         len-=(offset-begin);
1003         if(len>length)
1004                 len=length;
1005         return len;
1006 }
1007 
1008 /*
1009  *      For AF_UNIX we flip everything into an iovec. If this doesnt do any speed harm then it will
1010  *      be easier for all the low levels to be totally iovec based.
1011  */
1012  
1013 static int unix_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1014                 struct sockaddr *sa, int *addr_len)
1015 {
1016         struct iovec iov;
1017         struct msghdr msg;
1018         iov.iov_base=ubuf;
1019         iov.iov_len=size;
1020         msg.msg_name=(void *)sa;
1021         msg.msg_namelen=0;
1022         if (addr_len)
1023                 msg.msg_namelen = *addr_len;
1024         msg.msg_accrights=NULL;
1025         msg.msg_iov=&iov;
1026         msg.msg_iovlen=1;
1027         return unix_recvmsg(sock,&msg,size,noblock,flags,addr_len);     
1028 }
1029 
1030 static int unix_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1031 {
1032         return unix_recvfrom(sock,ubuf,size,noblock,0,NULL,NULL);
1033 }
1034 
1035 static int unix_recv(struct socket *sock, void *ubuf, int size, int noblock, unsigned int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1036 {
1037         return unix_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
1038 }
1039 
1040 static int unix_sendto(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1041                 struct sockaddr *sa, int addr_len)
1042 {
1043         struct iovec iov;
1044         struct msghdr msg;
1045         iov.iov_base=(void *)ubuf;
1046         iov.iov_len=size;
1047         msg.msg_name=(void *)sa;
1048         msg.msg_namelen=addr_len;
1049         msg.msg_accrights=NULL;
1050         msg.msg_iov=&iov;
1051         msg.msg_iovlen=1;
1052         return unix_sendmsg(sock,&msg,size,noblock,flags);      
1053 }
1054 
1055 static int unix_write(struct socket *sock, const char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1056 {       
1057         return unix_sendto(sock,ubuf,size,noblock, 0, NULL, 0);
1058 }
1059 
1060 static int unix_send(struct socket *sock, const void *ubuf, int size, int noblock, unsigned int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1061 {
1062         return unix_sendto(sock,ubuf,size,noblock, flags, NULL, 0);
1063 }
1064 
1065 
1066 static struct proto_ops unix_proto_ops = {
1067         AF_UNIX,
1068         
1069         unix_create,
1070         unix_dup,
1071         unix_release,
1072         unix_bind,
1073         unix_connect,
1074         unix_socketpair,
1075         unix_accept,
1076         unix_getname,
1077         unix_read,
1078         unix_write,
1079         unix_select,
1080         unix_ioctl,
1081         unix_listen,
1082         unix_send,
1083         unix_recv,
1084         unix_sendto,
1085         unix_recvfrom,
1086         unix_shutdown,
1087         unix_setsockopt,
1088         unix_getsockopt,
1089         unix_fcntl,
1090         unix_sendmsg,
1091         unix_recvmsg
1092 };
1093 
1094 
1095 void unix_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1096 {
1097         printk("NET3: Unix domain sockets 0.09 BETA for Linux NET3.030.\n");
1098         sock_register(unix_proto_ops.family, &unix_proto_ops);
1099         proc_net_register(&(struct proc_dir_entry) {
1100                 PROC_NET_UNIX,  4, "unix",
1101                 S_IFREG | S_IRUGO, 1, 0, 0,
1102                 0, &proc_net_inode_operations,
1103                 unix_get_info
1104         });
1105 }
1106 /*
1107  * Local variables:
1108  *  compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1109  * End:
1110  */

/* [previous][next][first][last][top][bottom][index][help] */