openenvutils/commandshell/shell/src/modules/socket.c
changeset 0 2e3d3ce01487
child 1 0fdb7f6b0309
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 // socket.c - Unix domain socket module
       
     2 //
       
     3 // © Portions Copyright (c) Symbian Software Ltd 2007. All rights reserved.
       
     4 //
       
     5 /*
       
     6  * This file is part of zsh, the Z shell.
       
     7  *
       
     8  * Copyright (c) 2002 Peter Stephenson
       
     9  * All rights reserved.
       
    10  *
       
    11  * Permission is hereby granted, without written agreement and without
       
    12  * license or royalty fees, to use, copy, modify, and distribute this
       
    13  * software and to distribute modified versions of this software for any
       
    14  * purpose, provided that the above copyright notice and the following
       
    15  * two paragraphs appear in all copies of this software.
       
    16  *
       
    17  * In no event shall Peter Stephenson or the Zsh Development
       
    18  * Group be liable to any party for direct, indirect, special, incidental,
       
    19  * or consequential damages arising out of the use of this software and
       
    20  * its documentation, even if Peter Stephenson, and the Zsh
       
    21  * Development Group have been advised of the possibility of such damage.
       
    22  *
       
    23  * Peter Stephenson and the Zsh Development Group specifically
       
    24  * disclaim any warranties, including, but not limited to, the implied
       
    25  * warranties of merchantability and fitness for a particular purpose.  The
       
    26  * software provided hereunder is on an "as is" basis, and Peter Stephenson
       
    27  * and the Zsh Development Group have no obligation to provide maintenance,
       
    28  * support, updates, enhancements, or modifications.
       
    29  *
       
    30  */
       
    31 #include "socket.mdh"
       
    32 #include "socket.pro"
       
    33 
       
    34 #ifdef __SYMBIAN32__
       
    35 #include "dummy.h" //poll
       
    36 #endif //__SYMBIAN32__
       
    37 
       
    38 
       
    39 #include <sys/socket.h>
       
    40 #ifndef __SYMBIAN32__
       
    41 #include <sys/un.h>
       
    42 #else
       
    43 #include "un.h"
       
    44 #endif
       
    45 
       
    46 #ifndef UNIX_PATH_MAX
       
    47 # define UNIX_PATH_MAX 108
       
    48 #endif
       
    49 
       
    50 /*
       
    51  * We need to include the zsh headers later to avoid clashes with
       
    52  * the definitions on some systems, however we need the configuration
       
    53  * file to decide whether we can include netinet/in_systm.h, which
       
    54  * doesn't exist on cygwin.
       
    55  */
       
    56 
       
    57 /*
       
    58  * We use poll() in preference to select because some subset of manuals says
       
    59  * that's the thing to do, plus it's a bit less fiddly.  I don't actually
       
    60  * have access to a system with poll but not select, however, though
       
    61  * both bits of the code have been tested on a machine with both.
       
    62  */
       
    63 #ifdef HAVE_POLL_H
       
    64 #ifndef __SYMBIAN32__
       
    65 # include <sys/poll.h> 
       
    66 #else
       
    67 # include "poll.h"
       
    68 #endif
       
    69 #endif
       
    70 #if defined(HAVE_POLL) && !defined(POLLIN) && !defined(POLLNORM)
       
    71 # undef HAVE_POLL
       
    72 #endif
       
    73 
       
    74 #ifdef __SYMBIAN32__
       
    75 #ifdef __WINSCW__
       
    76 #pragma warn_unusedarg off
       
    77 #endif//__WINSCW__
       
    78 #endif//__SYMBIAN32__
       
    79 
       
    80 static int
       
    81 bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func))
       
    82 {
       
    83     int err=1, verbose=0, test=0, targetfd=0;
       
    84     ZSOCKLEN_T len;
       
    85     struct sockaddr_un soun;
       
    86     int sfd;
       
    87 
       
    88     if (OPT_ISSET(ops,'v'))
       
    89 	verbose = 1;
       
    90 
       
    91     if (OPT_ISSET(ops,'t'))
       
    92 	test = 1;
       
    93 
       
    94     if (OPT_ISSET(ops,'d')) {
       
    95 	targetfd = atoi(OPT_ARG(ops,'d'));
       
    96 	if (!targetfd) {
       
    97 	    zwarnnam(nam, "%s is an invalid argument to -d",
       
    98 		     OPT_ARG(ops, 'd'), 0);
       
    99 	    return 1;
       
   100 	}
       
   101     }
       
   102 
       
   103     if (OPT_ISSET(ops,'l')) {
       
   104 	char *localfn;
       
   105 
       
   106 	if (!args[0]) {
       
   107 	    zwarnnam(nam, "-l requires an argument", NULL, 0);
       
   108 	    return 1;
       
   109 	}
       
   110 
       
   111 	localfn = args[0];
       
   112 
       
   113 	sfd = socket(PF_UNIX, SOCK_STREAM, 0);
       
   114 
       
   115 	if (sfd == -1) {
       
   116 	    zwarnnam(nam, "socket error: %e ", NULL, errno);
       
   117 	    return 1;
       
   118 	}
       
   119 
       
   120 	soun.sun_family = AF_UNIX;
       
   121 	strncpy(soun.sun_path, localfn, UNIX_PATH_MAX);
       
   122 
       
   123 	if (bind(sfd, (struct sockaddr *)&soun, sizeof(struct sockaddr_un)))
       
   124 	{
       
   125 	    zwarnnam(nam, "could not bind to %s: %e", soun.sun_path, errno);
       
   126 	    close(sfd);
       
   127 	    return 1;
       
   128 	}
       
   129 
       
   130 	if (listen(sfd, 1))
       
   131 	{
       
   132 	    zwarnnam(nam, "could not listen on socket: %e", NULL, errno);
       
   133 	    close(sfd);
       
   134 	    return 1;
       
   135 	}
       
   136 
       
   137 	if (targetfd) {
       
   138 	    redup(sfd, targetfd);
       
   139 	    sfd = targetfd;
       
   140 	}
       
   141 	else {
       
   142 	    /* move the fd since no one will want to read from it */
       
   143 	    sfd = movefd(sfd);
       
   144 	}
       
   145 
       
   146 	setiparam("REPLY", sfd);
       
   147 
       
   148 	if (verbose)
       
   149 	    printf("%s listener is on fd %d\n", soun.sun_path, sfd);
       
   150 
       
   151 	return 0;
       
   152 
       
   153     }
       
   154     else if (OPT_ISSET(ops,'a'))
       
   155     {
       
   156 	int lfd, rfd;
       
   157 
       
   158 	if (!args[0]) {
       
   159 	    zwarnnam(nam, "-a requires an argument", NULL, 0);
       
   160 	    return 1;
       
   161 	}
       
   162 
       
   163 	lfd = atoi(args[0]);
       
   164 
       
   165 	if (!lfd) {
       
   166 	    zwarnnam(nam, "invalid numerical argument", NULL, 0);
       
   167 	    return 1;
       
   168 	}
       
   169 
       
   170 	if (test) {
       
   171 #if defined(HAVE_POLL) || defined(HAVE_SELECT)
       
   172 # ifdef HAVE_POLL
       
   173 	    struct pollfd pfd;
       
   174 	    int ret;
       
   175 
       
   176 	    pfd.fd = lfd;
       
   177 	    pfd.events = POLLIN;
       
   178 	    if ((ret = poll(&pfd, 1, 0)) == 0) return 1;
       
   179 	    else if (ret == -1)
       
   180 	    {
       
   181 		zwarnnam(nam, "poll error: %e", NULL, errno);
       
   182 		return 1;
       
   183 	    }
       
   184 # else
       
   185 	    fd_set rfds;
       
   186 	    struct timeval tv;
       
   187 	    int ret;
       
   188 	    
       
   189 	    FD_ZERO(&rfds);
       
   190 	    FD_SET(lfd, &rfds);
       
   191 	    tv.tv_sec = 0;
       
   192 	    tv.tv_usec = 0;
       
   193 	    
       
   194 	    if ((ret = select(lfd+1, &rfds, NULL, NULL, &tv))) return 1;
       
   195 	    else if (ret == -1)
       
   196 	    {
       
   197 		zwarnnam(nam, "select error: %e", NULL, errno);
       
   198 		return 1;
       
   199 	    }
       
   200 	    
       
   201 # endif
       
   202 	    
       
   203 #else
       
   204 	    zwarnnam(nam, "not currently supported", NULL, 0);
       
   205 	    return 1;
       
   206 #endif
       
   207 	}
       
   208 
       
   209 	len = sizeof(soun);
       
   210 	if ((rfd = accept(lfd, (struct sockaddr *)&soun, &len)) == -1)
       
   211 	{
       
   212 	    zwarnnam(nam, "could not accept connection: %e", NULL, errno);
       
   213 	    return 1;
       
   214 	}
       
   215 
       
   216 	if (targetfd) {
       
   217 	    redup(rfd, targetfd);
       
   218 	    sfd = targetfd;
       
   219 	}
       
   220 	else {
       
   221 	    sfd = rfd;
       
   222 	}
       
   223 
       
   224 	setiparam("REPLY", sfd);
       
   225 
       
   226 	if (verbose)
       
   227 	    printf("new connection from %s is on fd %d\n", soun.sun_path, sfd);
       
   228     }
       
   229     else
       
   230     {
       
   231 	if (!args[0]) {
       
   232 	    zwarnnam(nam, "zsocket requires an argument", NULL, 0);
       
   233 	    return 1;
       
   234 	}
       
   235 
       
   236 	sfd = socket(PF_UNIX, SOCK_STREAM, 0);
       
   237 
       
   238 	if (sfd == -1) {
       
   239 	    zwarnnam(nam, "socket creation failed: %e", NULL, errno);
       
   240 	    return 1;
       
   241 	}
       
   242 
       
   243 	soun.sun_family = AF_UNIX;
       
   244 	strncpy(soun.sun_path, args[0], UNIX_PATH_MAX);
       
   245 	
       
   246 	if ((err = connect(sfd, (struct sockaddr *)&soun, sizeof(struct sockaddr_un)))) {
       
   247 	    zwarnnam(nam, "connection failed: %e", NULL, errno);
       
   248 	    close(sfd);
       
   249 	    return 1;
       
   250 	}
       
   251 	else
       
   252 	{
       
   253 	    if (targetfd) {
       
   254 		redup(sfd, targetfd);
       
   255 		sfd = targetfd;
       
   256 	    }
       
   257 
       
   258 	    setiparam("REPLY", sfd);
       
   259 
       
   260 	    if (verbose)
       
   261 		printf("%s is now on fd %d\n", soun.sun_path, sfd);
       
   262 	}
       
   263 	
       
   264     }
       
   265 
       
   266     return 0;
       
   267 }
       
   268 
       
   269 static struct builtin bintab[] = {
       
   270     BUILTIN("zsocket", 0, bin_zsocket, 0, 3, 0, "ad:ltv", NULL),
       
   271 };
       
   272 
       
   273 /* The load/unload routines required by the zsh library interface */
       
   274 
       
   275 /**/
       
   276 int
       
   277 setup_(UNUSED(Module m))
       
   278 {
       
   279     return 0;
       
   280 }
       
   281 
       
   282 /**/
       
   283 int
       
   284 boot_(Module m)
       
   285 {
       
   286     return !addbuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
       
   287 }
       
   288 
       
   289 
       
   290 /**/
       
   291 int
       
   292 cleanup_(Module m)
       
   293 {
       
   294     deletebuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
       
   295     return 0;
       
   296 }
       
   297 
       
   298 /**/
       
   299 int
       
   300 finish_(UNUSED(Module m))
       
   301 {
       
   302     return 0;
       
   303 }