viced.c: Don't store results of reads unecessarily
[openafs.git] / src / viced / viced.c
index fa7e3c6..cb1d426 100644 (file)
 #include <roken.h>
 
 #ifdef AFS_NT40_ENV
-#include <windows.h>
-#include <WINNT/afsevent.h>
+# include <windows.h>
+# include <WINNT/afsevent.h>
 #endif
 
 #ifdef HAVE_SYS_FILE_H
-#include <sys/file.h>
+# include <sys/file.h>
 #endif
 
 #ifdef HAVE_SYS_RESOURCE_H
-#include <sys/resource.h>
+# include <sys/resource.h>
 #endif
 
 #undef SHARED
 #include <afs/partition.h>
 #include <afs/dir.h>
 #ifndef AFS_NT40_ENV
-#include <afs/netutils.h>
+# include <afs/netutils.h>
 #endif
 #include "viced_prototypes.h"
 #include "viced.h"
 #include "host.h"
 #ifdef AFS_PTHREAD_ENV
-#include <afs/softsig.h>
+# include <afs/softsig.h>
 #endif
 #if defined(AFS_SGI_ENV)
-#include "sys/schedctl.h"
-#include "sys/lock.h"
+# include "sys/schedctl.h"
+# include "sys/lock.h"
 #endif
 #include <rx/rx_globals.h>
 
@@ -86,7 +86,6 @@ extern int etext;
 
 void *ShutDown(void *);
 static void ClearXStatValues(void);
-static void NewParms(int);
 static void PrintCounters(void);
 static void ResetCheckDescriptors(void);
 static void ResetCheckSignal(void);
@@ -105,7 +104,7 @@ char fsync_wait[1];
 #endif /* AFS_PTHREAD_ENV */
 
 #ifdef AFS_NT40_ENV
-#define NT_OPEN_MAX    1024    /* This is an arbitrary no. we came up with for
+# define NT_OPEN_MAX    1024   /* This is an arbitrary no. we came up with for
                                 * now. We hope this will be replaced by a more
                                 * intelligent estimate later. */
 #endif
@@ -214,11 +213,11 @@ static void FlagMsg(void);
  * disk
  */
 
-#if !defined(PTHREAD_RWLOCK_INITIALIZER) && defined(AFS_DARWIN80_ENV)
-#define PTHREAD_RWLOCK_INITIALIZER {0x2DA8B3B4, {0}}
-#endif
+# if !defined(PTHREAD_RWLOCK_INITIALIZER) && defined(AFS_DARWIN80_ENV)
+#  define PTHREAD_RWLOCK_INITIALIZER {0x2DA8B3B4, {0}}
+# endif
 
-#ifndef AFS_NT40_ENV
+# ifndef AFS_NT40_ENV
 struct fs_state fs_state =
     { FS_MODE_NORMAL,
       0,
@@ -229,7 +228,7 @@ struct fs_state fs_state =
       PTHREAD_COND_INITIALIZER,
       PTHREAD_RWLOCK_INITIALIZER
     };
-#else /* AFS_NT40_ENV */
+# else /* AFS_NT40_ENV */
 struct fs_state fs_state;
 
 static int fs_stateInit(void)
@@ -248,7 +247,7 @@ static int fs_stateInit(void)
     CV_INIT(&fs_state.worker_done_cv, "worker done", CV_DEFAULT, 0);
     osi_Assert(pthread_rwlock_init(&fs_state.state_lock, NULL) == 0);
 }
-#endif /* AFS_NT40_ENV */
+# endif /* AFS_NT40_ENV */
 #endif /* AFS_DEMAND_ATTACH_FS */
 
 /*
@@ -331,13 +330,11 @@ ResetCheckSignal(void)
 
 #if defined(AFS_HPUX_ENV)
     signo = SIGPOLL;
-#else
-#if defined(AFS_NT40_ENV)
+#elif defined(AFS_NT40_ENV)
     signo = SIGUSR2;
 #else
     signo = SIGXCPU;
 #endif
-#endif
 
 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
     softsig_signal(signo, CheckSignal_Signal);
@@ -350,11 +347,11 @@ static void
 ResetCheckDescriptors(void)
 {
 #ifndef AFS_NT40_ENV
-#if defined(AFS_PTHREAD_ENV)
+# if defined(AFS_PTHREAD_ENV)
     softsig_signal(SIGTERM, CheckDescriptors_Signal);
-#else
+# else
     (void)signal(SIGTERM, CheckDescriptors_Signal);
-#endif
+# endif
 #endif
 }
 
@@ -371,22 +368,22 @@ int
 viced_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
 {
     afs_uint32 rcode;
-#ifndef AFS_LINUX20_ENV
+# ifndef AFS_LINUX20_ENV
     void (*old) (int);
 
     old = (void (*)(int))signal(SIGSYS, SIG_IGN);
-#endif
+# endif
     rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
-#ifndef AFS_LINUX20_ENV
+# ifndef AFS_LINUX20_ENV
     signal(SIGSYS, old);
-#endif
+# endif
 
     return rcode;
 }
 #endif
 
 #if !defined(AFS_NT40_ENV)
-#include "AFS_component_version_number.c"
+# include "AFS_component_version_number.c"
 #endif /* !AFS_NT40_ENV */
 
 #define MAXADMINNAME 64
@@ -419,6 +416,7 @@ setThreadId(char *s)
     /* set our 'thread-id' so that the host hold table works */
     pthread_setspecific(rx_thread_id_key,
                        (void *)(intptr_t)rx_NewThreadId());
+    afs_pthread_setname_self(s);
     ViceLog(0,
            ("Set thread id %p for '%s'\n",
             pthread_getspecific(rx_thread_id_key), s));
@@ -667,15 +665,15 @@ ClearXStatValues(void)
      * declared in param.h.  If such a thing is not defined, we bitch
      * and declare ourselves to be an unknown system type.
      */
-#ifdef SYS_NAME_ID
+# ifdef SYS_NAME_ID
     afs_perfstats.sysname_ID = SYS_NAME_ID;
-#else
-#ifndef AFS_NT40_ENV
+# else
+#  ifndef AFS_NT40_ENV
     ViceLog(0, ("Sys name ID constant not defined in param.h!!\n"));
     ViceLog(0, ("[Choosing ``undefined'' sys name ID.\n"));
-#endif
+#  endif
     afs_perfstats.sysname_ID = SYS_NAME_ID_UNDEFINED;
-#endif /* SYS_NAME_ID */
+# endif /* SYS_NAME_ID */
 #endif
 
 }                              /*ClearXStatValues */
@@ -764,10 +762,9 @@ static void *
 ShutdownWatchdogLWP(void *unused)
 {
     sleep(panic_timeout);
-    ViceLog(0, ("ShutdownWatchdogLWP: Failed to shutdown and panic "
-                "within %d seconds; forcing panic\n", panic_timeout));
-    osi_Panic("ShutdownWatchdogLWP: Failed to shutdown and panic "
-             "within %d seconds; forcing panic\n", panic_timeout);
+    ViceLogThenPanic(0, ("ShutdownWatchdogLWP: Failed to shutdown and panic "
+                         "within %d seconds; forcing panic\n",
+                        panic_timeout));
     return NULL;
 }
 
@@ -815,7 +812,7 @@ ShutDownAndCore(int dopanic)
 
     strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
             localtime_r(&now, &tm));
-    ViceLog(0, ("Shutting down file server at %s", tbuffer));
+    ViceLog(0, ("Shutting down file server at %s\n", tbuffer));
     if (dopanic)
        ViceLog(0, ("ABNORMAL SHUTDOWN, see core file.\n"));
     DFlush();
@@ -1043,14 +1040,12 @@ ParseRights(char *arights)
 static int
 max_fileserver_thread(void)
 {
-#if defined(AFS_PTHREAD_ENV)
-#if defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV)
+#if defined(AFS_PTHREAD_ENV) && (defined(AFS_AIX_ENV) || defined(AFS_HPUX_ENV))
     long ans;
 
     ans = sysconf(_SC_THREAD_THREADS_MAX);
     if (0 < ans && ans < MAX_FILESERVER_THREAD)
        return (int)ans;
-#endif
 #endif /* defined(AFS_PTHREAD_ENV) */
     return MAX_FILESERVER_THREAD;
 }
@@ -1516,83 +1511,28 @@ ParseArgs(int argc, char *argv[])
 
 }                              /*ParseArgs */
 
-
-#define MAXPARMS 15
-
+/* Once upon a time, in a galaxy far far away, IBM AFS supported the use of
+ * a file /vice/file/parms, the contents of which would override any command
+ * line parameters. We no longer support the use of such a file, but we warn
+ * if we encounter its presence from an older release
+ */
 static void
-NewParms(int initializing)
+CheckParms(void)
 {
-    static struct afs_stat sbuf;
-    int i, fd;
-    char *parms;
-    char *argv[MAXPARMS];
-    int argc;
-
-    if (!(afs_stat("/vice/file/parms", &sbuf))) {
-       parms = (char *)malloc(sbuf.st_size);
-       if (!parms)
-           return;
-       fd = afs_open("parms", O_RDONLY, 0666);
-       if (fd <= 0) {
-           ViceLog(0, ("Open for parms failed with errno = %d\n", errno));
-           return;
-       }
-
-       i = read(fd, parms, sbuf.st_size);
-       close(fd);
-       if (i != sbuf.st_size) {
-           if (i < 0) {
-               ViceLog(0, ("Read on parms failed with errno = %d\n", errno));
-           } else {
-               ViceLog(0,
-                       ("Read on parms failed; expected %ld bytes but read %d\n",
-                        (long) sbuf.st_size, i));
-           }
-           free(parms);
-           return;
-       }
-
-       for (i = 0; i < MAXPARMS; argv[i++] = 0);
-
-       for (argc = i = 0; i < sbuf.st_size; i++) {
-           if ((*(parms + i) != ' ') && (*(parms + i) != '\n')) {
-               if (argv[argc] == 0)
-                   argv[argc] = (parms + i);
-           } else {
-               *(parms + i) = '\0';
-               if (argv[argc] != 0) {
-                   if (++argc == MAXPARMS)
-                       break;
-               }
-               while ((*(parms + i + 1) == ' ')
-                      || (*(parms + i + 1) == '\n'))
-                   i++;
-           }
-       }
-       if (ParseArgs(argc, argv) == 0) {
-           ViceLog(0, ("Change parameters to:"));
-       } else {
-           ViceLog(0, ("Invalid parameter in:"));
-       }
-       for (i = 0; i < argc; i++) {
-           ViceLog(0, (" %s", argv[i]));
-       }
-       ViceLog(0, ("\n"));
-       free(parms);
-    } else if (!initializing)
-       ViceLog(0,
-               ("Received request to change parms but no parms file exists\n"));
-
-}                              /*NewParms */
+    struct afs_stat sbuf;
 
+    if (afs_stat("/vice/file/parms", &sbuf) == 0) {
+       ViceLog(0, ("Using /vice/file/parms to override command line "
+                   "options is no longer supported"));
+    }
+}
 
 /* Miscellaneous routines */
 void
 Die(char *msg)
 {
-    ViceLog(0, ("%s\n", msg));
-    osi_Panic("%s\n", msg);
 
+    ViceLogThenPanic(0, ("%s\n", msg));
 }                              /*Die */
 
 
@@ -1714,7 +1654,7 @@ ReadSysIdFile(void)
                 errno));
        return EIO;
     }
-    if ((i = read(fd, (char *)&vsn, sizeof(vsn))) != sizeof(vsn)) {
+    if (read(fd, (char *)&vsn, sizeof(vsn)) != sizeof(vsn)) {
        ViceLog(0,
                ("%s: Read failed (%d)\n", AFSDIR_SERVER_SYSID_FILEPATH,
                 errno));
@@ -1732,9 +1672,8 @@ ReadSysIdFile(void)
                 AFSDIR_SERVER_SYSID_FILEPATH, vsn.version, SYSIDVERSION));
        return EIO;
     }
-    if ((i =
-        read(fd, (char *)&uuid,
-             sizeof(struct afsUUID))) != sizeof(struct afsUUID)) {
+    if (read(fd, (char *)&uuid, sizeof(struct afsUUID))
+           != sizeof(struct afsUUID)) {
        ViceLog(0,
                ("%s: read of uuid failed (%d)\n",
                 AFSDIR_SERVER_SYSID_FILEPATH, errno));
@@ -1742,9 +1681,7 @@ ReadSysIdFile(void)
     }
     afs_ntohuuid(&uuid);
     FS_HostUUID = uuid;
-    if ((i =
-        read(fd, (char *)&nentries,
-             sizeof(afs_int32))) != sizeof(afs_int32)) {
+    if (read(fd, (char *)&nentries, sizeof(afs_int32)) != sizeof(afs_int32)) {
        ViceLog(0,
                ("%s: Read of entries failed (%d)\n",
                 AFSDIR_SERVER_SYSID_FILEPATH, errno));
@@ -2001,6 +1938,9 @@ main(int argc, char *argv[])
     /* set ihandle package defaults prior to parsing args */
     ih_PkgDefaults();
 
+    /* check for the parameter file */
+    CheckParms();
+
     if (ParseArgs(argc, argv)) {
        FlagMsg();
        exit(-1);
@@ -2023,8 +1963,6 @@ main(int argc, char *argv[])
        exit(-1);
     }
 
-    NewParms(1);
-
     /* Open FileLog on stdout, stderr, fd 1 and fd2 (for perror), sigh. */
 #ifndef AFS_NT40_ENV
     serverLogSyslogTag = "fileserver";
@@ -2052,12 +1990,10 @@ main(int argc, char *argv[])
     schedctl(NDPRI, 0, NDPNORMMAX);
     if (SawLock)
        plock(PROCLOCK);
-#else
-#ifndef AFS_NT40_ENV
+#elif !defined(AFS_NT40_ENV)
     nice(-5);                  /* TODO: */
 #endif
-#endif
-    osi_Assert(DInit(buffs) == 0);
+    DInit(buffs);
 #ifdef AFS_DEMAND_ATTACH_FS
     FS_STATE_INIT;
 #endif
@@ -2385,7 +2321,7 @@ main(int argc, char *argv[])
     t = tp.tv_sec;
     strftime(tbuffer, sizeof(tbuffer), "%a %b %d %T %Y",
             localtime_r(&t, &tm));
-    ViceLog(0, ("File Server started %s", tbuffer));
+    ViceLog(0, ("File Server started %s\n", tbuffer));
 #if FS_STATS_DETAILED
     afs_FullPerfStats.det.epoch.tv_sec = StartTime = tp.tv_sec;
 #endif