Fixed bug 655: IP reverse map no longer works. Renamed blocking element
[yaz-moved-to-github.git] / src / unix.c
index 71f9d4f..77cea3c 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2005, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: unix.c,v 1.15 2005-06-25 15:46:06 adam Exp $
+ * $Id: unix.c,v 1.17 2006-09-06 15:01:53 adam Exp $
  * UNIX socket COMSTACK. By Morten Bøgeskov.
  */
 /**
 #define YAZ_SOCKLEN_T int
 #endif
 
+/* stat(2) masks: S_IFMT and S_IFSOCK may not be defined in gcc -ansi mode */
+#if __STRICT_ANSI__
+#ifndef S_IFSOCK
+#define S_IFMT   0170000
+#define S_IFSOCK 0140000
+#endif
+#endif
+
 static int unix_close(COMSTACK h);
 static int unix_put(COMSTACK h, char *buf, int size);
 static int unix_get(COMSTACK h, char **buf, int *bufsize);
@@ -99,7 +107,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;
@@ -121,7 +129,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;
@@ -183,11 +192,10 @@ static int unix_strtoaddr_ex(const char *str, struct sockaddr_un *add)
     return 1;
 }
 
-static void *unix_straddr(COMSTACK h, const char *str)
+static void *unix_straddr1(COMSTACK h, const char *str, char *f)
 {
     unix_state *sp = (unix_state *)h->cprivate;
-    char * s = strdup(str);
-    char * f = s;
+    char * s = f;
     const char * file = NULL;
     char * eol;
 
@@ -212,7 +220,6 @@ static void *unix_straddr(COMSTACK h, const char *str)
                     if(pw == NULL)
                     {
                         printf("No such user\n");
-                        free(f);
                         return 0;
                     }
                     sp->uid = pw->pw_uid;
@@ -231,7 +238,6 @@ static void *unix_straddr(COMSTACK h, const char *str)
                     if (gr == NULL)
                     {
                         printf("No such group\n");
-                        free(f);
                         return 0;
                     }
                     sp->gid = gr->gr_gid;
@@ -247,7 +253,6 @@ static void *unix_straddr(COMSTACK h, const char *str)
                     *end)
                 {
                     printf("Invalid umask\n");
-                    free(f);
                     return 0;
                 }
             }
@@ -259,7 +264,6 @@ static void *unix_straddr(COMSTACK h, const char *str)
             else
             {
                 printf("invalid or double argument: %s\n", s);
-                free(f);
                 return 0;
             }
         } while((s = eol));
@@ -277,14 +281,18 @@ static void *unix_straddr(COMSTACK h, const char *str)
     TRC(fprintf(stderr, "unix_straddr: %s\n", str ? str : "NULL"));
 
     if (!unix_strtoaddr_ex (file, &sp->addr))
-    {
-        free(f);
         return 0;
-    }
-    free(f);
     return &sp->addr;
 }
 
+static void *unix_straddr(COMSTACK h, const char *str)
+{
+    char *f = xstrdup(str);
+    void *vp = unix_straddr1(h, str, f);
+    xfree(f);
+    return vp;
+}
+
 struct sockaddr_un *unix_strtoaddr(const char *str)
 {
     static struct sockaddr_un add;
@@ -386,7 +394,8 @@ static int unix_bind(COMSTACK h, void *address, int mode)
     if(stat(path, &stat_buf) != -1) {
         struct sockaddr_un socket_unix;
         int socket_out = -1;
-        if(! S_ISSOCK(stat_buf.st_mode)) {
+
+        if((stat_buf.st_mode&S_IFMT) != S_IFSOCK) { /* used to be S_ISSOCK */
             h->cerrno = CSYSERR;
             yaz_set_errno(EEXIST); /* Not a socket (File exists) */
             return -1;
@@ -496,7 +505,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)
             )
         {
@@ -710,20 +719,20 @@ 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 */