2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.7 2004-11-18 15:18:14 heikki Exp $
6 * UNIX socket COMSTACK. By Morten Bøgeskov.
10 * \brief Implements UNIX domain socket COMSTACK
25 #include <sys/types.h>
27 #include <sys/socket.h>
31 #include <yaz/comstack.h>
36 #define YAZ_SOCKLEN_T int
39 static int unix_close(COMSTACK h);
40 static int unix_put(COMSTACK h, char *buf, int size);
41 static int unix_get(COMSTACK h, char **buf, int *bufsize);
42 static int unix_connect(COMSTACK h, void *address);
43 static int unix_more(COMSTACK h);
44 static int unix_rcvconnect(COMSTACK h);
45 static int unix_bind(COMSTACK h, void *address, int mode);
46 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
47 int (*check_ip)(void *cd, const char *a, int len, int type),
49 static int unix_set_blocking(COMSTACK p, int blocking);
51 static COMSTACK unix_accept(COMSTACK h);
52 static char *unix_addrstr(COMSTACK h);
53 static void *unix_straddr(COMSTACK h, const char *str);
56 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
57 + strlen ((ptr)->sun_path))
65 /* this state is used for both SSL and straight TCP/IP */
66 typedef struct unix_state
68 char *altbuf; /* alternate buffer for surplus data */
69 int altsize; /* size as xmalloced */
70 int altlen; /* length of data or 0 if none */
72 int written; /* -1 if we aren't writing */
73 int towrite; /* to verify against user input */
74 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
75 struct sockaddr_un addr; /* returned by cs_straddr */
79 char buf[128]; /* returned by cs_addrstr */
82 static int unix_init (void)
88 * This function is always called through the cs_create() macro.
89 * s >= 0: socket has already been established for us.
91 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
101 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
107 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
109 if (!(state = (struct unix_state *)(p->cprivate =
110 xmalloc(sizeof(unix_state)))))
113 if (!((p->blocking = blocking)&1))
115 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
118 signal (SIGPIPE, SIG_IGN);
125 p->protocol = (enum oid_proto) protocol;
127 p->f_connect = unix_connect;
128 p->f_rcvconnect = unix_rcvconnect;
131 p->f_close = unix_close;
132 p->f_more = unix_more;
133 p->f_bind = unix_bind;
134 p->f_listen = unix_listen;
135 p->f_accept = unix_accept;
136 p->f_addrstr = unix_addrstr;
137 p->f_straddr = unix_straddr;
138 p->f_set_blocking = unix_set_blocking;
140 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
146 state->altsize = state->altlen = 0;
147 state->towrite = state->written = -1;
148 if (protocol == PROTO_WAIS)
149 state->complete = completeWAIS;
151 state->complete = cs_complete_auto;
153 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
154 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
160 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
165 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
166 add->sun_family = AF_UNIX;
167 strncpy(add->sun_path, str, sizeof(add->sun_path));
168 cp = strchr (add->sun_path, ':');
174 static void *unix_straddr(COMSTACK h, const char *str)
176 unix_state *sp = (unix_state *)h->cprivate;
177 char * s = strdup(str);
179 const char * file = NULL;
182 sp->uid = sp->gid = sp->umask = -1;
184 if ((eol = strchr(s, ',')))
188 if ((eol = strchr(s, ',')))
190 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
193 if (strspn(arg, "0123456789") == strlen(arg))
199 struct passwd * pw = getpwnam(arg);
202 printf("No such user\n");
206 sp->uid = pw->pw_uid;
209 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
212 if (strspn(arg, "0123456789") == strlen(arg))
218 struct group * gr = getgrnam(arg);
221 printf("No such group\n");
225 sp->gid = gr->gr_gid;
228 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
233 sp->umask = strtol(arg, &end, 8);
234 if (errno == EINVAL ||
237 printf("Invalid umask\n");
242 else if (file == NULL && strncmp(s, "file=", 5) == 0)
249 printf("invalid or double argument: %s\n", s);
265 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
267 if (!unix_strtoaddr_ex (file, &sp->addr))
276 struct sockaddr_un *unix_strtoaddr(const char *str)
278 static struct sockaddr_un add;
280 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
282 if (!unix_strtoaddr_ex (str, &add))
287 static int unix_more(COMSTACK h)
289 unix_state *sp = (unix_state *)h->cprivate;
291 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
296 * connect(2) will block (sometimes) - nothing we can do short of doing
297 * weird things like spawning subprocesses or threading or some weird junk
300 static int unix_connect(COMSTACK h, void *address)
302 struct sockaddr_un *add = (struct sockaddr_un *)address;
305 TRC(fprintf(stderr, "unix_connect\n"));
307 if (h->state != CS_ST_UNBND)
309 h->cerrno = CSOUTSTATE;
312 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
315 if (yaz_errno() == EINPROGRESS)
317 h->event = CS_CONNECT;
318 h->state = CS_ST_CONNECTING;
319 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
325 h->event = CS_CONNECT;
326 h->state = CS_ST_CONNECTING;
328 return unix_rcvconnect (h);
334 static int unix_rcvconnect(COMSTACK h)
336 TRC(fprintf(stderr, "unix_rcvconnect\n"));
338 if (h->state == CS_ST_DATAXFER)
340 if (h->state != CS_ST_CONNECTING)
342 h->cerrno = CSOUTSTATE;
346 h->state = CS_ST_DATAXFER;
350 static int unix_bind(COMSTACK h, void *address, int mode)
352 unix_state *sp = (unix_state *)h->cprivate;
353 struct sockaddr *addr = (struct sockaddr *)address;
354 const char * path = ((struct sockaddr_un *)addr)->sun_path;
355 struct stat stat_buf;
357 TRC (fprintf (stderr, "unix_bind\n"));
359 if(stat(path, &stat_buf) != -1) {
360 struct sockaddr_un socket_unix;
362 if(! S_ISSOCK(stat_buf.st_mode)) {
364 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
367 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
371 socket_unix.sun_family = AF_UNIX;
372 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
373 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
374 if(yaz_errno() == ECONNREFUSED) {
375 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
383 yaz_set_errno(EADDRINUSE);
389 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
394 chown(path, sp->uid, sp->gid);
395 chmod(path, sp->umask != -1 ? sp->umask : 0666);
396 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
401 h->state = CS_ST_IDLE;
402 h->event = CS_LISTEN;
406 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
407 int (*check_ip)(void *cd, const char *a, int len, int t),
410 struct sockaddr_un addr;
411 YAZ_SOCKLEN_T len = sizeof(addr);
413 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
414 if (h->state != CS_ST_IDLE)
416 h->cerrno = CSOUTSTATE;
419 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
423 yaz_errno() == EWOULDBLOCK
425 #if EAGAIN != EWOULDBLOCK
426 || yaz_errno() == EAGAIN
430 h->cerrno = CSNODATA;
435 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
436 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
439 h->state = CS_ST_INCON;
443 static COMSTACK unix_accept(COMSTACK h)
446 unix_state *state, *st = (unix_state *)h->cprivate;
448 TRC(fprintf(stderr, "unix_accept\n"));
449 if (h->state == CS_ST_INCON)
451 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
458 memcpy(cnew, h, sizeof(*h));
459 cnew->iofile = h->newfd;
460 cnew->io_pending = 0;
461 if (!(state = (unix_state *)
462 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
472 if (!(cnew->blocking&1) &&
473 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
488 state->altsize = state->altlen = 0;
489 state->towrite = state->written = -1;
490 state->complete = st->complete;
491 memcpy(&state->addr, &st->addr, sizeof(state->addr));
492 cnew->state = CS_ST_ACCEPT;
493 cnew->event = CS_NONE;
494 h->state = CS_ST_IDLE;
498 if (h->state == CS_ST_ACCEPT)
503 h->cerrno = CSOUTSTATE;
507 h->state = CS_ST_DATAXFER;
512 #define CS_UNIX_BUFCHUNK 4096
515 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
516 * 0=connection closed.
518 static int unix_get(COMSTACK h, char **buf, int *bufsize)
520 unix_state *sp = (unix_state *)h->cprivate;
522 int tmpi, berlen, rest, req, tomove;
523 int hasread = 0, res;
525 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
526 if (sp->altlen) /* switch buffers */
528 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
529 (unsigned) sp->altbuf));
533 *bufsize = sp->altsize;
534 hasread = sp->altlen;
540 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
544 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
547 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
548 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
550 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
551 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
554 if (yaz_errno() == EWOULDBLOCK
556 #if EAGAIN != EWOULDBLOCK
557 || yaz_errno() == EAGAIN
560 || yaz_errno() == EINPROGRESS
563 h->io_pending = CS_WANT_READ;
566 else if (yaz_errno() == 0)
575 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
577 /* move surplus buffer (or everything if we didn't get a BER rec.) */
578 if (hasread > berlen)
580 tomove = req = hasread - berlen;
581 rest = tomove % CS_UNIX_BUFCHUNK;
583 req += CS_UNIX_BUFCHUNK - rest;
586 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
588 } else if (sp->altsize < req)
589 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
591 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
592 (unsigned) sp->altbuf));
593 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
595 if (berlen < CS_UNIX_BUFCHUNK - 1)
596 *(*buf + berlen) = '\0';
597 return berlen ? berlen : 1;
604 * In nonblocking mode, you must call again with same buffer while
607 static int unix_put(COMSTACK h, char *buf, int size)
610 struct unix_state *state = (struct unix_state *)h->cprivate;
612 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
615 if (state->towrite < 0)
617 state->towrite = size;
620 else if (state->towrite != size)
622 h->cerrno = CSWRONGBUF;
625 while (state->towrite > state->written)
628 send(h->iofile, buf + state->written, size -
638 yaz_errno() == EWOULDBLOCK
640 #if EAGAIN != EWOULDBLOCK
641 || yaz_errno() == EAGAIN
646 TRC(fprintf(stderr, " Flow control stop\n"));
647 h->io_pending = CS_WANT_WRITE;
653 state->written += res;
654 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
655 res, state->written, size));
657 state->towrite = state->written = -1;
658 TRC(fprintf(stderr, " Ok\n"));
662 static int unix_close(COMSTACK h)
664 unix_state *sp = (struct unix_state *)h->cprivate;
666 TRC(fprintf(stderr, "unix_close\n"));
678 static char *unix_addrstr(COMSTACK h)
680 unix_state *sp = (struct unix_state *)h->cprivate;
682 sprintf(buf, "unix:%s", sp->addr.sun_path);
686 static int unix_set_blocking(COMSTACK p, int blocking)
690 if (p->blocking == blocking)
692 flag = fcntl(p->iofile, F_GETFL, 0);
694 flag = flag & ~O_NONBLOCK;
696 flag = flag | O_NONBLOCK;
697 if (fcntl(p->iofile, F_SETFL, flag) < 0)
699 p->blocking = blocking;