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->allocation=GFP_KERNEL;
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 319 {
 320         return unix_create(newsock,0);
 321 }
 322 
 323 static int unix_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 324 {
 325         unix_socket *sk=sock->data;
 326         unix_socket *skpair;
 327         
 328         /* May not have data attached */
 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;       /* Person we send to (default) */
 336         if(sk->type==SOCK_STREAM && skpair!=NULL && skpair->state!=TCP_LISTEN)
 337         {
 338                 skpair->shutdown=SHUTDOWN_MASK;         /* No more writes */
 339                 skpair->state_change(skpair);           /* Wake any blocked writes */
 340         }
 341         if(skpair!=NULL)
 342                 skpair->protinfo.af_unix.locks--;               /* It may now die */
 343         sk->protinfo.af_unix.other=NULL;                        /* No pair */
 344         unix_destroy_socket(sk);                        /* Try and flush out this socket. Throw our buffers at least */
 345         return 0;
 346 }
 347 
 348 
 349 static unix_socket *unix_find_other(char *path, int *error)
     /* [previous][next][first][last][top][bottom][index][help] */
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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          *      Put ourselves in the filesystem
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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;                       /* Done */
 467         }
 468         
 469 
 470         if(sock->state==SS_UNCONNECTED)
 471         {
 472                 /*
 473                  *      Now ready to connect
 474                  */
 475          
 476                 skb=sock_alloc_send_skb(sk, 0, 0, 0, &err); /* Marker object */
 477                 if(skb==NULL)
 478                         return err;
 479                 skb->sk=sk;                             /* So they know it is us */
 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++;                /* Lock the other socket so it doesn't run off for a moment */
 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);             /* Wake up ! */         
 497         }
 498                         
 499         
 500         /* Wait for an accept */
 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          *      Has the other end closed on us ?
 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                 sti();
 528                 return -ECONNREFUSED;
 529         }
 530         
 531         /*
 532          *      Amazingly it has worked
 533          */
 534          
 535         sock->state=SS_CONNECTED;
 536         sti();
 537         return 0;
 538         
 539 }
 540 
 541 static int unix_socketpair(struct socket *a, struct socket *b)
     /* [previous][next][first][last][top][bottom][index][help] */
 542 {
 543         int err;
 544         unix_socket *ska,*skb;  
 545         
 546         err=unix_create(a, 0);
 547         if(err)
 548                 return err;
 549         err=unix_create(b, 0);
 550         if(err)
 551         {
 552                 unix_release(a, NULL);
 553                 a->data=NULL;
 554                 return err;
 555         }
 556 
 557         ska=a->data;
 558         skb=b->data;
 559 
 560         /* Join our sockets back to back */
 561         ska->protinfo.af_unix.locks++;
 562         skb->protinfo.af_unix.locks++;
 563         ska->protinfo.af_unix.other=skb;
 564         skb->protinfo.af_unix.other=ska;
 565         ska->state=TCP_ESTABLISHED;
 566         skb->state=TCP_ESTABLISHED;
 567         return 0;
 568 }
 569 
 570 static int unix_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 571 {
 572         unix_socket *sk=sock->data;
 573         unix_socket *newsk, *tsk;
 574         struct sk_buff *skb;
 575         
 576         if(sk->type!=SOCK_STREAM)
 577         {
 578                 return -EOPNOTSUPP;
 579         }
 580         if(sk->state!=TCP_LISTEN)
 581         {
 582                 return -EINVAL;
 583         }
 584                 
 585         newsk=newsock->data;
 586         if(sk->protinfo.af_unix.name!=NULL)
 587         {
 588                 newsk->protinfo.af_unix.name=kmalloc(strlen(sk->protinfo.af_unix.name)+1, GFP_KERNEL);
 589                 if(newsk->protinfo.af_unix.name==NULL)
 590                         return -ENOMEM;
 591                 strcpy(newsk->protinfo.af_unix.name, sk->protinfo.af_unix.name);
 592         }
 593                 
 594         do
 595         {
 596                 cli();
 597                 skb=skb_dequeue(&sk->receive_queue);
 598                 if(skb==NULL)
 599                 {
 600                         if(flags&O_NONBLOCK)
 601                         {
 602                                 sti();
 603                                 return -EAGAIN;
 604                         }
 605                         interruptible_sleep_on(sk->sleep);
 606                         if(current->signal & ~current->blocked)
 607                         {
 608                                 sti();
 609                                 return -ERESTARTSYS;
 610                         }
 611                         sti();
 612                 }
 613         }
 614         while(skb==NULL);
 615         tsk=skb->sk;
 616         kfree_skb(skb, FREE_WRITE);     /* The buffer is just used as a tag */
 617         sk->ack_backlog--;
 618         newsk->protinfo.af_unix.other=tsk;
 619         tsk->protinfo.af_unix.other=newsk;
 620         tsk->state=TCP_ESTABLISHED;
 621         newsk->state=TCP_ESTABLISHED;
 622         newsk->protinfo.af_unix.locks++;        /* Swap lock over */
 623         sk->protinfo.af_unix.locks--;   /* Locked to child socket not master */
 624         tsk->protinfo.af_unix.locks++;  /* Back lock */
 625         sti();
 626         tsk->state_change(tsk);         /* Wake up any sleeping connect */
 627         sock_wake_async(tsk->socket, 0);
 628         return 0;
 629 }
 630 
 631 static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 632 {
 633         unix_socket *sk=sock->data;
 634         struct sockaddr_un *sun=(struct sockaddr_un *)uaddr;
 635         
 636         if(peer)
 637         {
 638                 if(sk->protinfo.af_unix.other==NULL)
 639                         return -ENOTCONN;
 640                 sk=sk->protinfo.af_unix.other;
 641         }
 642         sun->sun_family=AF_UNIX;
 643         if(sk->protinfo.af_unix.name==NULL)
 644         {
 645                 *sun->sun_path=0;
 646                 *uaddr_len=sizeof(sun->sun_family)+1;
 647                 return 0;               /* Not bound */
 648         }
 649         *uaddr_len=sizeof(sun->sun_family)+strlen(sk->protinfo.af_unix.name)+1;
 650         strcpy(sun->sun_path,sk->protinfo.af_unix.name);                /* 108 byte limited */
 651         return 0;
 652 }
 653 
 654 static int unix_sendmsg(struct socket *sock, struct msghdr *msg, int len, int nonblock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 655 {
 656         unix_socket *sk=sock->data;
 657         unix_socket *other;
 658         struct sockaddr_un *sun=msg->msg_name;
 659         int err,size;
 660         struct sk_buff *skb;
 661         int limit=0;
 662         int sent=0;
 663 
 664         if(sk->err)
 665         {
 666                 cli();
 667                 err=sk->err;
 668                 sk->err=0;
 669                 sti();
 670                 return -err;
 671         }
 672         
 673         if(flags || msg->msg_accrights) /* For now */
 674                 return -EINVAL;
 675                 
 676         if(sun!=NULL)
 677         {
 678                 if(sock->type==SOCK_STREAM)
 679                 {
 680                         if(sk->state==TCP_ESTABLISHED)
 681                                 return -EISCONN;
 682                         else
 683                                 return -EOPNOTSUPP;
 684                 }
 685         }
 686         if(sun==NULL)
 687         {
 688                 if(sk->protinfo.af_unix.other==NULL)
 689                         return -ENOTCONN;
 690         }
 691 
 692 
 693         while(sent < len)
 694         {
 695                 /*
 696                  *      Optimisation for the fact that under 0.01% of X messages typically
 697                  *      need breaking up.
 698                  */
 699                  
 700                 size=len-sent;
 701 
 702                 if(size>(sk->sndbuf-sizeof(struct sk_buff))/2)  /* Keep two messages in the pipe so it schedules better */
 703                 {
 704                         if(sock->type==SOCK_DGRAM)
 705                                 return -EMSGSIZE;
 706                         size=(sk->sndbuf-sizeof(struct sk_buff))/2;
 707                 }
 708                 /*
 709                  *      Keep to page sized kmalloc()'s as various people
 710                  *      have suggested. Big mallocs stress the vm too
 711                  *      much.
 712                  */
 713 
 714                 if(size > 4000 && sock->type!=SOCK_DGRAM)
 715                         limit = 4000;   /* Fall back to 4K if we can't grab a big buffer this instant */
 716                 else
 717                         limit = 0;      /* Otherwise just grab and wait */
 718 
 719                 /*
 720                  *      Grab a buffer
 721                  */
 722                  
 723                 skb=sock_alloc_send_skb(sk,size,limit,nonblock, &err);
 724                 
 725                 if(skb==NULL)
 726                 {
 727                         if(sent)
 728                         {
 729                                 sk->err=-err;
 730                                 return sent;
 731                         }
 732                         return err;
 733                 }
 734                 size=skb_tailroom(skb);         /* If we dropped back on a limit then our skb is smaller */
 735 
 736                 skb->sk=sk;
 737                 skb->free=1;
 738                 
 739                 memcpy_fromiovec(skb_put(skb,size),msg->msg_iov, size);
 740 
 741                 cli();
 742                 if(sun==NULL)
 743                 {
 744                         other=sk->protinfo.af_unix.other;
 745                         if(sock->type==SOCK_DGRAM && other->dead)
 746                         {
 747                                 other->protinfo.af_unix.locks--;
 748                                 sk->protinfo.af_unix.other=NULL;
 749                                 sock->state=SS_UNCONNECTED;
 750                                 sti();
 751                                 if(!sent)
 752                                         return -ECONNRESET;
 753                                 else
 754                                         return sent;
 755                         }
 756                 }
 757                 else
 758                 {
 759                         unix_mkname(sun, msg->msg_namelen);
 760                         other=unix_find_other(sun->sun_path, &err);
 761                         if(other==NULL)
 762                         {
 763                                 kfree_skb(skb, FREE_WRITE);
 764                                 sti();
 765                                 if(sent)
 766                                         return sent;
 767                                 else
 768                                         return err;
 769                         }
 770                 }
 771                 skb_queue_tail(&other->receive_queue, skb);
 772                 sti();
 773                 other->data_ready(other,size);
 774                 sent+=size;
 775         }
 776         return sent;
 777 }
 778                 
 779 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] */
 780 {
 781         unix_socket *sk=sock->data;
 782         struct sockaddr_un *sun=msg->msg_name;
 783         int err;
 784         struct sk_buff *skb;
 785         int copied=0;
 786         unsigned char *sp;
 787         int len;
 788         int num;
 789         struct iovec *iov=msg->msg_iov;
 790         int ct=msg->msg_iovlen;
 791         
 792         if(addr_len)
 793                 *addr_len=0;
 794                 
 795         if(sk->err)
 796         {
 797                 cli();
 798                 err=sk->err;
 799                 sk->err=0;
 800                 sti();
 801                 return -err;
 802         }
 803         
 804 /*      printk("get rcv sem\n");*/
 805         down(&sk->protinfo.af_unix.readsem);            /* Lock the socket */
 806 /*      printk("got rcv sem\n");*/
 807 
 808         while(ct--)
 809         {
 810                 int done=0;
 811                 sp=iov->iov_base;
 812                 len=iov->iov_len;
 813                 iov++;
 814                 
 815                 while(done<len)
 816                 {
 817                         if(copied & (flags&MSG_PEEK))
 818                         {
 819                                 up(&sk->protinfo.af_unix.readsem);
 820                                 return copied;
 821                         }
 822                         cli();
 823                         skb=skb_peek(&sk->receive_queue);
 824                         if(skb==NULL)
 825                         {
 826                                 up(&sk->protinfo.af_unix.readsem);
 827                                 if(sk->shutdown & RCV_SHUTDOWN)
 828                                 {
 829                                         sti();
 830                                         return copied;
 831                                 }
 832                                 if(copied)
 833                                 {
 834                                         sti();
 835                                         return copied;
 836                                 }
 837                                 if(noblock)
 838                                 {
 839                                         sti();
 840                                         return -EAGAIN;
 841                                 }
 842                                 sk->socket->flags |= SO_WAITDATA;
 843                                 interruptible_sleep_on(sk->sleep);
 844                                 sk->socket->flags &= ~SO_WAITDATA;
 845                                 if( current->signal & ~current->blocked)
 846                                 {
 847                                         sti();
 848                                         if(copied)
 849                                                 return copied;
 850                                         return -ERESTARTSYS;
 851                                 }
 852                                 sti();
 853                                 down(&sk->protinfo.af_unix.readsem);
 854                                 continue;
 855                         }
 856                         if(msg->msg_name!=NULL)
 857                         {
 858                                 sun->sun_family=AF_UNIX;
 859                                 if(skb->sk->protinfo.af_unix.name)
 860                                 {
 861                                         memcpy(sun->sun_path, skb->sk->protinfo.af_unix.name, 108);
 862                                         if(addr_len)
 863                                                 *addr_len=strlen(sun->sun_path)+sizeof(short);
 864                                 }
 865                                 else
 866                                         if(addr_len)
 867                                                 *addr_len=sizeof(short);
 868                         }
 869                         num=min(skb->len,size-copied);
 870                         copied+=num;
 871                         done+=num;
 872                         if(flags&MSG_PEEK)
 873                         {
 874                                 memcpy_tofs(sp, skb->data, num);
 875                                 break;
 876                         }
 877                         else
 878                         {
 879                                 memcpy_tofs(sp, skb->data,num);
 880                                 skb_pull(skb,num);
 881                                 sp+=num;
 882                                 if(skb->len==0)
 883                                 {
 884                                         skb_unlink(skb);
 885                                         kfree_skb(skb, FREE_WRITE);
 886                                         if(sock->type==SOCK_DGRAM)
 887                                                 break;
 888                                 }
 889                         }
 890                         sti();
 891                 }       
 892         }       
 893         up(&sk->protinfo.af_unix.readsem);
 894         return copied;
 895 }
 896 
 897 static int unix_shutdown(struct socket *sock, int mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 898 {
 899         unix_socket *sk=(unix_socket *)sock->data;
 900         unix_socket *other=sk->protinfo.af_unix.other;
 901         if(mode&SEND_SHUTDOWN)
 902         {
 903                 sk->shutdown|=SEND_SHUTDOWN;
 904                 sk->state_change(sk);
 905                 if(other)
 906                 {
 907                         other->shutdown|=RCV_SHUTDOWN;
 908                         other->state_change(other);
 909                 }
 910         }
 911         other=sk->protinfo.af_unix.other;
 912         if(mode&RCV_SHUTDOWN)
 913         {
 914                 sk->shutdown|=RCV_SHUTDOWN;
 915                 sk->state_change(sk);
 916                 if(other)
 917                 {
 918                         other->shutdown|=SEND_SHUTDOWN;
 919                         other->state_change(other);
 920                 }
 921         }
 922         return 0;
 923 }
 924 
 925                 
 926 static int unix_select(struct socket *sock,  int sel_type, select_table *wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 927 {
 928         return datagram_select(sock->data,sel_type,wait);
 929 }
 930 
 931 static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 932 {
 933         unix_socket *sk=sock->data;
 934         int err;
 935         long amount=0;
 936                         
 937         switch(cmd)
 938         {
 939         
 940                 case TIOCOUTQ:
 941                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
 942                         if(err)
 943                                 return err;
 944                         amount=sk->sndbuf-sk->wmem_alloc;
 945                         if(amount<0)
 946                                 amount=0;
 947                         put_fs_long(amount,(unsigned long *)arg);
 948                         return 0;
 949                 case TIOCINQ:
 950                 {
 951                         struct sk_buff *skb;
 952                         if(sk->state==TCP_LISTEN)
 953                                 return -EINVAL;
 954                         /* These two are safe on a single CPU system as only user tasks fiddle here */
 955                         if((skb=skb_peek(&sk->receive_queue))!=NULL)
 956                                 amount=skb->len;
 957                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(unsigned long));
 958                         put_fs_long(amount,(unsigned long *)arg);
 959                         return 0;
 960                 }
 961 
 962                 default:
 963                         return -EINVAL;
 964         }
 965         /*NOTREACHED*/
 966         return(0);
 967 }
 968 
 969 static int unix_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
     /* [previous][next][first][last][top][bottom][index][help] */
 970 {
 971         off_t pos=0;
 972         off_t begin=0;
 973         int len=0;
 974         unix_socket *s=unix_socket_list;
 975         
 976         len+= sprintf(buffer,"Num       RefCount Protocol Flags    Type St Path\n");
 977         
 978         while(s!=NULL)
 979         {
 980                 len+=sprintf(buffer+len,"%p: %08X %08X %08lX %04X %02X",
 981                         s,
 982                         s->protinfo.af_unix.locks,
 983                         0,
 984                         s->socket->flags,
 985                         s->socket->type,
 986                         s->socket->state);
 987                 if(s->protinfo.af_unix.name!=NULL)
 988                         len+=sprintf(buffer+len, " %s\n", s->protinfo.af_unix.name);
 989                 else
 990                         buffer[len++]='\n';
 991                 
 992                 pos=begin+len;
 993                 if(pos<offset)
 994                 {
 995                         len=0;
 996                         begin=pos;
 997                 }
 998                 if(pos>offset+length)
 999                         break;
1000                 s=s->next;
1001         }
1002         *start=buffer+(offset-begin);
1003         len-=(offset-begin);
1004         if(len>length)
1005                 len=length;
1006         return len;
1007 }
1008 
1009 /*
1010  *      For AF_UNIX we flip everything into an iovec. If this doesnt do any speed harm then it will
1011  *      be easier for all the low levels to be totally iovec based.
1012  */
1013  
1014 static int unix_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1015                 struct sockaddr *sa, int *addr_len)
1016 {
1017         struct iovec iov;
1018         struct msghdr msg;
1019         iov.iov_base=ubuf;
1020         iov.iov_len=size;
1021         msg.msg_name=(void *)sa;
1022         msg.msg_namelen=0;
1023         if (addr_len)
1024                 msg.msg_namelen = *addr_len;
1025         msg.msg_accrights=NULL;
1026         msg.msg_iov=&iov;
1027         msg.msg_iovlen=1;
1028         return unix_recvmsg(sock,&msg,size,noblock,flags,addr_len);     
1029 }
1030 
1031 static int unix_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1032 {
1033         return unix_recvfrom(sock,ubuf,size,noblock,0,NULL,NULL);
1034 }
1035 
1036 static int unix_recv(struct socket *sock, void *ubuf, int size, int noblock, unsigned int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1037 {
1038         return unix_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
1039 }
1040 
1041 static int unix_sendto(struct socket *sock, const void *ubuf, int size, int noblock, unsigned flags,
     /* [previous][next][first][last][top][bottom][index][help] */
1042                 struct sockaddr *sa, int addr_len)
1043 {
1044         struct iovec iov;
1045         struct msghdr msg;
1046         iov.iov_base=(void *)ubuf;
1047         iov.iov_len=size;
1048         msg.msg_name=(void *)sa;
1049         msg.msg_namelen=addr_len;
1050         msg.msg_accrights=NULL;
1051         msg.msg_iov=&iov;
1052         msg.msg_iovlen=1;
1053         return unix_sendmsg(sock,&msg,size,noblock,flags);      
1054 }
1055 
1056 static int unix_write(struct socket *sock, const char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1057 {       
1058         return unix_sendto(sock,ubuf,size,noblock, 0, NULL, 0);
1059 }
1060 
1061 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] */
1062 {
1063         return unix_sendto(sock,ubuf,size,noblock, flags, NULL, 0);
1064 }
1065 
1066 
1067 static struct proto_ops unix_proto_ops = {
1068         AF_UNIX,
1069         
1070         unix_create,
1071         unix_dup,
1072         unix_release,
1073         unix_bind,
1074         unix_connect,
1075         unix_socketpair,
1076         unix_accept,
1077         unix_getname,
1078         unix_read,
1079         unix_write,
1080         unix_select,
1081         unix_ioctl,
1082         unix_listen,
1083         unix_send,
1084         unix_recv,
1085         unix_sendto,
1086         unix_recvfrom,
1087         unix_shutdown,
1088         unix_setsockopt,
1089         unix_getsockopt,
1090         unix_fcntl,
1091         unix_sendmsg,
1092         unix_recvmsg
1093 };
1094 
1095 
1096 void unix_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1097 {
1098         printk("NET3: Unix domain sockets 0.10 BETA for Linux NET3.031.\n");
1099         sock_register(unix_proto_ops.family, &unix_proto_ops);
1100         proc_net_register(&(struct proc_dir_entry) {
1101                 PROC_NET_UNIX,  4, "unix",
1102                 S_IFREG | S_IRUGO, 1, 0, 0,
1103                 0, &proc_net_inode_operations,
1104                 unix_get_info
1105         });
1106 }
1107 /*
1108  * Local variables:
1109  *  compile-command: "gcc -g -D__KERNEL__ -Wall -O6 -I/usr/src/linux/include -c af_unix.c"
1110  * End:
1111  */

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