2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.11 2005-02-01 14:37:47 adam Exp $
6 * UNIX socket COMSTACK. By Morten Bøgeskov.
10 * \brief Implements UNIX domain socket COMSTACK
20 #include <sys/types.h>
26 #include <sys/socket.h>
47 #define YAZ_SOCKLEN_T int
50 static int unix_close(COMSTACK h);
51 static int unix_put(COMSTACK h, char *buf, int size);
52 static int unix_get(COMSTACK h, char **buf, int *bufsize);
53 static int unix_connect(COMSTACK h, void *address);
54 static int unix_more(COMSTACK h);
55 static int unix_rcvconnect(COMSTACK h);
56 static int unix_bind(COMSTACK h, void *address, int mode);
57 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
58 int (*check_ip)(void *cd, const char *a, int len, int type),
60 static int unix_set_blocking(COMSTACK p, int blocking);
62 static COMSTACK unix_accept(COMSTACK h);
63 static char *unix_addrstr(COMSTACK h);
64 static void *unix_straddr(COMSTACK h, const char *str);
67 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
68 + strlen ((ptr)->sun_path))
76 /* this state is used for both SSL and straight TCP/IP */
77 typedef struct unix_state
79 char *altbuf; /* alternate buffer for surplus data */
80 int altsize; /* size as xmalloced */
81 int altlen; /* length of data or 0 if none */
83 int written; /* -1 if we aren't writing */
84 int towrite; /* to verify against user input */
85 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
86 struct sockaddr_un addr; /* returned by cs_straddr */
90 char buf[128]; /* returned by cs_addrstr */
93 static int unix_init (void)
99 * This function is always called through the cs_create() macro.
100 * s >= 0: socket has already been established for us.
102 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
112 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
118 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
120 if (!(state = (struct unix_state *)(p->cprivate =
121 xmalloc(sizeof(unix_state)))))
124 if (!((p->blocking = blocking)&1))
126 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
129 signal (SIGPIPE, SIG_IGN);
136 p->protocol = (enum oid_proto) protocol;
138 p->f_connect = unix_connect;
139 p->f_rcvconnect = unix_rcvconnect;
142 p->f_close = unix_close;
143 p->f_more = unix_more;
144 p->f_bind = unix_bind;
145 p->f_listen = unix_listen;
146 p->f_accept = unix_accept;
147 p->f_addrstr = unix_addrstr;
148 p->f_straddr = unix_straddr;
149 p->f_set_blocking = unix_set_blocking;
151 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
158 state->altsize = state->altlen = 0;
159 state->towrite = state->written = -1;
160 if (protocol == PROTO_WAIS)
161 state->complete = completeWAIS;
163 state->complete = cs_complete_auto;
165 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
166 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
172 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
177 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
178 add->sun_family = AF_UNIX;
179 strncpy(add->sun_path, str, sizeof(add->sun_path));
180 cp = strchr (add->sun_path, ':');
186 static void *unix_straddr(COMSTACK h, const char *str)
188 unix_state *sp = (unix_state *)h->cprivate;
189 char * s = strdup(str);
191 const char * file = NULL;
194 sp->uid = sp->gid = sp->umask = -1;
196 if ((eol = strchr(s, ',')))
200 if ((eol = strchr(s, ',')))
202 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
205 if (strspn(arg, "0123456789") == strlen(arg))
211 struct passwd * pw = getpwnam(arg);
214 printf("No such user\n");
218 sp->uid = pw->pw_uid;
221 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
224 if (strspn(arg, "0123456789") == strlen(arg))
230 struct group * gr = getgrnam(arg);
233 printf("No such group\n");
237 sp->gid = gr->gr_gid;
240 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
245 sp->umask = strtol(arg, &end, 8);
246 if (errno == EINVAL ||
249 printf("Invalid umask\n");
254 else if (file == NULL && strncmp(s, "file=", 5) == 0)
261 printf("invalid or double argument: %s\n", s);
277 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
279 if (!unix_strtoaddr_ex (file, &sp->addr))
288 struct sockaddr_un *unix_strtoaddr(const char *str)
290 static struct sockaddr_un add;
292 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
294 if (!unix_strtoaddr_ex (str, &add))
299 static int unix_more(COMSTACK h)
301 unix_state *sp = (unix_state *)h->cprivate;
303 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
308 * connect(2) will block (sometimes) - nothing we can do short of doing
309 * weird things like spawning subprocesses or threading or some weird junk
312 static int unix_connect(COMSTACK h, void *address)
314 struct sockaddr_un *add = (struct sockaddr_un *)address;
317 TRC(fprintf(stderr, "unix_connect\n"));
319 if (h->state != CS_ST_UNBND)
321 h->cerrno = CSOUTSTATE;
324 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
327 if (yaz_errno() == EINPROGRESS)
329 h->event = CS_CONNECT;
330 h->state = CS_ST_CONNECTING;
331 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
337 h->event = CS_CONNECT;
338 h->state = CS_ST_CONNECTING;
340 return unix_rcvconnect (h);
346 static int unix_rcvconnect(COMSTACK h)
348 TRC(fprintf(stderr, "unix_rcvconnect\n"));
350 if (h->state == CS_ST_DATAXFER)
352 if (h->state != CS_ST_CONNECTING)
354 h->cerrno = CSOUTSTATE;
358 h->state = CS_ST_DATAXFER;
362 static int unix_bind(COMSTACK h, void *address, int mode)
364 unix_state *sp = (unix_state *)h->cprivate;
365 struct sockaddr *addr = (struct sockaddr *)address;
366 const char * path = ((struct sockaddr_un *)addr)->sun_path;
367 struct stat stat_buf;
369 TRC (fprintf (stderr, "unix_bind\n"));
371 if(stat(path, &stat_buf) != -1) {
372 struct sockaddr_un socket_unix;
374 if(! S_ISSOCK(stat_buf.st_mode)) {
376 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
379 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
383 socket_unix.sun_family = AF_UNIX;
384 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
385 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
386 if(yaz_errno() == ECONNREFUSED) {
387 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
395 yaz_set_errno(EADDRINUSE);
401 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
406 chown(path, sp->uid, sp->gid);
407 chmod(path, sp->umask != -1 ? sp->umask : 0666);
408 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
413 h->state = CS_ST_IDLE;
414 h->event = CS_LISTEN;
418 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
419 int (*check_ip)(void *cd, const char *a, int len, int t),
422 struct sockaddr_un addr;
423 YAZ_SOCKLEN_T len = sizeof(addr);
425 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
426 if (h->state != CS_ST_IDLE)
428 h->cerrno = CSOUTSTATE;
431 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
435 yaz_errno() == EWOULDBLOCK
437 #if EAGAIN != EWOULDBLOCK
438 || yaz_errno() == EAGAIN
442 h->cerrno = CSNODATA;
447 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
448 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
451 h->state = CS_ST_INCON;
455 static COMSTACK unix_accept(COMSTACK h)
458 unix_state *state, *st = (unix_state *)h->cprivate;
460 TRC(fprintf(stderr, "unix_accept\n"));
461 if (h->state == CS_ST_INCON)
463 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
470 memcpy(cnew, h, sizeof(*h));
471 cnew->iofile = h->newfd;
472 cnew->io_pending = 0;
473 if (!(state = (unix_state *)
474 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
484 if (!(cnew->blocking&1) &&
485 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
500 state->altsize = state->altlen = 0;
501 state->towrite = state->written = -1;
502 state->complete = st->complete;
503 memcpy(&state->addr, &st->addr, sizeof(state->addr));
504 cnew->state = CS_ST_ACCEPT;
505 cnew->event = CS_NONE;
506 h->state = CS_ST_IDLE;
510 if (h->state == CS_ST_ACCEPT)
515 h->cerrno = CSOUTSTATE;
519 h->state = CS_ST_DATAXFER;
524 #define CS_UNIX_BUFCHUNK 4096
527 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
528 * 0=connection closed.
530 static int unix_get(COMSTACK h, char **buf, int *bufsize)
532 unix_state *sp = (unix_state *)h->cprivate;
534 int tmpi, berlen, rest, req, tomove;
535 int hasread = 0, res;
537 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
538 if (sp->altlen) /* switch buffers */
540 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
541 (unsigned) sp->altbuf));
545 *bufsize = sp->altsize;
546 hasread = sp->altlen;
552 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
556 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
559 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
560 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
562 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
563 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
566 if (yaz_errno() == EWOULDBLOCK
568 #if EAGAIN != EWOULDBLOCK
569 || yaz_errno() == EAGAIN
572 || yaz_errno() == EINPROGRESS
575 h->io_pending = CS_WANT_READ;
578 else if (yaz_errno() == 0)
587 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
589 /* move surplus buffer (or everything if we didn't get a BER rec.) */
590 if (hasread > berlen)
592 tomove = req = hasread - berlen;
593 rest = tomove % CS_UNIX_BUFCHUNK;
595 req += CS_UNIX_BUFCHUNK - rest;
598 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
600 } else if (sp->altsize < req)
601 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
603 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
604 (unsigned) sp->altbuf));
605 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
607 if (berlen < CS_UNIX_BUFCHUNK - 1)
608 *(*buf + berlen) = '\0';
609 return berlen ? berlen : 1;
616 * In nonblocking mode, you must call again with same buffer while
619 static int unix_put(COMSTACK h, char *buf, int size)
622 struct unix_state *state = (struct unix_state *)h->cprivate;
624 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
627 if (state->towrite < 0)
629 state->towrite = size;
632 else if (state->towrite != size)
634 h->cerrno = CSWRONGBUF;
637 while (state->towrite > state->written)
640 send(h->iofile, buf + state->written, size -
650 yaz_errno() == EWOULDBLOCK
652 #if EAGAIN != EWOULDBLOCK
653 || yaz_errno() == EAGAIN
658 TRC(fprintf(stderr, " Flow control stop\n"));
659 h->io_pending = CS_WANT_WRITE;
665 state->written += res;
666 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
667 res, state->written, size));
669 state->towrite = state->written = -1;
670 TRC(fprintf(stderr, " Ok\n"));
674 static int unix_close(COMSTACK h)
676 unix_state *sp = (struct unix_state *)h->cprivate;
678 TRC(fprintf(stderr, "unix_close\n"));
690 static char *unix_addrstr(COMSTACK h)
692 unix_state *sp = (struct unix_state *)h->cprivate;
694 sprintf(buf, "unix:%s", sp->addr.sun_path);
698 static int unix_set_blocking(COMSTACK p, int blocking)
702 if (p->blocking == blocking)
704 flag = fcntl(p->iofile, F_GETFL, 0);
706 flag = flag & ~O_NONBLOCK;
708 flag = flag | O_NONBLOCK;
709 if (fcntl(p->iofile, F_SETFL, flag) < 0)
711 p->blocking = blocking;