afsd: print syscalls on separate lines with afsd -debug
[openafs.git] / src / afsd / afsd_kernel.c
index f4d85b2..cc4c02b 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
+
+#ifdef IGNORE_SOME_GCC_WARNINGS
+# ifdef __clang__
+#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+# else
+#  pragma GCC diagnostic warning "-Wdeprecated-declarations"
+# endif
+#endif
+
 #define VFS 1
 
 #include <afs/cmd.h>
 #include "afsd.h"
 
 #include <assert.h>
-#include <potpourri.h>
 #include <afs/afsutil.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <signal.h>
-#include <string.h>
-#include <stdlib.h>
-#include <time.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/file.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <dirent.h>
 #include <sys/wait.h>
 
-
-#ifdef HAVE_SYS_PARAM_H
-#include <sys/param.h>
+#if defined(AFS_LINUX20_ENV)
+#include <sys/resource.h>
 #endif
 
 #ifdef HAVE_SYS_FS_TYPES_H
 #include <sys/fstyp.h>
 #endif
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
+#include <ctype.h>
 
-#include <netinet/in.h>
+#include <afs/opr.h>
 #include <afs/afs_args.h>
 #include <afs/cellconfig.h>
-#include <ctype.h>
 #include <afs/afssyscalls.h>
 #include <afs/afsutil.h>
 
 #ifdef AFS_DARWIN_ENV
 #ifdef AFS_DARWIN80_ENV
-#include <sys/ioctl.h>
 #include <sys/xattr.h>
 #endif
 #include <mach/mach.h>
@@ -112,6 +101,7 @@ kern_return_t DiskArbDiskAppearedWithMountpointPing_auto(char *, unsigned int,
 #endif /* MOUNT_AFS */
 
 #ifdef AFS_SGI65_ENV
+# include <sched.h>
 # define SET_RTPRI(P) {  \
     struct sched_param sp; \
     sp.sched_priority = P; \
@@ -146,127 +136,156 @@ afsd_set_afsd_rtpri(void)
     SET_AFSD_RTPRI();
 }
 
-#if !defined(AFS_SGI_ENV) && !defined(AFS_AIX32_ENV)
-
+#if defined(AFS_LINUX20_ENV)
 int
-afsd_call_syscall(long param1, long param2, long param3, long param4, long param5,
-            long param6, long  param7)
+os_syscall(struct afsd_syscall_args *args)
 {
     int error;
-# ifdef AFS_LINUX20_ENV
-    long eparm[4];
     struct afsprocdata syscall_data;
+
     int fd = open(PROC_SYSCALL_FNAME, O_RDWR);
     if (fd < 0)
        fd = open(PROC_SYSCALL_ARLA_FNAME, O_RDWR);
-    eparm[0] = param4;
-    eparm[1] = param5;
-    eparm[2] = param6;
-    eparm[3] = param7;
 
-    param4 = (long)eparm;
+    if (fd < 0)
+       return -1;
 
     syscall_data.syscall = AFSCALL_CALL;
-    syscall_data.param1 = param1;
-    syscall_data.param2 = param2;
-    syscall_data.param3 = param3;
-    syscall_data.param4 = param4;
-    if (fd > 0) {
-       error = ioctl(fd, VIOC_SYSCALL, &syscall_data);
-       close(fd);
-    } else
-# endif /* AFS_LINUX20_ENV */
-# ifdef AFS_DARWIN80_ENV
-    struct afssysargs syscall_data;
-    void *ioctldata;
-    int fd = open(SYSCALL_DEV_FNAME,O_RDWR);
-    int syscallnum;
-#ifdef AFS_DARWIN100_ENV
-    int is64 = 0;
-    struct afssysargs64 syscall64_data;
-    if (sizeof(param1) == 8) {
-       syscallnum = VIOC_SYSCALL64;
-       is64 = 1;
-       ioctldata = &syscall64_data;
-       syscall64_data.syscall = (int)AFSCALL_CALL;
-       syscall64_data.param1 = param1;
-       syscall64_data.param2 = param2;
-       syscall64_data.param3 = param3;
-       syscall64_data.param4 = param4;
-       syscall64_data.param5 = param5;
-       syscall64_data.param6 = param6;
-    } else {
-#endif
-       syscallnum = VIOC_SYSCALL;
-        ioctldata = &syscall_data;
-       syscall_data.syscall = AFSCALL_CALL;
-       syscall_data.param1 = param1;
-       syscall_data.param2 = param2;
-       syscall_data.param3 = param3;
-       syscall_data.param4 = param4;
-       syscall_data.param5 = param5;
-       syscall_data.param6 = param6;
-#ifdef AFS_DARWIN100_ENV
-    }
-#endif
-    if(fd >= 0) {
-        error = ioctl(fd, syscallnum, ioctldata);
-        close(fd);
-    } else {
-        error = -1;
-    }
-    if (!error) {
-#ifdef AFS_DARWIN100_ENV
-        if (is64)
-            error=syscall64_data.retval;
-        else
-#endif
-            error=syscall_data.retval;
-    }
-# else /* AFS_DARWIN80_ENV */
-    error =
-       syscall(AFS_SYSCALL, AFSCALL_CALL, param1, param2, param3, param4,
-               param5, param6, param7);
-# endif /* !AFS_DARWIN80_ENV */
+    syscall_data.param1 = args->syscall;
+    syscall_data.param2 = args->params[0];
+    syscall_data.param3 = args->params[1];
+    syscall_data.param4 = (long) &args->params[2];
 
-    if (afsd_debug) {
-#ifdef AFS_NBSD40_ENV
-        char *s = strerror(errno);
-        printf("SScall(%d, %d, %d)=%d (%d, %s)\n", AFS_SYSCALL, AFSCALL_CALL,
-                param1, error, errno, s);
-#else
-       printf("SScall(%d, %d, %ld)=%d ", AFS_SYSCALL, AFSCALL_CALL, param1,
-              error);
-#endif
-    }
+    error = ioctl(fd, VIOC_SYSCALL, &syscall_data);
+    close(fd);
 
-    return (error);
+    return error;
 }
-#else /* !AFS_SGI_ENV && !AFS_AIX32_ENV */
-# if defined(AFS_SGI_ENV)
-int
-afsd_call_syscall(call, parm0, parm1, parm2, parm3, parm4)
+#elif defined(AFS_DARWIN80_ENV)
+
+# if defined(AFS_DARWIN100_ENV)
+static int
+os_syscall64(struct afsd_syscall_args *args)
 {
+    int error;
+    struct afssysargs64 syscall64_data;
+    int fd = open(SYSCALL_DEV_FNAME, O_RDWR);
+
+    if (fd < 0)
+       return -1;
+
+    syscall64_data.syscall = (int)AFSCALL_CALL;
+    syscall64_data.param1 = args->syscall;
+    syscall64_data.param2 = args->params[0];
+    syscall64_data.param3 = args->params[1];
+    syscall64_data.param4 = args->params[2];
+    syscall64_data.param5 = args->params[3];
+    syscall64_data.param6 = args->params[4];
+
+    error = ioctl(fd, VIOC_SYSCALL64, &syscall64_data);
+    close(fd);
+
+    if (error)
+       return error;
 
+    return syscall64_data.retval;
+}
+# endif
+
+static int
+os_syscall(struct afsd_syscall_args *args)
+{
     int error;
+    struct afssysargs syscall_data;
+    int fd;
 
-    error = afs_syscall(call, parm0, parm1, parm2, parm3, parm4);
-    if (afsd_verbose)
-       printf("SScall(%d, %d)=%d ", call, parm0, error);
+# ifdef AFS_DARWIN100_ENV
+    if (sizeof(long) == 8)
+       return os_syscall64(args);
+# endif
 
-    return error;
+    fd = open(SYSCALL_DEV_FNAME, O_RDWR);
+    if (fd < 0)
+       return -1;
+
+    syscall_data.syscall = AFSCALL_CALL;
+    syscall_data.param1 = (unsigned int)(uintptr_t)args->syscall;
+    syscall_data.param2 = (unsigned int)(uintptr_t)args->params[0];
+    syscall_data.param3 = (unsigned int)(uintptr_t)args->params[1];
+    syscall_data.param4 = (unsigned int)(uintptr_t)args->params[2];
+    syscall_data.param5 = (unsigned int)(uintptr_t)args->params[3];
+    syscall_data.param6 = (unsigned int)(uintptr_t)args->params[4];
+
+    error = ioctl(fd, VIOC_SYSCALL, syscall_data);
+    close(fd);
+
+    if (error)
+       return error;
+
+    return syscall_data.retval;
 }
-# else /* AFS_SGI_ENV */
-int
-afsd_call_syscall(call, parm0, parm1, parm2, parm3, parm4, parm5, parm6)
+
+#elif defined(AFS_SUN511_ENV)
+static int
+os_syscall(struct afsd_syscall_args *args)
 {
+    int retval, error;
 
-    return syscall(AFSCALL_CALL, call, parm0, parm1, parm2, parm3, parm4,
-                  parm5, parm6);
+    error = ioctl_sun_afs_syscall(AFSCALL_CALL, args->syscall,
+                                args->params[0], args->params[1],
+                                args->params[2], args->params[3],
+                                args->params[4], &retval);
+    if (error)
+       return error;
+
+    return retval;
+}
+#elif defined(AFS_SGI_ENV)
+static int
+os_syscall(struct afsd_syscall_args *args)
+{
+    return afs_syscall(args->syscall, args->params[0], args->params[1],
+                      args->params[2], args->params[3], args->params[4]);
+}
+#elif defined(AFS_AIX32_ENV)
+static int
+os_syscall(struct afsd_syscall_args *args)
+{
+    return syscall(AFSCALL_CALL, args->syscall,
+                  args->params[0], args->params[1], args->params[2],
+                  args->params[3], args->params[4], args->params[5],
+                  args->params[6]);
+}
+#else
+static int
+os_syscall(struct afsd_syscall_args *args)
+{
+    return syscall(AFS_SYSCALL, AFSCALL_CALL, args->syscall,
+                  args->params[0], args->params[1], args->params[2],
+                  args->params[3], args->params[4], args->params[5]);
 }
-# endif /* !AFS_SGI_ENV */
-#endif /* AFS_SGI_ENV || AFS_AIX32_ENV */
+#endif
+
+int
+afsd_call_syscall(struct afsd_syscall_args *args)
+{
+    int error;
+
+    error = os_syscall(args);
+
+    if (afsd_debug) {
+#ifdef AFS_NBSD40_ENV
+        char *s = strerror(errno);
+        printf("SScall(%d, %d, %d)=%d (%d, %s)\n", AFS_SYSCALL, AFSCALL_CALL,
+                (int)args->params[0], error, errno, s);
+#else
+       printf("SScall(%d, %d, %d)=%d\n", AFS_SYSCALL, AFSCALL_CALL,
+              (int)args->params[0], error);
+#endif
+    }
 
+    return error;
+}
 
 #ifdef AFS_AIX_ENV
 /* Special handling for AIX's afs mount operation since they require much more
@@ -275,21 +294,18 @@ afsd_call_syscall(call, parm0, parm1, parm2, parm3, parm4, parm5, parm6)
 
 #define        ROUNDUP(x)  (((x) + 3) & ~3)
 
-aix_vmount()
+aix_vmount(const char *cacheMountDir)
 {
     struct vmount *vmountp;
     int size, error;
 
     size = sizeof(struct vmount) + ROUNDUP(strlen(cacheMountDir) + 1) + 5 * 4;
-    /* Malloc the vmount structure */
-    if ((vmountp = (struct vmount *)malloc(size)) == (struct vmount *)NULL) {
+    /* Malloc and zero the vmount structure */
+    if ((vmountp = calloc(1, size)) == NULL) {
        printf("Can't allocate space for the vmount structure (AIX)\n");
        exit(1);
     }
 
-    /* zero out the vmount structure */
-    memset(vmountp, '\0', size);
-
     /* transfer info into the vmount structure */
     vmountp->vmt_revision = VMT_REVISION;
     vmountp->vmt_length = size;
@@ -363,49 +379,49 @@ vmountdata(struct vmount * vmtp, char *obj, char *stub, char *host,
 #ifdef AFS_HPUX_ENV
 #define        MOUNTED_TABLE   MNT_MNTTAB
 #else
-#ifdef AFS_SUN5_ENV
-#define        MOUNTED_TABLE   MNTTAB
-#else
 #define        MOUNTED_TABLE   MOUNTED
 #endif
-#endif
 
 static int
 HandleMTab(char *cacheMountDir)
 {
-#if (defined (AFS_SUN_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)) && !defined(AFS_SUN58_ENV)
+#if (defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV))
     FILE *tfilep;
-#ifdef AFS_SUN5_ENV
-    char tbuf[16];
-    struct mnttab tmntent;
-
-    memset(&tmntent, '\0', sizeof(struct mnttab));
-    if (!(tfilep = fopen(MOUNTED_TABLE, "a+"))) {
-       printf("Can't open %s\n", MOUNTED_TABLE);
-       perror(MNTTAB);
-       exit(-1);
-    }
-    tmntent.mnt_special = "AFS";
-    tmntent.mnt_mountp = cacheMountDir;
-    tmntent.mnt_fstype = "xx";
-    tmntent.mnt_mntopts = "rw";
-    sprintf(tbuf, "%ld", (long)time((time_t *) 0));
-    tmntent.mnt_time = tbuf;
-    putmntent(tfilep, &tmntent);
-    fclose(tfilep);
-#else
 #if defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
     struct mntent tmntent;
+    char *dir;
+    int i;
 
     tfilep = setmntent("/etc/mtab", "a+");
+    if (!tfilep) {
+       printf("Can't open /etc/mtab for writing (errno %d); not adding "
+              "an entry for AFS\n", errno);
+       return 1;
+    }
+
+    dir = strdup(cacheMountDir);
+
+    /* trim trailing slashes; don't look at dir[0] in case we are somehow
+     * just "/" */
+    for (i = strlen(dir)-1; i > 0; i--) {
+       if (dir[i] == '/') {
+           dir[i] = '\0';
+       } else {
+           break;
+       }
+    }
+
     tmntent.mnt_fsname = "AFS";
-    tmntent.mnt_dir = cacheMountDir;
+    tmntent.mnt_dir = dir;
     tmntent.mnt_type = "afs";
     tmntent.mnt_opts = "rw";
     tmntent.mnt_freq = 1;
     tmntent.mnt_passno = 3;
     addmntent(tfilep, &tmntent);
     endmntent(tfilep);
+
+    free(dir);
+    dir = NULL;
 #else
     struct mntent tmntent;
 
@@ -430,7 +446,6 @@ HandleMTab(char *cacheMountDir)
     addmntent(tfilep, &tmntent);
     endmntent(tfilep);
 #endif /* AFS_SGI_ENV */
-#endif /* AFS_SUN5_ENV */
 #endif /* unreasonable systems */
 #ifdef AFS_DARWIN_ENV
 #ifndef AFS_DARWIN100_ENV
@@ -474,12 +489,12 @@ afsd_mount_afs(const char *rn, const char *cacheMountDir)
        printf("%s: Mounting the AFS root on '%s', flags: %d.\n", rn,
            cacheMountDir, mountFlags);
 #if defined(AFS_FBSD60_ENV)
-    /* data must be non-NULL but is otherwise ignored */
-    if ((mount(MOUNT_AFS, cacheMountDir, mountFlags, rn)) < 0) {
+    /* data must be non-const non-NULL but is otherwise ignored */
+    if ((mount(MOUNT_AFS, cacheMountDir, mountFlags, &mountFlags)) < 0) {
 #elif defined(AFS_FBSD_ENV)
     if ((mount("AFS", cacheMountDir, mountFlags, (caddr_t) 0)) < 0) {
 #elif defined(AFS_AIX_ENV)
-    if (aix_vmount()) {
+    if (aix_vmount(cacheMountDir)) {
 #elif defined(AFS_HPUX100_ENV)
     if ((mount("", cacheMountDir, mountFlags, "afs", NULL, 0)) < 0) {
 #elif defined(AFS_SUN5_ENV)
@@ -517,7 +532,7 @@ afsd_fork(int wait, afsd_callback_func cb, void *rock)
     } else {
        assert(code > 0);
        if (wait) {
-           assert(waitpid(code, NULL, 0) != -1);
+           opr_Verify(waitpid(code, NULL, 0) != -1);
        }
     }
     return 0;
@@ -530,6 +545,24 @@ afsd_daemon(int nochdir, int noclose)
 }
 
 int
+afsd_check_mount(const char *rn, const char *mountdir)
+{
+    struct stat statbuf;
+
+    if (stat(mountdir, &statbuf)) {
+       printf("%s: Mountpoint %s missing.\n", rn, mountdir);
+       return -1;
+    } else if (!S_ISDIR(statbuf.st_mode)) {
+       printf("%s: Mountpoint %s is not a directory.\n", rn, mountdir);
+       return -1;
+    } else if (mountdir[0] != '/') {
+       printf("%s: Mountpoint %s is not an absolute path.\n", rn, mountdir);
+       return -1;
+    }
+    return 0;
+}
+
+int
 main(int argc, char **argv)
 {
     int code;
@@ -537,7 +570,10 @@ main(int argc, char **argv)
     afsd_init();
 
     code = afsd_parse(argc, argv);
-    if (code) {
+    if (code == CMD_HELP) {
+       return 0; /* Displaying help is not an error. */
+    }
+    if (code != 0) {
        return -1;
     }