gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r32445 - msh/src


From: gnunet
Subject: [GNUnet-SVN] r32445 - msh/src
Date: Thu, 20 Feb 2014 14:24:12 +0100

Author: harsha
Date: 2014-02-20 14:24:12 +0100 (Thu, 20 Feb 2014)
New Revision: 32445

Added:
   msh/src/setproctitle.c
   msh/src/setproctitle.h
Modified:
   msh/src/Makefile.am
   msh/src/mshd.c
Log:
Use setproctitle() from OpenSSH to set the process names for forked daemons.


Modified: msh/src/Makefile.am
===================================================================
--- msh/src/Makefile.am 2014-02-20 13:17:05 UTC (rev 32444)
+++ msh/src/Makefile.am 2014-02-20 13:24:12 UTC (rev 32445)
@@ -5,7 +5,8 @@
 mshd_SOURCES = mshd.c mshd.h util.c util.h mtypes.h \
   common.h bitmap.c bitmap.h addressmap.c addressmap.h reduce.h reduce.c \
   server.c pmonitor.c pmonitor.h \
-  ttymodes.h ttymodes.c
+  ttymodes.h ttymodes.c \
+  setproctitle.h setproctitle.c
 mshd_LDADD = -lgnunetutil -lm
 
 msh_SOURCES = msh.c mtypes.h ttymodes.c ttymodes.h
@@ -36,4 +37,4 @@
 test_pty_LDADD = -lgnunetutil
 
 msh_waiter_SOURCES = msh_waiter.c mtypes.h ttymodes.h ttymodes.c
-msh_waiter_LDADD = -lgnunetutil
\ No newline at end of file
+msh_waiter_LDADD = -lgnunetutil

Modified: msh/src/mshd.c
===================================================================
--- msh/src/mshd.c      2014-02-20 13:17:05 UTC (rev 32444)
+++ msh/src/mshd.c      2014-02-20 13:24:12 UTC (rev 32445)
@@ -14,6 +14,7 @@
 #include "reduce.h"
 #include "pmonitor.h"
 #include "server.h"
+#include "setproctitle.h"
 
 #define LOG(kind,...)                           \
   GNUNET_log (kind, __VA_ARGS__)
@@ -552,6 +553,7 @@
   GNUNET_assert (0 == sigprocmask (SIG_SETMASK, &sigset, NULL));
   client_conn = GNUNET_CONNECTION_create_from_existing (sock);
   sock = NULL;
+  setproctitle ("mshd: worker");
   GNUNET_log_setup ("mshd-worker", NULL, NULL);
   /* cleanup child processes as we are now not their parent */
   while (NULL != (chld = chld_head))
@@ -804,6 +806,7 @@
                                         GNUNET_OS_INHERIT_STD_ALL,
                                         NULL,
                                         NULL,
+                                        NULL,
                                         run_args[0],
                                         run_args);
     if (NULL == proc)
@@ -893,7 +896,7 @@
   }
   if (0 == pid)
   {
-    
+    setproctitle ("mshd: local-serv");
     GNUNET_break (0 == sigemptyset (&sigset));
     GNUNET_assert (0 == sigprocmask (SIG_SETMASK, &sigset, NULL));
     GNUNET_DISK_file_close (write_end);
@@ -1496,6 +1499,7 @@
     LOG_ERROR ("Cannot determine our MPI rank\n");
     goto fail;
   }
+  compat_init_setproctitle (argc, argv);
   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "mshd", "mshd: MSH daemon",
                                        options, &run, NULL))
   {

Added: msh/src/setproctitle.c
===================================================================
--- msh/src/setproctitle.c                              (rev 0)
+++ msh/src/setproctitle.c      2014-02-20 13:24:12 UTC (rev 32445)
@@ -0,0 +1,117 @@
+/**
+ * @file setproctitle.c
+ * @brief BSD style setproctitle implementation
+ * @author Sree Harsha Totakura <address@hidden> 
+ */
+
+/* This file is adapted from openssh bsd-compat sources
+ * Copyright 2003 Damien Miller
+ * Copyright (c) 1983, 1995-1997 Eric P. Allman
+ * Copyright (c) 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "common.h"
+
+static char *argv_start = NULL;
+static size_t argv_env_len = 0;
+
+/**
+ * Initialize the setproctitle buffers.
+ *
+ * @param argc the argc given to main()
+ * @param argv the argv given to main()
+ */
+void
+compat_init_setproctitle (int argc, char *argv[])
+{
+  extern char **environ;
+  char *lastargv = NULL;
+  char **envp = environ;
+  int i;
+
+  /*
+   * NB: This assumes that argv has already been copied out of the
+   * way. This is true for sshd, but may not be true for other 
+   * programs. Beware.
+   */
+
+  if (argc == 0 || argv[0] == NULL)
+    return;
+
+  /* Fail if we can't allocate room for the new environment */
+  for (i = 0; envp[i] != NULL; i++)
+    ;
+  if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
+    environ = envp;    /* put it back */
+    return;
+  }
+
+  /*
+   * Find the last argv string or environment variable within 
+   * our process memory area.
+   */
+  for (i = 0; i < argc; i++) {
+    if (lastargv == NULL || lastargv + 1 == argv[i])
+      lastargv = argv[i] + strlen(argv[i]);
+  }
+  for (i = 0; envp[i] != NULL; i++) {
+    if (lastargv + 1 == envp[i])
+      lastargv = envp[i] + strlen(envp[i]);
+  }
+
+  argv[1] = NULL;
+  argv_start = argv[0];
+  argv_env_len = lastargv - argv[0] - 1;
+
+  /* 
+   * Copy environment 
+   * XXX - will truncate env on strdup fail
+   */
+  for (i = 0; envp[i] != NULL; i++)
+    environ[i] = strdup(envp[i]);
+  environ[i] = NULL;
+}
+
+
+/**
+ * Set the process's title
+ *
+ * @param ptitle the new title for the process
+ */
+void
+setproctitle(const char *ptitle)
+{
+  size_t len;
+
+  if (argv_env_len <= 0)
+    return;
+  len = snprintf(argv_start, argv_env_len, "%s", ptitle);
+  for(; len < argv_env_len - 1 ; len++)
+    argv_start[len] = '\0';
+  argv_start[len] = '\0';
+}

Added: msh/src/setproctitle.h
===================================================================
--- msh/src/setproctitle.h                              (rev 0)
+++ msh/src/setproctitle.h      2014-02-20 13:24:12 UTC (rev 32445)
@@ -0,0 +1,28 @@
+/**
+ * @file setproctitle.h
+ * @brief BSD style setproctitle implementation
+ * @author Sree Harsha Totakura <address@hidden> 
+ */
+
+#ifndef SETPROCTITLE_H_
+#define SETPROCTITLE_H_
+
+/**
+ * Initialize the setproctitle buffers.
+ *
+ * @param argc the argc given to main()
+ * @param argv the argv given to main()
+ */
+void
+compat_init_setproctitle (int argc, char *argv[]);
+
+
+/**
+ * Set the process's title to the given string
+ *
+ * @param ptitle the new title for the process
+ */
+void
+setproctitle(const char *ptitle);
+
+#endif  /* SETPROCTITLE_H_ */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]