]> granicus.if.org Git - psmisc/commitdiff
Patches added for hurd
authorCraig Small <csmall@users.sourceforge.net>
Sun, 18 Aug 2002 08:19:15 +0000 (08:19 +0000)
committerCraig Small <csmall@users.sourceforge.net>
Sun, 18 Aug 2002 08:19:15 +0000 (08:19 +0000)
ChangeLog
src/fuser.c
src/killall.c
src/pstree.c

index ab1aa1bb2670301d55738b409deaf59752c31424..fc3ce3832711828a92bbbf5964fd7f3eaa67d8ad 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 Changes in 21.1
 ===============
+2002-08-18
+       * Added patches from James Morrison so it works on Hurd
+
 2002-08-17
        * Now has LFS support. Closes Debian #129157 and #156017
        * Fixed pstree -a extra bracket problem, Closes Debian #96791
index d530c57154a67e1b0d92b1384797d515036d1f26..c6998a01e67df25bc513a4e97adb7735e6fc5f77 100644 (file)
@@ -2,6 +2,10 @@
 
 /* Copyright 1993-1999 Werner Almesberger. See file COPYING for details. */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 #include <sys/stat.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#ifdef __linux__
 #include <linux/kdev_t.h>      /* for MKDEV */
 #include <linux/major.h>       /* for LOOP_MAJOR */
+#endif
 #include <libintl.h>
 #include <locale.h>
 #define _(String) gettext (String)
 #define LOOP_MAJOR 7
 #endif
 
+#ifndef MAJOR
+#define MAJOR(arg) 6           /* something that doesn't = LOOP_MAJOR */
+#endif
+
+#ifndef MKDEV
+#define MKDEV(arg1, arg2) mknod("/dev/Isuck", arg1, arg2) /* this is wrong */
+#endif
+
 
 #define REF_FILE   1           /* an open file */
 #define REF_ROOT   2           /* current root */
@@ -207,9 +221,11 @@ parse_net_file (SPACE_DSC * dsc,char *filename, NET_CACHE **lastptr,int version
 static void
 fill_net_cache (SPACE_DSC * dsc)
 {
-  char buffer[PATH_MAX + 1];
   NET_CACHE *last;
   struct stat statbuf;
+  char *buffer = malloc (strlen (PROC_BASE) + strlen (dsc->name) + 8);
+  if (!buffer)
+    return;
 
   if (dsc->once)
     return;
@@ -249,7 +265,7 @@ fill_unix_cache (void)
   FILE *file;
   UNIX_CACHE *new, *last;
   struct stat st;
-  char path[PATH_MAX + 1], line[MAX_LINE + 1];
+  char *path = NULL, line[MAX_LINE + 1];
   int ino;
 
   if (once)
@@ -264,10 +280,14 @@ fill_unix_cache (void)
   (void) fgets (line, MAX_LINE, file);
   while (fgets (line, MAX_LINE, file))
     {
-      if (sscanf (line, "%*x: %*x %*x %*x %*x %*x %d %s", &ino, path) != 2)
+      if (sscanf (line, "%*x: %*x %*x %*x %*x %*x %d %as", &ino, &path) != 2)
        continue;
-      if (stat (path, &st) < 0)
+      if (stat (path, &st) < 0) {
+       free (path);
        continue;
+      }
+      free (path);
+
       new = malloc (sizeof (UNIX_CACHE));
       new->fs_dev = st.st_dev;
       new->fs_ino = st.st_ino;
@@ -418,15 +438,16 @@ check_dir (const char *rel, pid_t pid, int type)
 {
   DIR *dir;
   struct dirent *de;
-  char path[PATH_MAX + 1];
+  char *path;
 
   if (!(dir = opendir (rel)))
     return;
   while ((de = readdir (dir)) != NULL)
     if (strcmp (de->d_name, ".") && strcmp (de->d_name, ".."))
       {
-       sprintf (path, "%s/%s", rel, de->d_name);
+       asprintf (&path, "%s/%s", rel, de->d_name);
        check_link (path, pid, type);
+       free (path);
       }
   (void) closedir (dir);
 }
@@ -437,7 +458,7 @@ scan_fd (void)
 {
   DIR *dir;
   struct dirent *de;
-  char path[PATH_MAX + 1];
+  char *path;
   pid_t pid;
   int empty;
 
@@ -451,7 +472,8 @@ scan_fd (void)
     if ((pid = atoi (de->d_name)) != 0)
       {
        empty = 0;
-       sprintf (path, "%s/%d", PROC_BASE, pid);
+       if (asprintf (&path, "%s/%d", PROC_BASE, pid) < 0)
+         continue;
        if (chdir (path) >= 0)
          {
            check_link ("root", pid, REF_ROOT);
@@ -462,6 +484,7 @@ scan_fd (void)
            check_map ("maps", pid, REF_MMAP);
            check_dir ("fd", pid, REF_FILE);
          }
+       free (path);
       }
   (void) closedir (dir);
   if (empty)
@@ -477,18 +500,21 @@ scan_mounts (void)
 {
   FILE *file;
   struct stat st_dev, st_parent, st_mounted;
-  char line[MAX_LINE + 1], path[PATH_MAX + 1], mounted[PATH_MAX + 3];
+  char line[MAX_LINE + 1], *path = NULL, *mounted = NULL;
   char *end;
 
   if (!(file = fopen (PROC_BASE "/mounts", "r")))
     return;                    /* old kernel */
   while (fgets (line, MAX_LINE, file))
     {
-      if (sscanf (line, "%s %s", path, mounted) != 2)
+      if (sscanf (line, "%as %as", &path, &mounted) != 2)
        continue;
       /* new kernel :-) */
-      if (stat (path, &st_dev) < 0)
-       continue;               /* might be NFS or such */
+      if (stat (path, &st_dev) < 0) {
+       free (path);            /* might be NFS or such */
+       free (mounted);
+       continue;               
+      }
       if (S_ISBLK (st_dev.st_mode) && MAJOR (st_dev.st_rdev) == LOOP_MAJOR)
        {
           struct loop_info loopinfo;
@@ -504,16 +530,25 @@ scan_mounts (void)
       if (stat (mounted, &st_mounted) < 0)
        {
          perror (mounted);
+         free (path);
+         free (mounted);
+         continue;
+       }
+      if (asprintf (&end, "%s/..", mounted) < 0)
+       {
+         free (path);
+         free (mounted);
          continue;
        }
-      end = strchr (mounted, 0);
-      strcpy (end, "/..");
-      if (stat (mounted, &st_parent) >= 0)
+
+      if (stat (end, &st_parent) >= 0)
        {
-         *end = 0;
          add_other (it_mount, st_parent.st_dev, st_mounted.st_dev,
                     st_mounted.st_ino, mounted);
        }
+      free (end);
+      free (path);
+      free (mounted);
     }
   (void) fclose (file);
 }
@@ -524,19 +559,22 @@ scan_swaps (void)
 {
   FILE *file;
   struct stat st;
-  char line[MAX_LINE + 1], path[PATH_MAX + 1], type[MAX_LINE + 1];
+  char line[MAX_LINE + 1], *path, type[MAX_LINE + 1];
 
   if (!(file = fopen (PROC_BASE "/swaps", "r")))
     return;                    /* old kernel */
   (void) fgets (line, MAX_LINE, file);
   while (fgets (line, MAX_LINE, file))
     {
-      if (sscanf (line, "%s %s", path, type) != 2)
+      if (sscanf (line, "%as %s", &path, type) != 2)
        continue;               /* new kernel :-) */
-      if (strcmp (type, "file"))
+      if (strcmp (type, "file")) {
+       free (path);
        continue;
+      }
       if (stat (path, &st) >= 0)
        add_other (it_swap, st.st_dev, st.st_dev, st.st_ino, path);
+      free (path);
     }
   (void) fclose (file);
 }
@@ -607,7 +645,7 @@ show_files_or_kill (void)
   FILE *f;
   const struct passwd *pw;
   const char *user, *scan;
-  char tmp[10], path[PATH_MAX + 1], comm[COMM_LEN + 1];
+  char tmp[10], *path, comm[COMM_LEN + 1];
   int length, header, first, dummy;
   header = 1;
   for (file = files; file; file = file->next)
@@ -677,13 +715,14 @@ show_files_or_kill (void)
                switch (item->type)
                  {
                  case it_proc:
-                   sprintf (path, PROC_BASE "/%d/stat", item->u.proc.pid);
+                   asprintf (&path, PROC_BASE "/%d/stat", item->u.proc.pid);
                    strcpy (comm, "???");
                    if ((f = fopen (path, "r")) != NULL)
                      {
                        (void) fscanf (f, "%d (%[^)]", &dummy, comm);
                        (void) fclose (f);
                      }
+                   free (path);
                    name = comm;
                    uid = item->u.proc.uid;
                    break;
@@ -936,7 +975,6 @@ int
 main (int argc, char **argv)
 {
   SPACE_DSC *name_space;
-  char path[PATH_MAX + 1];
   int flags, silent, do_kill, sig_number, no_files;
 
   flags = silent = do_kill = 0;
@@ -1071,7 +1109,6 @@ main (int argc, char **argv)
                  st.st_dev = st.st_rdev;
                else if (S_ISDIR (st.st_mode))
                  {
-                   sprintf (path, "%s/.", *argv);
                    if (stat (*argv, &st) < 0)
                      {
                        perror (*argv);
index 57b0108e0a575251a126183fd2d1643b62dbf289..3aaa11b7f7c9a2ccf8743fe51cef236acf7a596c 100644 (file)
@@ -2,6 +2,10 @@
 
 /* Copyright 1993-1998 Werner Almesberger. See file COPYING for details. */
 
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -64,8 +68,8 @@ kill_all (int signal, int names, char **namelist)
   FILE *file;
   struct stat st, sts[MAX_NAMES];
   int *name_len;
-  char path[PATH_MAX + 1], comm[COMM_LEN];
-  char command_buf[PATH_MAX + 1];
+  char *path, comm[COMM_LEN];
+  char *command_buf;
   char *command;
   pid_t *pid_table, pid, self, *pid_killed;
   pid_t *pgids;
@@ -141,9 +145,14 @@ kill_all (int signal, int names, char **namelist)
     }
   for (i = 0; i < pids; i++)
     {
-      sprintf (path, PROC_BASE "/%d/stat", pid_table[i]);
-      if (!(file = fopen (path, "r")))
+      if (asprintf (&path, PROC_BASE "/%d/stat", pid_table[i]) < 0)
        continue;
+      if (!(file = fopen (path, "r"))) 
+       {
+         free (path);
+         continue;
+       }
+      free (path);
       empty = 0;
       okay = fscanf (file, "%*d (%[^)]", comm) == 1;
       (void) fclose (file);
@@ -154,15 +163,33 @@ kill_all (int signal, int names, char **namelist)
       length = strlen (comm);
       if (length == COMM_LEN - 1)
        {
-         sprintf (path, PROC_BASE "/%d/cmdline", pid_table[i]);
-         if (!(file = fopen (path, "r")))
+         if (asprintf (&path, PROC_BASE "/%d/cmdline", pid_table[i]) < 0)
+           continue;
+         if (!(file = fopen (path, "r"))) {
+           free (path);
            continue;
+         }
+         free (path);
           while (1) {
             /* look for actual command so we skip over initial "sh" if any */
             char *p;
+           int cmd_size = 128;
+           command_buf = (char *)malloc (cmd_size);
+           if (!command_buf)
+             exit (1);
+
             /* 'cmdline' has arguments separated by nulls */
-            for (p=command_buf; p<command_buf+PATH_MAX; p++) {
-              int c = fgetc(file);
+            for (p=command_buf; ; p++) {
+              int c;
+             if (p == (command_buf + cmd_size)) 
+               {
+                 int cur_size = cmd_size;
+                 cmd_size *= 2;
+                 if (!realloc(&command_buf, cmd_size))
+                   exit (1);
+                 p = command_buf + cur_size;
+               }
+              c = fgetc(file);
               if (c == EOF || c == '\0') {
                 *p = '\0';
                 break;
@@ -209,9 +236,14 @@ kill_all (int signal, int names, char **namelist)
            }
          else
            {
-             sprintf (path, PROC_BASE "/%d/exe", pid_table[i]);
-             if (stat (path, &st) < 0)
+             if (asprintf (&path, PROC_BASE "/%d/exe", pid_table[i]) < 0)
                continue;
+             if (stat (path, &st) < 0) {
+               free (path);
+               continue;
+             }
+             free (path);
+
              if (sts[j].st_dev != st.st_dev || sts[j].st_ino != st.st_ino)
                continue;
            }
index 09b90cb8716c4cff2140b73da30d81aa0099eb67..6df6ad48cf75a78ad393b1f882c10584a14b452e 100644 (file)
@@ -481,7 +481,7 @@ read_proc (void)
   struct dirent *de;
   FILE *file;
   struct stat st;
-  char path[PATH_MAX + 1], comm[COMM_LEN + 1];
+  char *path, comm[COMM_LEN + 1];
   char *buffer;
   char readbuf[BUFSIZ+1];
   char *tmpptr;
@@ -505,11 +505,13 @@ read_proc (void)
   while ((de = readdir (dir)) != NULL)
     if ((pid = atoi (de->d_name)) != 0)
       {
+       if (!(path = malloc (strlen (PROC_BASE) + strlen (de->d_name) + 10)))
+         exit (2);
        sprintf (path, "%s/%d/stat", PROC_BASE, pid);
        if ((file = fopen (path, "r")) != NULL)
          {
            empty = 0;
-            sprintf (path, "%s/%d", PROC_BASE, pid);
+           sprintf (path, "%s/%d", PROC_BASE, pid);
             if (stat (path, &st) < 0)
            {
                perror (path);
@@ -554,9 +556,12 @@ read_proc (void)
                      buffer[size++] = 0;
                    add_proc (comm, pid, ppid, st.st_uid, buffer, size);
                  }
-             } } }
+               }
+             }
+           }
            (void) fclose (file);
          }
+       free (path);
       }
   (void) closedir (dir);
   if (print_args)