2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.20 2007-10-09 06:00:56 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 /* stat(2) masks: S_IFMT and S_IFSOCK may not be defined in gcc -ansi mode */
53 #define S_IFMT 0170000
54 #define S_IFSOCK 0140000
58 static int unix_close(COMSTACK h);
59 static int unix_put(COMSTACK h, char *buf, int size);
60 static int unix_get(COMSTACK h, char **buf, int *bufsize);
61 static int unix_connect(COMSTACK h, void *address);
62 static int unix_more(COMSTACK h);
63 static int unix_rcvconnect(COMSTACK h);
64 static int unix_bind(COMSTACK h, void *address, int mode);
65 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
66 int (*check_ip)(void *cd, const char *a, int len, int type),
68 static int unix_set_blocking(COMSTACK p, int blocking);
70 static COMSTACK unix_accept(COMSTACK h);
71 static char *unix_addrstr(COMSTACK h);
72 static void *unix_straddr(COMSTACK h, const char *str);
75 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
76 + strlen ((ptr)->sun_path))
84 /* this state is used for both SSL and straight TCP/IP */
85 typedef struct unix_state
87 char *altbuf; /* alternate buffer for surplus data */
88 int altsize; /* size as xmalloced */
89 int altlen; /* length of data or 0 if none */
91 int written; /* -1 if we aren't writing */
92 int towrite; /* to verify against user input */
93 int (*complete)(const char *buf, int len); /* length/complete. */
94 struct sockaddr_un addr; /* returned by cs_straddr */
98 char buf[128]; /* returned by cs_addrstr */
101 static int unix_init (void)
107 * This function is always called through the cs_create() macro.
108 * s >= 0: socket has already been established for us.
110 COMSTACK unix_type(int s, int flags, int protocol, void *vp)
120 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
126 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
128 if (!(state = (struct unix_state *)(p->cprivate =
129 xmalloc(sizeof(unix_state)))))
133 if (!(p->flags&CS_FLAGS_BLOCKING))
135 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
138 signal (SIGPIPE, SIG_IGN);
145 p->protocol = (enum oid_proto) protocol;
147 p->f_connect = unix_connect;
148 p->f_rcvconnect = unix_rcvconnect;
151 p->f_close = unix_close;
152 p->f_more = unix_more;
153 p->f_bind = unix_bind;
154 p->f_listen = unix_listen;
155 p->f_accept = unix_accept;
156 p->f_addrstr = unix_addrstr;
157 p->f_straddr = unix_straddr;
158 p->f_set_blocking = unix_set_blocking;
160 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
167 state->altsize = state->altlen = 0;
168 state->towrite = state->written = -1;
169 if (protocol == PROTO_WAIS)
170 state->complete = completeWAIS;
172 state->complete = cs_complete_auto;
174 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
175 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
181 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
186 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
187 add->sun_family = AF_UNIX;
188 strncpy(add->sun_path, str, sizeof(add->sun_path)-1);
189 add->sun_path[sizeof(add->sun_path)-1] = 0;
190 cp = strchr (add->sun_path, ':');
196 static void *unix_straddr1(COMSTACK h, const char *str, char *f)
198 unix_state *sp = (unix_state *)h->cprivate;
200 const char * file = NULL;
203 sp->uid = sp->gid = sp->umask = -1;
205 if ((eol = strchr(s, ',')))
209 if ((eol = strchr(s, ',')))
211 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
214 if (strspn(arg, "0123456789") == strlen(arg))
220 struct passwd * pw = getpwnam(arg);
223 printf("No such user\n");
226 sp->uid = pw->pw_uid;
229 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
232 if (strspn(arg, "0123456789") == strlen(arg))
238 struct group * gr = getgrnam(arg);
241 printf("No such group\n");
244 sp->gid = gr->gr_gid;
247 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
252 sp->umask = strtol(arg, &end, 8);
253 if (errno == EINVAL ||
256 printf("Invalid umask\n");
260 else if (file == NULL && strncmp(s, "file=", 5) == 0)
267 printf("invalid or double argument: %s\n", s);
282 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
284 if (!unix_strtoaddr_ex (file, &sp->addr))
289 static void *unix_straddr(COMSTACK h, const char *str)
291 char *f = xstrdup(str);
292 void *vp = unix_straddr1(h, str, f);
297 struct sockaddr_un *unix_strtoaddr(const char *str)
299 static struct sockaddr_un add;
301 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
303 if (!unix_strtoaddr_ex (str, &add))
308 static int unix_more(COMSTACK h)
310 unix_state *sp = (unix_state *)h->cprivate;
312 return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
316 * connect(2) will block (sometimes) - nothing we can do short of doing
317 * weird things like spawning subprocesses or threading or some weird junk
320 static int unix_connect(COMSTACK h, void *address)
322 struct sockaddr_un *add = (struct sockaddr_un *)address;
326 TRC(fprintf(stderr, "unix_connect\n"));
328 if (h->state != CS_ST_UNBND)
330 h->cerrno = CSOUTSTATE;
333 for (i = 0; i<3; i++)
335 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
336 if (r < 0 && yaz_errno() == EAGAIN)
339 usleep(i*10000+1000); /* 1ms, 11ms, 21ms */
350 if (yaz_errno() == EINPROGRESS)
352 h->event = CS_CONNECT;
353 h->state = CS_ST_CONNECTING;
354 h->io_pending = CS_WANT_WRITE;
360 h->event = CS_CONNECT;
361 h->state = CS_ST_CONNECTING;
363 return unix_rcvconnect (h);
369 static int unix_rcvconnect(COMSTACK h)
371 TRC(fprintf(stderr, "unix_rcvconnect\n"));
373 if (h->state == CS_ST_DATAXFER)
375 if (h->state != CS_ST_CONNECTING)
377 h->cerrno = CSOUTSTATE;
381 h->state = CS_ST_DATAXFER;
385 static int unix_bind(COMSTACK h, void *address, int mode)
387 unix_state *sp = (unix_state *)h->cprivate;
388 struct sockaddr *addr = (struct sockaddr *)address;
389 const char * path = ((struct sockaddr_un *)addr)->sun_path;
390 struct stat stat_buf;
392 TRC (fprintf (stderr, "unix_bind\n"));
394 if(stat(path, &stat_buf) != -1) {
395 struct sockaddr_un socket_unix;
398 if((stat_buf.st_mode&S_IFMT) != S_IFSOCK) { /* used to be S_ISSOCK */
400 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
403 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
407 socket_unix.sun_family = AF_UNIX;
408 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path)-1);
409 socket_unix.sun_path[sizeof(socket_unix.sun_path)-1] = 0;
410 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
411 if(yaz_errno() == ECONNREFUSED) {
412 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
420 yaz_set_errno(EADDRINUSE);
426 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
431 chown(path, sp->uid, sp->gid);
432 chmod(path, sp->umask != -1 ? sp->umask : 0666);
433 if (mode == CS_SERVER && listen(h->iofile, 100) < 0)
438 h->state = CS_ST_IDLE;
439 h->event = CS_LISTEN;
443 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
444 int (*check_ip)(void *cd, const char *a, int len, int t),
447 struct sockaddr_un addr;
448 YAZ_SOCKLEN_T len = sizeof(addr);
450 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
451 if (h->state != CS_ST_IDLE)
453 h->cerrno = CSOUTSTATE;
456 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
460 yaz_errno() == EWOULDBLOCK
462 #if EAGAIN != EWOULDBLOCK
463 || yaz_errno() == EAGAIN
467 h->cerrno = CSNODATA;
472 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
473 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
476 h->state = CS_ST_INCON;
480 static COMSTACK unix_accept(COMSTACK h)
483 unix_state *state, *st = (unix_state *)h->cprivate;
485 TRC(fprintf(stderr, "unix_accept\n"));
486 if (h->state == CS_ST_INCON)
488 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
495 memcpy(cnew, h, sizeof(*h));
496 cnew->iofile = h->newfd;
497 cnew->io_pending = 0;
498 if (!(state = (unix_state *)
499 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
509 if (!(cnew->flags&CS_FLAGS_BLOCKING) &&
510 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
525 state->altsize = state->altlen = 0;
526 state->towrite = state->written = -1;
527 state->complete = st->complete;
528 memcpy(&state->addr, &st->addr, sizeof(state->addr));
529 cnew->state = CS_ST_ACCEPT;
530 cnew->event = CS_NONE;
531 h->state = CS_ST_IDLE;
535 if (h->state == CS_ST_ACCEPT)
540 h->cerrno = CSOUTSTATE;
544 h->state = CS_ST_DATAXFER;
549 #define CS_UNIX_BUFCHUNK 4096
552 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
553 * 0=connection closed.
555 static int unix_get(COMSTACK h, char **buf, int *bufsize)
557 unix_state *sp = (unix_state *)h->cprivate;
559 int tmpi, berlen, rest, req, tomove;
560 int hasread = 0, res;
562 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
563 if (sp->altlen) /* switch buffers */
565 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
566 (unsigned) sp->altbuf));
570 *bufsize = sp->altsize;
571 hasread = sp->altlen;
577 while (!(berlen = (*sp->complete)(*buf, hasread)))
581 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
584 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
585 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
587 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
588 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
591 if (yaz_errno() == EWOULDBLOCK
593 #if EAGAIN != EWOULDBLOCK
594 || yaz_errno() == EAGAIN
597 || yaz_errno() == EINPROGRESS
600 h->io_pending = CS_WANT_READ;
603 else if (yaz_errno() == 0)
612 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
614 /* move surplus buffer (or everything if we didn't get a BER rec.) */
615 if (hasread > berlen)
617 tomove = req = hasread - berlen;
618 rest = tomove % CS_UNIX_BUFCHUNK;
620 req += CS_UNIX_BUFCHUNK - rest;
623 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
625 } else if (sp->altsize < req)
626 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
628 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
629 (unsigned) sp->altbuf));
630 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
632 if (berlen < CS_UNIX_BUFCHUNK - 1)
633 *(*buf + berlen) = '\0';
634 return berlen ? berlen : 1;
641 * In nonblocking mode, you must call again with same buffer while
644 static int unix_put(COMSTACK h, char *buf, int size)
647 struct unix_state *state = (struct unix_state *)h->cprivate;
649 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
652 if (state->towrite < 0)
654 state->towrite = size;
657 else if (state->towrite != size)
659 h->cerrno = CSWRONGBUF;
662 while (state->towrite > state->written)
665 send(h->iofile, buf + state->written, size -
675 yaz_errno() == EWOULDBLOCK
677 #if EAGAIN != EWOULDBLOCK
678 || yaz_errno() == EAGAIN
683 TRC(fprintf(stderr, " Flow control stop\n"));
684 h->io_pending = CS_WANT_WRITE;
690 state->written += res;
691 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
692 res, state->written, size));
694 state->towrite = state->written = -1;
695 TRC(fprintf(stderr, " Ok\n"));
699 static int unix_close(COMSTACK h)
701 unix_state *sp = (struct unix_state *)h->cprivate;
703 TRC(fprintf(stderr, "unix_close\n"));
715 static char *unix_addrstr(COMSTACK h)
717 unix_state *sp = (struct unix_state *)h->cprivate;
719 sprintf(buf, "unix:%s", sp->addr.sun_path);
723 static int unix_set_blocking(COMSTACK p, int flags)
727 if (p->flags == flags)
729 flag = fcntl(p->iofile, F_GETFL, 0);
730 if (flags & CS_FLAGS_BLOCKING)
731 flag = flag & ~O_NONBLOCK;
733 flag = flag | O_NONBLOCK;
734 if (fcntl(p->iofile, F_SETFL, flag) < 0)
743 * indent-tabs-mode: nil
745 * vim: shiftwidth=4 tabstop=8 expandtab