Fix leak for odr_print of ZOOM connection.
[yaz-moved-to-github.git] / src / unix.c
index 7f1ba93..5c3dce0 100644 (file)
@@ -1,9 +1,6 @@
-/*
- * Copyright (C) 1995-2005, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2009 Index Data
  * See the file LICENSE for details.
- *
- * $Id: unix.c,v 1.16 2005-10-22 13:32:04 adam Exp $
- * UNIX socket COMSTACK. By Morten Bøgeskov.
  */
 /**
  * \file unix.c
@@ -41,7 +38,7 @@
 #endif
 
 #include <yaz/unix.h>
-#include <yaz/nmem.h>
+#include <yaz/errno.h>
 
 #ifndef YAZ_SOCKLEN_T
 #define YAZ_SOCKLEN_T int
@@ -90,7 +87,7 @@ typedef struct unix_state
 
     int written;  /* -1 if we aren't writing */
     int towrite;  /* to verify against user input */
-    int (*complete)(const unsigned char *buf, int len); /* length/comple. */
+    int (*complete)(const char *buf, int len); /* length/complete. */
     struct sockaddr_un addr;  /* returned by cs_straddr */
     int uid;
     int gid;
@@ -107,7 +104,7 @@ static int unix_init (void)
  * This function is always called through the cs_create() macro.
  * s >= 0: socket has already been established for us.
  */
-COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
+COMSTACK unix_type(int s, int flags, int protocol, void *vp)
 {
     COMSTACK p;
     unix_state *state;
@@ -129,7 +126,8 @@ COMSTACK unix_type(int s, int blocking, int protocol, void *vp)
                                         xmalloc(sizeof(unix_state)))))
         return 0;
 
-    if (!((p->blocking = blocking)&1))
+    p->flags = flags;
+    if (!(p->flags&CS_FLAGS_BLOCKING))
     {
         if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
             return 0;
@@ -184,7 +182,8 @@ static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
         return 0;
     TRC(fprintf(stderr, "unix_strtoaddress: %s\n", str ? str : "NULL"));
     add->sun_family = AF_UNIX;
-    strncpy(add->sun_path, str, sizeof(add->sun_path));
+    strncpy(add->sun_path, str, sizeof(add->sun_path)-1);
+    add->sun_path[sizeof(add->sun_path)-1] = 0;
     cp = strchr (add->sun_path, ':');
     if (cp)
         *cp = '\0';
@@ -307,8 +306,7 @@ static int unix_more(COMSTACK h)
 {
     unix_state *sp = (unix_state *)h->cprivate;
 
-    return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
-                                         sp->altlen);
+    return sp->altlen && (*sp->complete)(sp->altbuf, sp->altlen);
 }
 
 /*
@@ -404,7 +402,8 @@ static int unix_bind(COMSTACK h, void *address, int mode)
             return -1;
         }
         socket_unix.sun_family = AF_UNIX;
-        strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path));
+        strncpy(socket_unix.sun_path, path, sizeof(socket_unix.sun_path)-1);
+        socket_unix.sun_path[sizeof(socket_unix.sun_path)-1] = 0;
         if(connect(socket_out, (struct sockaddr *) &socket_unix, SUN_LEN(&socket_unix)) < 0) {
             if(yaz_errno() == ECONNREFUSED) {
                 TRC (fprintf (stderr, "Socket exists but nobody is listening\n"));
@@ -426,8 +425,16 @@ static int unix_bind(COMSTACK h, void *address, int mode)
         h->cerrno = CSYSERR;
         return -1;
     }
-    chown(path, sp->uid, sp->gid);
-    chmod(path, sp->umask != -1 ? sp->umask : 0666);
+    if (chown(path, sp->uid, sp->gid))
+    {
+        h->cerrno = CSYSERR;
+        return -1;
+    }
+    if (chmod(path, sp->umask != -1 ? sp->umask : 0666))
+    {
+        h->cerrno = CSYSERR;
+        return -1;
+    }
     if (mode == CS_SERVER && listen(h->iofile, 100) < 0)
     {
         h->cerrno = CSYSERR;
@@ -504,7 +511,7 @@ static COMSTACK unix_accept(COMSTACK h)
             }
             return 0;
         }
-        if (!(cnew->blocking&1) && 
+        if (!(cnew->flags&CS_FLAGS_BLOCKING) && 
             (fcntl(cnew->iofile, F_SETFL, O_NONBLOCK) < 0)
             )
         {
@@ -572,7 +579,7 @@ static int unix_get(COMSTACK h, char **buf, int *bufsize)
         sp->altsize = tmpi;
     }
     h->io_pending = 0;
-    while (!(berlen = (*sp->complete)((unsigned char *)*buf, hasread)))
+    while (!(berlen = (*sp->complete)(*buf, hasread)))
     {
         if (!*bufsize)
         {
@@ -718,26 +725,27 @@ static char *unix_addrstr(COMSTACK h)
     return buf;
 }
 
-static int unix_set_blocking(COMSTACK p, int blocking)
+static int unix_set_blocking(COMSTACK p, int flags)
 {
     unsigned long flag;
 
-    if (p->blocking == blocking)
+    if (p->flags == flags)
         return 1;
     flag = fcntl(p->iofile, F_GETFL, 0);
-    if(!blocking)
+    if (flags & CS_FLAGS_BLOCKING)
         flag = flag & ~O_NONBLOCK;
     else
         flag = flag | O_NONBLOCK;
     if (fcntl(p->iofile, F_SETFL, flag) < 0)
         return 0;
-    p->blocking = blocking;
+    p->flags = flags;
     return 1;
 }
 #endif /* WIN32 */
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab