2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: unix.c,v 1.6 2004-10-15 00:19:01 adam 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>
37 #define YAZ_SOCKLEN_T int
40 static int unix_close(COMSTACK h);
41 static int unix_put(COMSTACK h, char *buf, int size);
42 static int unix_get(COMSTACK h, char **buf, int *bufsize);
43 static int unix_connect(COMSTACK h, void *address);
44 static int unix_more(COMSTACK h);
45 static int unix_rcvconnect(COMSTACK h);
46 static int unix_bind(COMSTACK h, void *address, int mode);
47 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
48 int (*check_ip)(void *cd, const char *a, int len, int type),
50 static int unix_set_blocking(COMSTACK p, int blocking);
52 static COMSTACK unix_accept(COMSTACK h);
53 static char *unix_addrstr(COMSTACK h);
54 static void *unix_straddr(COMSTACK h, const char *str);
57 #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \
58 + strlen ((ptr)->sun_path))
66 /* this state is used for both SSL and straight TCP/IP */
67 typedef struct unix_state
69 char *altbuf; /* alternate buffer for surplus data */
70 int altsize; /* size as xmalloced */
71 int altlen; /* length of data or 0 if none */
73 int written; /* -1 if we aren't writing */
74 int towrite; /* to verify against user input */
75 int (*complete)(const unsigned char *buf, int len); /* length/comple. */
76 struct sockaddr_un addr; /* returned by cs_straddr */
80 char buf[128]; /* returned by cs_addrstr */
83 static int unix_init (void)
89 * This function is always called through the cs_create() macro.
90 * s >= 0: socket has already been established for us.
92 COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
102 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
108 if (!(p = (struct comstack *)xmalloc(sizeof(struct comstack))))
110 if (!(state = (struct unix_state *)(p->cprivate =
111 xmalloc(sizeof(unix_state)))))
114 if (!((p->blocking = blocking)&1))
116 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
119 signal (SIGPIPE, SIG_IGN);
126 p->protocol = (enum oid_proto) protocol;
128 p->f_connect = unix_connect;
129 p->f_rcvconnect = unix_rcvconnect;
132 p->f_close = unix_close;
133 p->f_more = unix_more;
134 p->f_bind = unix_bind;
135 p->f_listen = unix_listen;
136 p->f_accept = unix_accept;
137 p->f_addrstr = unix_addrstr;
138 p->f_straddr = unix_straddr;
139 p->f_set_blocking = unix_set_blocking;
141 p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
147 state->altsize = state->altlen = 0;
148 state->towrite = state->written = -1;
149 if (protocol == PROTO_WAIS)
150 state->complete = completeWAIS;
152 state->complete = cs_complete_auto;
154 p->timeout = COMSTACK_DEFAULT_TIMEOUT;
155 TRC(fprintf(stderr, "Created new UNIX comstack\n"));
161 static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
166 TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
167 add->sun_family = AF_UNIX;
168 strncpy(add->sun_path, str, sizeof(add->sun_path));
169 cp = strchr (add->sun_path, ':');
175 static void *unix_straddr(COMSTACK h, const char *str)
177 unix_state *sp = (unix_state *)h->cprivate;
178 char * s = strdup(str);
180 const char * file = NULL;
183 sp->uid = sp->gid = sp->umask = -1;
185 if ((eol = strchr(s, ',')))
189 if ((eol = strchr(s, ',')))
191 if (sp->uid == -1 && strncmp(s, "user=", 5) == 0)
194 if (strspn(arg, "0123456789") == strlen(arg))
200 struct passwd * pw = getpwnam(arg);
203 printf("No such user\n");
207 sp->uid = pw->pw_uid;
210 else if (sp->gid == -1 && strncmp(s, "group=", 6) == 0)
213 if (strspn(arg, "0123456789") == strlen(arg))
219 struct group * gr = getgrnam(arg);
222 printf("No such group\n");
226 sp->gid = gr->gr_gid;
229 else if (sp->umask == -1 && strncmp(s, "umask=", 6) == 0)
234 sp->umask = strtol(arg, &end, 8);
235 if (errno == EINVAL ||
238 printf("Invalid umask\n");
243 else if (file == NULL && strncmp(s, "file=", 5) == 0)
250 printf("invalid or double argument: %s\n", s);
266 TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
268 if (!unix_strtoaddr_ex (file, &sp->addr))
277 struct sockaddr_un *unix_strtoaddr(const char *str)
279 static struct sockaddr_un add;
281 TRC(fprintf(stderr, "unix_strtoaddr: %s\n", str ? str : "NULL"));
283 if (!unix_strtoaddr_ex (str, &add))
288 static int unix_more(COMSTACK h)
290 unix_state *sp = (unix_state *)h->cprivate;
292 return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
297 * connect(2) will block (sometimes) - nothing we can do short of doing
298 * weird things like spawning subprocesses or threading or some weird junk
301 static int unix_connect(COMSTACK h, void *address)
303 struct sockaddr_un *add = (struct sockaddr_un *)address;
306 TRC(fprintf(stderr, "unix_connect\n"));
308 if (h->state != CS_ST_UNBND)
310 h->cerrno = CSOUTSTATE;
313 r = connect(h->iofile, (struct sockaddr *) add, SUN_LEN(add));
316 if (yaz_errno() == EINPROGRESS)
318 h->event = CS_CONNECT;
319 h->state = CS_ST_CONNECTING;
320 h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
326 h->event = CS_CONNECT;
327 h->state = CS_ST_CONNECTING;
329 return unix_rcvconnect (h);
335 static int unix_rcvconnect(COMSTACK h)
337 TRC(fprintf(stderr, "unix_rcvconnect\n"));
339 if (h->state == CS_ST_DATAXFER)
341 if (h->state != CS_ST_CONNECTING)
343 h->cerrno = CSOUTSTATE;
347 h->state = CS_ST_DATAXFER;
351 static int unix_bind(COMSTACK h, void *address, int mode)
353 unix_state *sp = (unix_state *)h->cprivate;
354 struct sockaddr *addr = (struct sockaddr *)address;
355 const char * path = ((struct sockaddr_un *)addr)->sun_path;
356 struct stat stat_buf;
358 TRC (fprintf (stderr, "unix_bind\n"));
360 if(stat(path, &stat_buf) != -1) {
361 struct sockaddr_un socket_unix;
363 if(! S_ISSOCK(stat_buf.st_mode)) {
365 yaz_set_errno(EEXIST); /* Not a socket (File exists) */
368 if((socket_out = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
372 socket_unix.sun_family = AF_UNIX;
373 strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
374 if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
375 if(yaz_errno() == ECONNREFUSED) {
376 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
384 yaz_set_errno(EADDRINUSE);
390 if (bind(h->iofile, (struct sockaddr *) addr, SUN_LEN((struct sockaddr_un *)addr)))
395 chown(path, sp->uid, sp->gid);
396 chmod(path, sp->umask != -1 ? sp->umask : 0666);
397 if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
402 h->state = CS_ST_IDLE;
403 h->event = CS_LISTEN;
407 static int unix_listen(COMSTACK h, char *raddr, int *addrlen,
408 int (*check_ip)(void *cd, const char *a, int len, int t),
411 struct sockaddr_un addr;
412 YAZ_SOCKLEN_T len = sizeof(addr);
414 TRC(fprintf(stderr, "unix_listen pid=%d\n", getpid()));
415 if (h->state != CS_ST_IDLE)
417 h->cerrno = CSOUTSTATE;
420 h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
424 yaz_errno() == EWOULDBLOCK
426 #if EAGAIN != EWOULDBLOCK
427 || yaz_errno() == EAGAIN
431 h->cerrno = CSNODATA;
436 if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_un))
437 memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_un));
440 h->state = CS_ST_INCON;
444 static COMSTACK unix_accept(COMSTACK h)
447 unix_state *state, *st = (unix_state *)h->cprivate;
449 TRC(fprintf(stderr, "unix_accept\n"));
450 if (h->state == CS_ST_INCON)
452 if (!(cnew = (COMSTACK)xmalloc(sizeof(*cnew))))
459 memcpy(cnew, h, sizeof(*h));
460 cnew->iofile = h->newfd;
461 cnew->io_pending = 0;
462 if (!(state = (unix_state *)
463 (cnew->cprivate = xmalloc(sizeof(unix_state)))))
473 if (!(cnew->blocking&1) &&
474 (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
489 state->altsize = state->altlen = 0;
490 state->towrite = state->written = -1;
491 state->complete = st->complete;
492 memcpy(&state->addr, &st->addr, sizeof(state->addr));
493 cnew->state = CS_ST_ACCEPT;
494 cnew->event = CS_NONE;
495 h->state = CS_ST_IDLE;
499 if (h->state == CS_ST_ACCEPT)
504 h->cerrno = CSOUTSTATE;
508 h->state = CS_ST_DATAXFER;
513 #define CS_UNIX_BUFCHUNK 4096
516 * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
517 * 0=connection closed.
519 static int unix_get(COMSTACK h, char **buf, int *bufsize)
521 unix_state *sp = (unix_state *)h->cprivate;
523 int tmpi, berlen, rest, req, tomove;
524 int hasread = 0, res;
526 TRC(fprintf(stderr, "unix_get: bufsize=%d\n", *bufsize));
527 if (sp->altlen) /* switch buffers */
529 TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
530 (unsigned) sp->altbuf));
534 *bufsize = sp->altsize;
535 hasread = sp->altlen;
541 while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
545 if (!(*buf = (char *)xmalloc(*bufsize = CS_UNIX_BUFCHUNK)))
548 else if (*bufsize - hasread < CS_UNIX_BUFCHUNK)
549 if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
551 res = recv(h->iofile, *buf + hasread, CS_UNIX_BUFCHUNK, 0);
552 TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
555 if (yaz_errno() == EWOULDBLOCK
557 #if EAGAIN != EWOULDBLOCK
558 || yaz_errno() == EAGAIN
561 || yaz_errno() == EINPROGRESS
564 h->io_pending = CS_WANT_READ;
567 else if (yaz_errno() == 0)
576 TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
578 /* move surplus buffer (or everything if we didn't get a BER rec.) */
579 if (hasread > berlen)
581 tomove = req = hasread - berlen;
582 rest = tomove % CS_UNIX_BUFCHUNK;
584 req += CS_UNIX_BUFCHUNK - rest;
587 if (!(sp->altbuf = (char *)xmalloc(sp->altsize = req)))
589 } else if (sp->altsize < req)
590 if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
592 TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
593 (unsigned) sp->altbuf));
594 memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
596 if (berlen < CS_UNIX_BUFCHUNK - 1)
597 *(*buf + berlen) = '\0';
598 return berlen ? berlen : 1;
605 * In nonblocking mode, you must call again with same buffer while
608 static int unix_put(COMSTACK h, char *buf, int size)
611 struct unix_state *state = (struct unix_state *)h->cprivate;
613 TRC(fprintf(stderr, "unix_put: size=%d\n", size));
616 if (state->towrite < 0)
618 state->towrite = size;
621 else if (state->towrite != size)
623 h->cerrno = CSWRONGBUF;
626 while (state->towrite > state->written)
629 send(h->iofile, buf + state->written, size -
639 yaz_errno() == EWOULDBLOCK
641 #if EAGAIN != EWOULDBLOCK
642 || yaz_errno() == EAGAIN
647 TRC(fprintf(stderr, " Flow control stop\n"));
648 h->io_pending = CS_WANT_WRITE;
654 state->written += res;
655 TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
656 res, state->written, size));
658 state->towrite = state->written = -1;
659 TRC(fprintf(stderr, " Ok\n"));
663 static int unix_close(COMSTACK h)
665 unix_state *sp = (struct unix_state *)h->cprivate;
667 TRC(fprintf(stderr, "unix_close\n"));
679 static char *unix_addrstr(COMSTACK h)
681 unix_state *sp = (struct unix_state *)h->cprivate;
683 sprintf(buf, "unix:%s", sp->addr.sun_path);
687 static int unix_set_blocking(COMSTACK p, int blocking)
691 if (p->blocking == blocking)
693 flag = fcntl(p->iofile, F_GETFL, 0);
695 flag = flag & ~O_NONBLOCK;
697 flag = flag | O_NONBLOCK;
698 if (fcntl(p->iofile, F_SETFL, flag) < 0)
700 p->blocking = blocking;