Back to index

glibc  2.9
socketpair.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992, 94, 95, 96, 97, 2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <fcntl.h>
00021 #include <sys/socket.h>
00022 #include <unistd.h>
00023 
00024 #include <hurd.h>
00025 #include <hurd/fd.h>
00026 #include <hurd/socket.h>
00027 
00028 /* Create two new sockets, of type TYPE in domain DOMAIN and using
00029    protocol PROTOCOL, which are connected to each other, and put file
00030    descriptors for them in FDS[0] and FDS[1].  If PROTOCOL is zero,
00031    one will be chosen automatically.  Returns 0 on success, -1 for errors.  */
00032 int
00033 __socketpair (int domain, int type, int protocol, int fds[2])
00034 {
00035   error_t err;
00036   socket_t server, sock1, sock2;
00037   int d1, d2;
00038 
00039   if (fds == NULL)
00040     return __hurd_fail (EINVAL);
00041 
00042   /* Find the domain's socket server.  */
00043   server = _hurd_socket_server (domain, 0);
00044   if (server == MACH_PORT_NULL)
00045     return -1;
00046 
00047   /* Create two sockets and connect them together.  */
00048 
00049   err = __socket_create (server, type, protocol, &sock1);
00050   if (err == MACH_SEND_INVALID_DEST || err == MIG_SERVER_DIED
00051       || err == MIG_BAD_ID || err == EOPNOTSUPP)
00052     {
00053       /* On the first use of the socket server during the operation,
00054         allow for the old server port dying.  */
00055       server = _hurd_socket_server (domain, 1);
00056       if (server == MACH_PORT_NULL)
00057        return -1;
00058       err = __socket_create (server, type, protocol, &sock1);
00059     }
00060   if (err)
00061     return __hurd_fail (err);
00062   if (err = __socket_create (server, type, protocol, &sock2))
00063     {
00064       __mach_port_deallocate (__mach_task_self (), sock1);
00065       return __hurd_fail (err);
00066     }
00067   if (err = __socket_connect2 (sock1, sock2))
00068     {
00069       __mach_port_deallocate (__mach_task_self (), sock1);
00070       __mach_port_deallocate (__mach_task_self (), sock2);
00071       return __hurd_fail (err);
00072     }
00073 
00074   /* Put the sockets into file descriptors.  */
00075 
00076   d1 = _hurd_intern_fd (sock1, O_IGNORE_CTTY, 1);
00077   if (d1 < 0)
00078     {
00079       __mach_port_deallocate (__mach_task_self (), sock2);
00080       return -1;
00081     }
00082   d2 = _hurd_intern_fd (sock2, O_IGNORE_CTTY, 1);
00083   if (d2 < 0)
00084     {
00085       err = errno;
00086       (void) close (d1);
00087       return __hurd_fail (err);
00088     }
00089 
00090   fds[0] = d1;
00091   fds[1] = d2;
00092   return 0;
00093 }
00094 
00095 weak_alias (__socketpair, socketpair)