chiark / gitweb /
Useful functions (u_daemon and versioncmp) moved to mLib.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 7 Jan 2007 19:02:15 +0000 (19:02 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 7 Jan 2007 19:02:15 +0000 (19:02 +0000)
Remove the code here, and use the mLib versions.

client/tripectl.c
common/util.c
server/admin.c
server/servutil.c
server/tripe.c
server/tripe.h

index 5bdc502580c33e235e8301c9e4546fea8577d8d1..b27d3fdcbf331da023c15009b7ebabf35ff0786d 100644 (file)
@@ -51,6 +51,7 @@
 #include <netdb.h>
 
 #include <mLib/alloc.h>
+#include <mLib/daemonize.h>
 #include <mLib/darray.h>
 #include <mLib/dstr.h>
 #include <mLib/mdwopt.h>
@@ -60,6 +61,7 @@
 #include <mLib/selbuf.h>
 #include <mLib/sig.h>
 #include <mLib/str.h>
+#include <mLib/versioncmp.h>
 
 #include "util.h"
 
@@ -481,7 +483,7 @@ int main(int argc, char *argv[])
        fclose(pidfp);
       closelog();
       if (f & f_daemon)
-       u_detach();
+       detachtty();
       execvp(DA(&spawnopts)[0], DA(&spawnopts));
       die(127, "couldn't exec `%s': %s", spawnpath, strerror(errno));
     }
@@ -506,7 +508,7 @@ int main(int argc, char *argv[])
   }
 
   if (f & f_daemon) {
-    if (u_daemon())
+    if (daemonize())
       die(EXIT_FAILURE, "error becoming daemon: %s", strerror(errno));
   }
   if (pidfp) {
index 4b32985c1f626c777d86e177f078261d4f9c18ef..1745655104b5e8f6dc43d71a5adee6473fb47324 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include <sys/types.h>
-#include <unistd.h>
-#include <fcntl.h>
-
 #include <mLib/dstr.h>
 
 #include "util.h"
 
-#include <sys/ioctl.h>
-
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @u_quotify@ --- *
@@ -73,51 +67,4 @@ void u_quotify(dstr *d, const char *p)
   }
 }
 
-/* --- @u_detach@ --- *
- *
- * Arguments:  ---
- *
- * Returns:    ---
- *
- * Use:                Detaches from the current terminal and ensures it can never
- *             acquire a new one.  Calls @fork@.
- */
-
-void u_detach(void)
-{
-#ifdef TIOCNOTTY
-  {
-    int fd;
-    if ((fd = open("/dev/tty", O_RDONLY)) >= 0) {
-      ioctl(fd, TIOCNOTTY);
-      close(fd);
-    }
-  }
-#endif
-  setsid();
-  if (fork() > 0)
-    _exit(0);
-}
-
-/* --- @u_daemon@ --- *
- *
- * Arguments:  ---
- *
- * Returns:    Zero if OK, nonzero on failure.
- *
- * Use:                Becomes a daemon.
- */
-
-int u_daemon(void)
-{
-  pid_t kid;
-
-  if ((kid = fork()) < 0)
-    return (-1);
-  if (kid)
-    _exit(0);
-  u_detach();
-  return (0);
-}
-
 /*----- That's all, folks -------------------------------------------------*/
index d9a75ec2c68658174fa6c01a056dc086c56754a4..d98283bcd3fcb0eb8ded9890c9b62d4e1892b795 100644 (file)
@@ -1609,7 +1609,7 @@ static void acmd_daemon(admin *a, unsigned ac, char *av[])
     a_notify("DAEMON", A_END);
     if (a_stdin)
       a_destroy(a_stdin);
-    if (u_daemon())
+    if (daemonize())
       a_fail(a, "daemon-error", "?ERRNO", A_END);
     else {
       flags |= F_DAEMON;
index 77cfd2c6c7fceba19c55c14ef60b380985be9409..92a5f775f03b456a0068317508db70294bedcb77 100644 (file)
@@ -140,157 +140,4 @@ int seq_check(seqwin *s, uint32 q, const char *service)
   return (0);
 }
 
-/* --- @versioncmp@ --- *
- *
- * Arguments:  @const char *va, *vb@ = two version strings
- *
- * Returns:    Less than, equal to, or greater than zero, according to
- *             whether @va@ is less than, equal to, or greater than @vb@.
- *
- * Use:                Compares version number strings.
- *
- *             The algorithm is an extension of the Debian version
- *             comparison algorithm.  A version number consists of three
- *             components:
- *
- *               [EPOCH :] MAIN [- SUB]
- *
- *             The MAIN part may contain colons or hyphens if there is an
- *             EPOCH or SUB, respectively.  Version strings are compared
- *             componentwise: first epochs, then main parts, and finally
- *             subparts.
- *
- *             The component comparison is done as follows.  First, the
- *             initial subsequence of nondigit characters is extracted from
- *             each string, and these are compared lexicographically, using
- *             ASCII ordering, except that letters precede non-letters.  If
- *             both are the same, an initial sequence of digits is extracted
- *             from the remaining parts of the version strings, and these
- *             are compared numerically (an empty sequence being considered
- *             to have the value zero).  This process is repeated until we
- *             have a winner or until both strings are exhausted.
- */
-
-struct vinfo {
-  const char *e, *el;
-  const char *m, *ml;
-  const char *s, *sl;
-};
-
-static int vint(const char **vv, const char *vl)
-{
-  int n = 0;
-  const char *v = *vv;
-  int ch;
-
-  while (v < vl) {
-    ch = *v;
-    if (!isdigit((unsigned char)ch))
-      break;
-    v++;
-    n = n * 10 + (ch - '0');
-  }
-  *vv = v;
-  return (n);
-}
-
-static const char *vchr(const char **vv, const char *vl)
-{
-  const char *v = *vv;
-  const char *b = v;
-  int ch;
-
-  while (v < vl) {
-    ch = *v;
-    if (isdigit((unsigned char)ch))
-      break;
-    v++;
-  }
-  *vv = v;
-  return (b);
-}
-
-#define CMP(x, y) ((x) < (y) ? -1 : +1)
-
-static int vcmp(const char *va, const char *val,
-               const char *vb, const char *vbl)
-{
-  const char *pa, *pb;
-  int ia, ib;
-
-  for (;;) {
-
-    /* --- See if we're done --- */
-
-    if (va == val && vb == vbl)
-      return (0);
-
-    /* --- Compare nondigit portions --- */
-
-    pa = vchr(&va, val); pb = vchr(&vb, vbl);
-    for (;;) {
-      if (pa == va && pb == vb)
-       break;
-      else if (pa == va)
-       return (-1);
-      else if (pb == vb)
-       return (+1);
-      else if (*pa == *pb) {
-       pa++; pb++;
-       continue;
-      } else if (isalpha((unsigned char)*pa) == isalpha((unsigned char)*pb))
-       return (CMP(*pa, *pb));
-      else if (isalpha((unsigned char)*pa))
-       return (-1);
-      else
-       return (+1);
-    }
-
-    /* --- Compare digit portions --- */
-
-    ia = vint(&va, val); ib = vint(&vb, vbl);
-    if (ia != ib)
-      return (CMP(ia, ib));
-  }
-}
-
-static void vsplit(const char *v, struct vinfo *vi)
-{
-  const char *p;
-  size_t n;
-
-  if ((p = strchr(v, ':')) == 0)
-    vi->e = vi->el = 0;
-  else {
-    vi->e = v;
-    vi->el = p;
-    v = p + 1;
-  }
-
-  n = strlen(v);
-  if ((p = strrchr(v, '-')) == 0)
-    vi->s = vi->sl = 0;
-  else {    
-    vi->s = p + 1;
-    vi->sl = v + n;
-    n = p - v;
-  }
-
-  vi->m = v;
-  vi->ml = v + n;
-}
-
-int versioncmp(const char *va, const char *vb)
-{
-  struct vinfo via, vib;
-  int rc;
-
-  vsplit(va, &via); vsplit(vb, &vib);
-  if ((rc = vcmp(via.e, via.el, vib.e, vib.el)) != 0 ||
-      (rc = vcmp(via.m, via.ml, vib.m, vib.ml)) != 0 ||
-      (rc = vcmp(via.s, via.sl, vib.s, vib.sl)) != 0)
-    return (rc);
-  return (0);
-}
-
 /*----- That's all, folks -------------------------------------------------*/
index d98a2af45bf5369839cb21e366f770f3d27de31b..f9cf0e86ecca9956ec6d8ea8644cd15055f99efb 100644 (file)
@@ -330,7 +330,7 @@ int main(int argc, char *argv[])
   km_init(kr_priv, kr_pub, tag_priv);
   a_init(csock);
   if (f & f_daemon) {
-    if (u_daemon())
+    if (daemonize())
       die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
     a_daemon();
   }
index 1d13f32d99180a444c004c1c7362baf371167497..66fb091d3e5f253a095a2184c4efebcd23cf2b99 100644 (file)
@@ -68,6 +68,7 @@
 #include <mLib/arena.h>
 #include <mLib/base64.h>
 #include <mLib/bres.h>
+#include <mLib/daemonize.h>
 #include <mLib/dstr.h>
 #include <mLib/env.h>
 #include <mLib/fdflags.h>
@@ -82,6 +83,7 @@
 #include <mLib/sub.h>
 #include <mLib/trace.h>
 #include <mLib/tv.h>
+#include <mLib/versioncmp.h>
 
 #include <catacomb/buf.h>
 
@@ -1177,39 +1179,6 @@ extern void seq_reset(seqwin */*s*/);
 
 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
 
-/* --- @versioncmp@ --- *
- *
- * Arguments:  @const char *va, *vb@ = two version strings
- *
- * Returns:    Less than, equal to, or greater than zero, according to
- *             whether @va@ is less than, equal to, or greater than @vb@.
- *
- * Use:                Compares version number strings.
- *
- *             The algorithm is an extension of the Debian version
- *             comparison algorithm.  A version number consists of three
- *             components:
- *
- *               [EPOCH :] MAIN [- SUB]
- *
- *             The MAIN part may contain colons or hyphens if there is an
- *             EPOCH or SUB, respectively.  Version strings are compared
- *             componentwise: first epochs, then main parts, and finally
- *             subparts.
- *
- *             The component comparison is done as follows.  First, the
- *             initial subsequence of nondigit characters is extracted from
- *             each string, and these are compared lexicographically, using
- *             ASCII ordering, except that letters precede non-letters.  If
- *             both are the same, an initial sequence of digits is extracted
- *             from the remaining parts of the version strings, and these
- *             are compared numerically (an empty sequence being considered
- *             to have the value zero).  This process is repeated until we
- *             have a winner or until both strings are exhausted.
- */
-
-extern int versioncmp(const char */*va*/, const char */*vb*/);
-
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus