bug-hurd
[Top][All Lists]
Advanced

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

[PATCH] proc: cleanup


From: Neal H Walfield
Subject: [PATCH] proc: cleanup
Date: Fri, 13 Apr 2001 18:09:48 +0200
User-agent: Mutt/1.3.15i

This includes, among others, the proposed changes that I sent on March 26.

2000-04-03  Neal H Walfield  <neal@cs.uml.edu>

        * proc.h (struct ids): Drop gids and ngids, we never use
        them.  Turn uids into a zero length array.
        * mgt.c (make_ids): Conform to new semantics.
        (ids_free): Removed.  Rendered obsolete by ids_rele.
        (ids_rele): New function.  Drop a reference to the given
        ids.
        (ids_ref): Add a reference to the given ids.
        (S_proc_reauthenticate): Use ids_rele and new make_ids
        semantics.
        (S_proc_child): Use ids_rele and ids_ref.
        (create_startup_proc): Conform to new make_ids semantics.
        (complete_proc): Use ids_ref.
        (process_has_exited): Use ids_rele.

        * host.c: Doc fix: ``Implement foo as described in <hurd/proc.defs>''
        => ``Implement foo as described in <hurd/process.defs>''
        * info.c: Likewise.
        * mgt.c: Likewise.
        * pgrp.c: Likewise.
        * wait.c: Likewise.

        * mgt.c (S_proc_reauthenticate): Calculate the value of 
        ngen_uids, naux_uids, ngen_gids and naux_gids; do not use
        a constant.
        (create_startup_proc): Add asserts.

        * host.c (S_proc_setexecdata):  Check for memory allocation errors.
        Use memcpy, not bcopy.
        (S_proc_getexecdata): Likewise.
        (S_proc_execdata_notify): Check return of malloc.
        (S_proc_register_version): Likewise.
        (initialize_version_info): Add asserts.
        * info.c (S_proc_pid2task): Do not bother searching for the pid
        if we do not have a valid caller.
        (S_proc_pid2proc): Likewise.
        Use memcpy, not bcopy.
        (S_proc_getprocinfo): Doc fixes.  Use MAP_FAILED not -1.  Use
        memcpy, not bcopy.
        (S_proc_getloginpids): Check return value of malloc.  Use realloc
        correctly.
        (S_proc_setlogin): Check return value of malloc.
        * main.c (main): Assert allocate_proc.
        * mgt.c (make_ids): Check return value of malloc and fail
        accordingly.
        (S_proc_reauthenticate): Check return value of make_ids and fail
        accordingly.
        (S_proc_child): Call task_find after we know that we were passed
        a valid child.
        (S_proc_reassign): Likewise.
        (S_proc_handle_exceptions): Use memcpy, not bcopy.
        (allocate_proc): Check return value of ports_create_port and fail
        accordingly.
        (create_startup_proc): Add asserts.
        (complete_proc): Do not assume the length of "<none>".
        * msg.c (S_proc_getmsgport): Call pid_find_allow_zombie after
        we know we were passed a valid caller.
        * pgrp.c: Include assert.h.
        (new_pgrp): Check return value of malloc and fail accordingly.
        (new_session): Likewise.
        (boot_setsid): Assert sess.
        (S_proc_getsessionpids): Use MAP_FAILED not -1.
        (S_proc_getsessionppids): Likewise.
        (S_proc_getpgrppids): Likewise.
        * wait.c (S_proc_wait): Use memset, not bzero.

diff --exclude CVS -ur hurd-20010329-snapshot/proc/host.c 
hurd-20010401/proc/host.c
--- hurd-20010329-snapshot/proc/host.c  Tue Sep 16 21:30:42 1997
+++ hurd-20010401/proc/host.c   Fri Apr  6 19:25:52 2001
@@ -1,5 +1,5 @@
 /* Proc server host management calls
-   Copyright (C) 1992, 1993, 1994, 1996, 1997 Free Software Foundation
+   Copyright (C) 1992,93,94,96,97,2001 Free Software Foundation
 
 This file is part of the GNU Hurd.
 
@@ -32,6 +32,7 @@
 #include <unistd.h>
 #include <assert.h>
 #include <version.h>
+#include <sys/mman.h>
 
 #include "proc.h"
 #include "process_S.h"
@@ -55,7 +56,7 @@
 } *execdata_notifys;
 
 
-/* Implement proc_getprivports as described in <hurd/proc.defs>. */
+/* Implement proc_getprivports as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getprivports (struct proc *p,
                     mach_port_t *hostpriv,
@@ -73,7 +74,7 @@
 }
 
 
-/* Implement proc_setexecdata as described in <hurd/proc.defs>. */
+/* Implement proc_setexecdata as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_setexecdata (struct proc *p,
                    mach_port_t *ports,
@@ -83,12 +84,26 @@
 {
   int i;
   struct execdata_notify *n;
-  
+  mach_port_t *std_port_array_new;
+  int *std_int_array_new;
+
   if (!p)
     return EOPNOTSUPP;
   
   if (!check_uid (p, 0))
     return EPERM;
+
+  /* Allocate memory up front.  */
+  std_port_array_new = malloc (sizeof (mach_port_t) * nports);
+  if (! std_port_array_new)
+    return ENOMEM;
+
+  std_int_array_new = malloc (sizeof (int) * nints);
+  if (! std_int_array_new)
+    {
+      free (std_port_array_new);
+      return ENOMEM;
+    }
   
   if (std_port_array)
     {
@@ -99,13 +114,13 @@
   if (std_int_array)
     free (std_int_array);
   
-  std_port_array = malloc (sizeof (mach_port_t) * nports);
+  std_port_array = std_port_array_new;
   n_std_ports = nports;
-  bcopy (ports, std_port_array, sizeof (mach_port_t) * nports);
+  memcpy (std_port_array, ports, sizeof (mach_port_t) * nports);
   
-  std_int_array = malloc (sizeof (int) * nints);
+  std_int_array = std_int_array_new;
   n_std_ints = nints;
-  bcopy (ints, std_int_array, sizeof (int) * nints);
+  memcpy (std_int_array, ints, sizeof (int) * nints);
   
   for (n = execdata_notifys; n; n = n->next)
     exec_setexecdata (n->notify_port, std_port_array, MACH_MSG_TYPE_COPY_SEND,
@@ -114,7 +129,7 @@
   return 0;
 }
 
-/* Implement proc_getexecdata as described in <hurd/proc.defs>. */
+/* Implement proc_getexecdata as described in <hurd/process.defs>. */
 kern_return_t 
 S_proc_getexecdata (struct proc *p,
                    mach_port_t **ports,
@@ -125,34 +140,46 @@
 {
   /* No need to check P here; we don't use it. */
 
-  /* XXX memory leak here */
-
   if (!std_port_array)
     return ENOENT;
 
   if (*nports < n_std_ports)
-    *ports = malloc (n_std_ports * sizeof (mach_port_t));
-  bcopy (std_port_array, *ports, n_std_ports * sizeof (mach_port_t));
+    {
+      *ports = mmap (0, round_page (n_std_ports * sizeof (mach_port_t)),
+                    PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+      if (*ports == MAP_FAILED)
+        return ENOMEM;
+    }
+  memcpy (*ports, std_port_array, n_std_ports * sizeof (mach_port_t));
   *nports = n_std_ports;
   
   if (*nints < n_std_ints)
-    *ints = malloc (n_std_ints * sizeof (mach_port_t));
-  bcopy (std_int_array, *ints, n_std_ints * sizeof (int));
+    {
+      *ints = mmap (0, round_page (n_std_ints * sizeof (int)),
+                   PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+      if (*ints == MAP_FAILED)
+        return ENOMEM;
+    }
+  memcpy (*ints, std_int_array, n_std_ints * sizeof (int));
   *nints = n_std_ints;
 
   return 0;
 }
 
-/* Implement proc_execdata_notify as described in <hurd/proc.defs>. */
+/* Implement proc_execdata_notify as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_execdata_notify (struct proc *p,
                        mach_port_t notify)
 {
-  struct execdata_notify *n = malloc (sizeof (struct execdata_notify));
+  struct execdata_notify *n;
   mach_port_t foo;
 
   /* No need to check P here; we don't use it. */
 
+  n = malloc (sizeof (struct execdata_notify));
+  if (! n)
+    return ENOMEM;
+
   n->notify_port = notify;
   n->next = execdata_notifys;
   execdata_notifys = n;
@@ -327,6 +354,7 @@
   /* Notice Mach's and our own version and initialize server version
      varables. */
   server_versions = malloc (sizeof (struct server_version) * 10);
+  assert (server_versions);
   server_versions_nalloc = 10;
 
   err = host_kernel_version (mach_host_self (), kv);
@@ -338,10 +366,14 @@
   if (p)
     *p = '\0';
   kernel_name = strdup (p ? kv : "mach");
+  assert (kernel_name);
   kernel_version = strdup (p ? p + 1 : kv);
+  assert (kernel_version);
 
   server_versions[0].name = strdup ("proc");
+  assert (server_versions[0].name);
   server_versions[0].version = strdup (HURD_VERSION);
+  assert (server_versions[0].version);
 
   nserver_versions = 1;
 
@@ -366,6 +398,7 @@
                         char *release, 
                         char *version)
 {
+  error_t err = 0;
   int i;
 
   /* No need to check SERVER here; we don't use it. */
@@ -380,6 +413,11 @@
        /* Change this entry.  */
        free (server_versions[i].version);
        server_versions[i].version = malloc (strlen (version) + 1);
+       if (! server_versions[i].version)
+         {
+           err = ENOMEM;
+           goto out;
+         }
        strcpy (server_versions[i].version, version);
        break;
       }
@@ -388,21 +426,40 @@
       /* Didn't find it; extend.  */
       if (nserver_versions == server_versions_nalloc)
        {
+         void *new = realloc (server_versions,
+                              sizeof (struct server_version) *
+                              server_versions_nalloc * 2);
+         if (! new)
+           {
+             err = ENOMEM;
+             goto out;
+           }
+
          server_versions_nalloc *= 2;
-         server_versions = realloc (server_versions,
-                                    sizeof (struct server_version) *
-                                    server_versions_nalloc);
+         server_versions = new;
        }
       server_versions[nserver_versions].name = malloc (strlen (name) + 1);
+      if (! server_versions[nserver_versions].name)
+       {
+         err = ENOMEM;
+         goto out;
+       }
       server_versions[nserver_versions].version = malloc (strlen (version) 
                                                          + 1);
+      if (! server_versions[nserver_versions].version)
+        {
+         free (server_versions[nserver_versions].name);
+         err = ENOMEM;
+         goto out;
+       }
       strcpy (server_versions[nserver_versions].name, name);
       strcpy (server_versions[nserver_versions].version, version);
       nserver_versions++;
     }
   
   rebuild_uname ();
+out:
   mach_port_deallocate (mach_task_self (), credential);
-  return 0;
+  return err;
 }
 
diff --exclude CVS -ur hurd-20010329-snapshot/proc/info.c 
hurd-20010401/proc/info.c
--- hurd-20010329-snapshot/proc/info.c  Mon Jan 24 08:14:02 2000
+++ hurd-20010401/proc/info.c   Fri Apr  6 18:54:53 2001
@@ -1,5 +1,5 @@
 /* Process information queries
-   Copyright (C) 1992,93,94,95,96,99,2000 Free Software Foundation, Inc.
+   Copyright (C) 1992,93,94,95,96,99,2000,01 Free Software Foundation, Inc.
 
 This file is part of the GNU Hurd.
 
@@ -33,17 +33,18 @@
 #include "proc.h"
 #include "process_S.h"
 
-/* Implement S_proc_pid2task as described in <hurd/proc.defs>. */
+/* Implement S_proc_pid2task as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_pid2task (struct proc *callerp,
-              pid_t pid,
-              task_t *t)
+                pid_t pid,
+                task_t *t)
 {
-  struct proc *p = pid_find_allow_zombie (pid);
-
+  struct proc *p;
+  
   if (!callerp)
     return EOPNOTSUPP;
 
+  p = pid_find_allow_zombie (pid);
   if (!p)
     return ESRCH;
 
@@ -62,11 +63,11 @@
   return 0;
 }
 
-/* Implement proc_task2pid as described in <hurd/proc.defs>. */
+/* Implement proc_task2pid as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_task2pid (struct proc *callerp,
-              task_t t,
-              pid_t *pid)
+                task_t t,
+                pid_t *pid)
 {
   struct proc *p = task_find (t);
 
@@ -80,11 +81,11 @@
   return 0;
 }
 
-/* Implement proc_task2proc as described in <hurd/proc.defs>. */
+/* Implement proc_task2proc as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_task2proc (struct proc *callerp,
-               task_t t,
-               mach_port_t *outproc)
+                 task_t t,
+                 mach_port_t *outproc)
 {
   struct proc *p = task_find (t);
 
@@ -98,10 +99,10 @@
   return 0;
 }
 
-/* Implement proc_proc2task as described in <hurd/proc.defs>. */
+/* Implement proc_proc2task as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_proc2task (struct proc *p,
-               task_t *t)
+                 task_t *t)
 {
   if (!p)
     return EOPNOTSUPP;
@@ -109,17 +110,18 @@
   return 0;
 }
 
-/* Implement proc_pid2proc as described in <hurd/proc.defs>. */
+/* Implement proc_pid2proc as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_pid2proc (struct proc *callerp,
-              pid_t pid,
-              mach_port_t *outproc)
+                pid_t pid,
+                mach_port_t *outproc)
 {
-  struct proc *p = pid_find_allow_zombie (pid);
+  struct proc *p;
 
   if (!callerp)
     return EOPNOTSUPP;
 
+  p = pid_find_allow_zombie (pid);
   if (!p)
     return ESRCH;
 
@@ -225,7 +227,7 @@
            if (*vec == NULL)
              err = ENOMEM;
            else
-             bcopy ((char *)(data + (addr - readaddr)), *vec,
+             memcpy (*vec, (char *)(data + (addr - readaddr)),
                     (char *)t - (char *)(data + (addr - readaddr)));
            break;
          }
@@ -313,7 +315,7 @@
 }
 
 
-/* Implement proc_getprocargs as described in <hurd/proc.defs>. */
+/* Implement proc_getprocargs as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getprocargs (struct proc *callerp,
                  pid_t pid,
@@ -330,7 +332,7 @@
   return get_string_array (p->p_task, p->p_argv, (vm_address_t *) buf, buflen);
 }
 
-/* Implement proc_getprocenv as described in <hurd/proc.defs>. */
+/* Implement proc_getprocenv as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getprocenv (struct proc *callerp,
                 pid_t pid,
@@ -351,7 +353,7 @@
 #define PI_FETCH_THREAD_DETAILS  \
   (PI_FETCH_THREAD_SCHED | PI_FETCH_THREAD_BASIC | PI_FETCH_THREAD_WAITS)
 
-/* Implement proc_getprocinfo as described in <hurd/proc.defs>. */
+/* Implement proc_getprocinfo as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getprocinfo (struct proc *callerp,
                    pid_t pid,
@@ -406,6 +408,8 @@
   if (structsize / sizeof (int) > *piarraylen)
     {
       *piarray = mmap (0, structsize, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
+      if (*piarray == MAP_FAILED)
+        return errno;
       pi_alloced = 1;
     }
   *piarraylen = structsize / sizeof (int);
@@ -489,15 +493,15 @@
              continue;
            }
          if (err)
-           /* Something screwy, give up o nthis bit of info.  */
+           /* Something screwy, give up on this bit of info.  */
            {
              *flags &= ~PI_FETCH_THREAD_SCHED;
              err = 0;
            }
        }
 
-      /* Note that there are thread wait entries only for threads not marked
-        dead.  */
+      /* Note that there are thread wait entries only for those threads
+         not marked dead.  */
 
       if (*flags & PI_FETCH_THREAD_WAITS)
        {
@@ -526,14 +530,14 @@
 
                  new_waits = mmap (0, new_len, PROT_READ|PROT_WRITE,
                                    MAP_ANON, 0, 0);
-                 err = (new_waits == (char *) -1) ? errno : 0;
+                 err = (new_waits == MAP_FAILED) ? errno : 0;
                  if (err)
                    /* Just don't return any more waits information.  */
                    *flags &= ~PI_FETCH_THREAD_WAITS;
                  else
                    {
                      if (waits_used > 0)
-                       bcopy (*waits, new_waits, waits_used);
+                       memcpy (new_waits, *waits, waits_used);
                      if (*waits_len > 0 && waits_alloced)
                        munmap (*waits, *waits_len);
                      *waits = new_waits;
@@ -545,7 +549,7 @@
              if (waits_used + desc_len + 1 <= *waits_len)
                /* Append DESC to WAITS.  */
                {
-                 bcopy (desc, *waits + waits_used, desc_len);
+                 memcpy (*waits + waits_used, desc, desc_len);
                  waits_used += desc_len;
                  (*waits)[waits_used++] = '\0';
                }
@@ -608,6 +612,7 @@
                     pid_t **pids,
                     u_int *npids)
 {
+  error_t err = 0;
   struct proc *l = pid_find (id);
   struct proc *p;
   struct proc **tail, **new, **parray;
@@ -622,6 +627,9 @@
   /* Simple breadth first search of the children of L. */
   parraysize = 50;
   parray = malloc (sizeof (struct proc *) * parraysize);
+  if (! parray)
+    return ENOMEM;
+
   parray[0] = l;
   for (tail = parray, new = &parray[1]; tail != new; tail++)
     {
@@ -634,6 +642,12 @@
                struct proc **newparray;
                newparray = realloc (parray, ((parraysize *= 2)
                                              * sizeof (struct proc *)));
+               if (! newparray)
+                 {
+                   free (parray);
+                   return ENOMEM;
+                 }
+
                tail = newparray + (tail - parray);
                new = newparray + (new - parray);
                parray = newparray;
@@ -643,19 +657,28 @@
     }
 
   if (*npids < new - parray)
-    *pids = mmap (0, (new - parray) * sizeof (pid_t), PROT_READ|PROT_WRITE,
-                 MAP_ANON, 0, 0);
-  *npids = new - parray;
-  for (i = 0; i < *npids; i++)
-    (*pids)[i] = parray[i]->p_pid;
+    {
+      *pids = mmap (0, (new - parray) * sizeof (pid_t), PROT_READ|PROT_WRITE,
+                   MAP_ANON, 0, 0);
+      if (*pids == MAP_FAILED)
+        err = errno;
+    }
+
+  if (! err)
+    {
+      *npids = new - parray;
+      for (i = 0; i < *npids; i++)
+        (*pids)[i] = parray[i]->p_pid;
+    }
+
   free (parray);
-  return 0;
+  return err;
 }
 
-/* Implement proc_setlogin as described in <hurd/proc.defs>. */
+/* Implement proc_setlogin as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_setlogin (struct proc *p,
-              char *login)
+                char *login)
 {
   struct login *l;
 
@@ -666,6 +689,9 @@
     return EPERM;
 
   l = malloc (sizeof (struct login) + strlen (login) + 1);
+  if (! l)
+    return ENOMEM;
+
   l->l_refcnt = 1;
   strcpy (l->l_name, login);
   if (!--p->p_login->l_refcnt)
@@ -674,10 +700,10 @@
   return 0;
 }
 
-/* Implement proc_getlogin as described in <hurd/proc.defs>. */
+/* Implement proc_getlogin as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getlogin (struct proc *p,
-              char *login)
+                char *login)
 {
   if (!p)
     return EOPNOTSUPP;
@@ -685,7 +711,7 @@
   return 0;
 }
 
-/* Implement proc_get_tty as described in <hurd/proc.defs>. */
+/* Implement proc_get_tty as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_get_tty (struct proc *p, pid_t pid,
                mach_port_t *tty, mach_msg_type_name_t *tty_type)
diff --exclude CVS -ur hurd-20010329-snapshot/proc/main.c 
hurd-20010401/proc/main.c
--- hurd-20010329-snapshot/proc/main.c  Fri Mar 17 11:31:34 2000
+++ hurd-20010401/proc/main.c   Fri Apr  6 16:19:49 2001
@@ -1,5 +1,5 @@
 /* Initialization of the proc server
-   Copyright (C) 1993,94,95,96,97,99,2000 Free Software Foundation, Inc.
+   Copyright (C) 1993,94,95,96,97,99,2000,01 Free Software Foundation, Inc.
 
 This file is part of the GNU Hurd.
 
@@ -85,6 +85,8 @@
 
   /* Create our own proc object (we are PID 0).  */
   self_proc = allocate_proc (mach_task_self ());
+  assert (self_proc);
+
   complete_proc (self_proc, 0);
 
   startup_port = ports_get_send_right (startup_proc);
@@ -101,8 +103,8 @@
   add_proc_to_hash (startup_proc); /* Now that we have the task port.  */
 
   /* Set our own argv and envp locations.  */
-  self_proc->p_argv = (int) argv;
-  self_proc->p_envp = (int) envp;
+  self_proc->p_argv = (vm_address_t) argv;
+  self_proc->p_envp = (vm_address_t) envp;
 
   /* Give ourselves good scheduling performance, because we are so
      important. */
diff --exclude CVS -ur hurd-20010329-snapshot/proc/mgt.c 
hurd-20010401/proc/mgt.c
--- hurd-20010329-snapshot/proc/mgt.c   Tue Mar 14 01:49:42 2000
+++ hurd-20010401/proc/mgt.c    Fri Apr  6 20:04:18 2001
@@ -1,5 +1,5 @@
 /* Process management
-   Copyright (C) 1992,93,94,95,96,99,2000 Free Software Foundation, Inc.
+   Copyright (C) 1992,93,94,95,96,99,2000,01 Free Software Foundation, Inc.
 
 This file is part of the GNU Hurd.
 
@@ -42,29 +42,34 @@
 
 /* Create a new id structure with the given genuine uids and gids. */
 static inline struct ids *
-make_ids (const uid_t *uids, size_t nuids, const uid_t *gids, size_t ngids)
+make_ids (const uid_t *uids, size_t nuids)
 {
   struct ids *i;
 
-  i = malloc (sizeof (struct ids));
+  i = malloc (sizeof (struct ids) + sizeof (uid_t) * nuids);;
+  if (! i)
+    return NULL;
+
   i->i_nuids = nuids;
-  i->i_ngids = ngids;
-  i->i_uids = malloc (sizeof (uid_t) * nuids);
-  i->i_gids = malloc (sizeof (uid_t) * ngids);
   i->i_refcnt = 1;
 
-  memcpy (i->i_uids, uids, sizeof (uid_t) * nuids);
-  memcpy (i->i_gids, gids, sizeof (uid_t) * ngids);
+  memcpy (&i->i_uids, uids, sizeof (uid_t) * nuids);
   return i;
 }
 
+static inline void
+ids_ref (struct ids *i)
+{
+  i->i_refcnt ++;
+}
+
 /* Free an id structure. */
 static inline void
-free_ids (struct ids *i)
+ids_rele (struct ids *i)
 {
-  free (i->i_uids);
-  free (i->i_gids);
-  free (i);
+  i->i_refcnt --;
+  if (i->i_refcnt == 0)
+    free (i);
 }
 
 /* Tell if process P has uid UID, or has root.  */
@@ -78,8 +83,7 @@
   return 0;
 }
 
-
-/* Implement proc_reathenticate as described in <hurd/proc.defs>. */
+/* Implement proc_reathenticate as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_reauthenticate (struct proc *p, mach_port_t rendport)
 {
@@ -96,9 +100,10 @@
   gen_gids = ggbuf;
   aux_gids = agbuf;
 
-  ngen_uids = naux_uids = 50;
-  ngen_gids = naux_gids = 50;
-
+  ngen_uids = sizeof (gubuf) / sizeof (uid_t);
+  naux_uids = sizeof (aubuf) / sizeof (uid_t);
+  ngen_gids = sizeof (ggbuf) / sizeof (uid_t);
+  naux_gids = sizeof (agbuf) / sizeof (uid_t);
 
   err = auth_server_authenticate (authserver,
                                  rendport, MACH_MSG_TYPE_COPY_SEND,
@@ -111,9 +116,10 @@
     return err;
   mach_port_deallocate (mach_task_self (), rendport);
 
-  if (!--p->p_id->i_refcnt)
-    free_ids (p->p_id);
-  p->p_id = make_ids (gen_uids, ngen_uids, gen_gids, ngen_gids);
+  ids_rele (p->p_id);
+  p->p_id = make_ids (gen_uids, ngen_uids);
+  if (! p->p_id)
+    err = ENOMEM;
 
   if (gen_uids != gubuf)
     munmap (gen_uids, ngen_uids * sizeof (uid_t));
@@ -124,19 +130,20 @@
   if (aux_gids != agbuf)
     munmap (aux_gids, naux_gids * sizeof (uid_t));
 
-  return 0;
+  return err;
 }
 
-/* Implement proc_child as described in <hurd/proc.defs>. */
+/* Implement proc_child as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_child (struct proc *parentp,
              task_t childt)
 {
-  struct proc *childp = task_find (childt);
+  struct proc *childp;
 
   if (!parentp)
     return EOPNOTSUPP;
 
+  childp = task_find (childt);
   if (!childp)
     return ESRCH;
 
@@ -157,10 +164,9 @@
   childp->p_owner = parentp->p_owner;
   childp->p_noowner = parentp->p_noowner;
 
-  if (!--childp->p_id->i_refcnt)
-    free_ids (childp->p_id);
+  ids_rele (childp->p_id);
+  ids_ref (parentp->p_id);
   childp->p_id = parentp->p_id;
-  childp->p_id->i_refcnt++;
 
   /* Process hierarchy.  Remove from our current location
      and place us under our new parent.  Sanity check to make sure
@@ -194,16 +200,17 @@
   return 0;
 }
 
-/* Implement proc_reassign as described in <hurd/proc.defs>. */
+/* Implement proc_reassign as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_reassign (struct proc *p,
                 task_t newt)
 {
-  struct proc *stubp = task_find (newt);
+  struct proc *stubp;
 
   if (!p)
     return EOPNOTSUPP;
 
+  stubp = task_find (newt);
   if (!stubp)
     return ESRCH;
 
@@ -218,7 +225,7 @@
   mach_port_destroy (mach_task_self (), p->p_task);
   p->p_task = stubp->p_task;
 
-  /* For security, we need use the request port from STUBP */
+  /* For security, we need to use the request port from STUBP */
   ports_transfer_right (p, stubp);
 
   /* Enqueued messages might refer to the old task port, so
@@ -245,7 +252,7 @@
   return 0;
 }
 
-/* Implement proc_setowner as described in <hurd/proc.defs>. */
+/* Implement proc_setowner as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_setowner (struct proc *p,
                 uid_t owner,
@@ -268,7 +275,7 @@
   return 0;
 }
 
-/* Implement proc_getpids as described in <hurd/proc.defs>. */
+/* Implement proc_getpids as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getpids (struct proc *p,
                pid_t *pid,
@@ -283,7 +290,7 @@
   return 0;
 }
 
-/* Implement proc_set_arg_locations as described in <hurd/proc.defs>. */
+/* Implement proc_set_arg_locations as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_set_arg_locations (struct proc *p,
                          vm_address_t argv,
@@ -296,7 +303,7 @@
   return 0;
 }
 
-/* Implement proc_get_arg_locations as described in <hurd/proc.defs>. */
+/* Implement proc_get_arg_locations as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_get_arg_locations (struct proc *p,
                          vm_address_t *argv,
@@ -307,13 +314,14 @@
   return 0;
 }
 
-/* Implement proc_dostop as described in <hurd/proc.defs>. */
+/* Implement proc_dostop as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_dostop (struct proc *p,
               thread_t contthread)
 {
   thread_t threadbuf[2], *threads = threadbuf;
-  unsigned int nthreads = 2, i;
+  int nthreads = sizeof (threadbuf) / sizeof (thread_t);
+  int i;
   error_t err;
 
   if (!p)
@@ -328,7 +336,7 @@
   for (i = 0; i < nthreads; i++)
     {
       if (threads[i] != contthread)
-       err = thread_suspend (threads[i]);
+       thread_suspend (threads[i]);
       mach_port_deallocate (mach_task_self (), threads[i]);
     }
   if (threads != threadbuf)
@@ -359,7 +367,7 @@
                          mach_msg_type_number_t statecnt)
 {
   struct exc *e;
-    error_t err;
+  error_t err;
 
   /* No need to check P here; we don't use it. */
 
@@ -372,7 +380,7 @@
   e->forwardport = forwardport;
   e->flavor = flavor;
   e->statecnt = statecnt;
-  bcopy (new_state, e->thread_state, statecnt * sizeof (natural_t));
+  memcpy (e->thread_state, new_state, statecnt * sizeof (natural_t));
   ports_port_deref (e);
   return 0;
 }
@@ -429,12 +437,12 @@
       /* FALLTHROUGH */
 
     case MACH_SEND_NOTIFY_IN_PROGRESS:
-      /* The port's queue is full, meaning the thread didn't receive
+      /* The port's queue is full; this means the thread didn't receive
         the exception message we forwarded last time it faulted.
         Declare that signal thread hopeless and the task crashed.  */
 
       /* Translate the exception code into a signal number
-        and mark the process has dying that way.  */
+        and mark the process as having died that way.  */
       hsd.exc = exception;
       hsd.exc_code = code;
       hsd.exc_subcode = subcode;
@@ -443,8 +451,8 @@
       p->p_status = W_EXITCODE (0, signo);
       p->p_sigcode = hsd.code;
 
-      /* Nuke the task; we will get a notification message and report it
-        died with SIGNO.  */
+      /* Nuke the task; we will get a notification message and report that
+        it died with SIGNO.  */
       task_terminate (task);
       ports_port_deref (e);
 
@@ -478,7 +486,7 @@
 
 }
 
-/* Implement proc_getallpids as described in <hurd/proc.defs>. */
+/* Implement proc_getallpids as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getallpids (struct proc *p,
                   pid_t **pids,
@@ -504,8 +512,12 @@
   prociterate (count_up, &nprocs);
 
   if (nprocs > *pidslen)
-    *pids = mmap (0, nprocs * sizeof (pid_t), PROT_READ|PROT_WRITE,
-                 MAP_ANON, 0, 0);
+    {
+      *pids = mmap (0, nprocs * sizeof (pid_t), PROT_READ|PROT_WRITE,
+                   MAP_ANON, 0, 0);
+      if (*pids == MAP_FAILED)
+        return ENOMEM;
+    }
 
   loc = *pids;
   prociterate (store_pid, &loc);
@@ -520,13 +532,16 @@
 struct proc *
 allocate_proc (task_t task)
 {
+  error_t err;
   struct proc *p;
 
   /* Pid 0 is us; pid 1 is init.  We handle those here specially;
      all other processes inherit from init here (though proc_child
      will move them to their actual parent usually).  */
 
-  ports_create_port (proc_class, proc_bucket, sizeof (struct proc), &p);
+  err = ports_create_port (proc_class, proc_bucket, sizeof (struct proc), &p);
+  if (err)
+    return NULL;
 
   p->p_task = task;
   p->p_msgport = MACH_PORT_NULL;
@@ -558,8 +573,10 @@
 {
   static const uid_t zero;
   struct proc *p;
+  const char *rootsname = "root";
 
   p = allocate_proc (MACH_PORT_NULL);
+  assert (p);
 
   p->p_pid = 1;
 
@@ -572,12 +589,15 @@
   p->p_deadmsg = 1;            /* Force initial "re-"fetch of msgport.  */
 
   p->p_noowner = 0;
-  p->p_id = make_ids (&zero, 1, &zero, 1);
+  p->p_id = make_ids (&zero, 1);
+  assert (p->p_id);
 
   p->p_loginleader = 1;
-  p->p_login = malloc (sizeof (struct login) + 5);
+  p->p_login = malloc (sizeof (struct login) + strlen (rootsname) + 1);
+  assert (p->p_login);
+
   p->p_login->l_refcnt = 1;
-  strcpy (p->p_login->l_name, "root");
+  strcpy (p->p_login->l_name, rootsname);
 
   boot_setsid (p);
 
@@ -611,19 +631,20 @@
   /* Because these have a reference count of one before starting,
      they can never be freed, so we're safe. */
   static struct login *nulllogin;
-  static struct ids nullids = {0, 0, 0, 0, 1};
+  static struct ids nullids = { i_refcnt : 1, i_nuids : 0};
+  const char nullsname [] = "<none>";
 
   if (!nulllogin)
     {
-      nulllogin = malloc (sizeof (struct login) + 7);
+      nulllogin = malloc (sizeof (struct login) + sizeof (nullsname) + 1);
       nulllogin->l_refcnt = 1;
-      strcpy (nulllogin->l_name, "<none>");
+      strcpy (nulllogin->l_name, nullsname);
     }
 
   p->p_pid = pid;
 
+  ids_ref (&nullids);
   p->p_id = &nullids;
-  p->p_id->i_refcnt++;
 
   p->p_login = nulllogin;
   p->p_login->l_refcnt++;
@@ -655,8 +676,11 @@
 static struct proc *
 new_proc (task_t task)
 {
-  struct proc *p = allocate_proc (task);
-  complete_proc (p, genpid ());
+  struct proc *p;
+  
+  p = allocate_proc (task);
+  if (p)
+    complete_proc (p, genpid ());
   return p;
 }
 
@@ -682,20 +706,19 @@
 
   prociterate ((void (*) (struct proc *, void *))check_message_dying, p);
 
-  /* Nuke external send rights and the (possible) associated reference */
+  /* Nuke external send rights and the (possible) associated reference.  */
   ports_destroy_right (p);
 
   if (!--p->p_login->l_refcnt)
     free (p->p_login);
 
-  if (!--p->p_id->i_refcnt)
-    free_ids (p->p_id);
+  ids_rele (p->p_id);
 
   /* Reparent our children to init by attaching the head and tail
-     of our list onto init's. */
+     of our list onto init's.  */
   if (p->p_ochild)
     {
-      struct proc *tp;         /* will point to the last one */
+      struct proc *tp;         /* will point to the last one.  */
       int isdead = 0;
 
       /* first tell them their parent is changing */
@@ -715,7 +738,7 @@
                                !tp->p_pgrp->pg_orphcnt);
       tp->p_parent = startup_proc;
 
-      /* And now nappend the lists. */
+      /* And now append the lists. */
       tp->p_sib = startup_proc->p_ochild;
       if (tp->p_sib)
        tp->p_sib->p_prevsib = &tp->p_sib;
diff --exclude CVS -ur hurd-20010329-snapshot/proc/msg.c 
hurd-20010401/proc/msg.c
--- hurd-20010329-snapshot/proc/msg.c   Tue May  4 19:05:27 1999
+++ hurd-20010401/proc/msg.c    Tue Apr  3 23:14:59 2001
@@ -1,5 +1,5 @@
 /* Message port manipulations
-   Copyright (C) 1994, 1995, 1996, 1999 Free Software Foundation
+   Copyright (C) 1994, 1995, 1996, 1999, 2001 Free Software Foundation
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -116,10 +116,12 @@
                   mach_port_t *msgport)
 {
   int cancel;
-  struct proc *p = pid_find_allow_zombie (pid);
+  struct proc *p;
 
   if (!callerp)
     return EOPNOTSUPP;
+
+  p = pid_find_allow_zombie (pid);
 
 restart:  
   while (p && p->p_deadmsg && !p->p_dead)
diff --exclude CVS -ur hurd-20010329-snapshot/proc/pgrp.c 
hurd-20010401/proc/pgrp.c
--- hurd-20010329-snapshot/proc/pgrp.c  Sun Jul 11 07:32:01 1999
+++ hurd-20010401/proc/pgrp.c   Tue Apr  3 23:14:59 2001
@@ -1,5 +1,5 @@
 /* Session and process group manipulation 
-   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1999 Free Software Foundation, 
Inc.
+   Copyright (C) 1992,93,94,95,96,99,2001 Free Software Foundation, Inc.
 
 This file is part of the GNU Hurd.
 
@@ -25,6 +25,7 @@
 #include <sys/errno.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <assert.h>
 
 #include "proc.h"
 #include "process_S.h"
@@ -39,6 +40,9 @@
   struct pgrp *pg;
   
   pg = malloc (sizeof (struct pgrp));
+  if (! pg)
+    return NULL;
+
   pg->pg_plist = 0;
   pg->pg_pgid = pgid;
   pg->pg_orphcnt = 0;
@@ -61,6 +65,9 @@
   struct session *sess;
   
   sess = malloc (sizeof (struct session));
+  if (! sess)
+    return NULL;
+
   sess->s_sid = p->p_pid;
   sess->s_pgrps = 0;
   sess->s_sessionid = MACH_PORT_NULL;
@@ -94,7 +101,7 @@
   free (pg);
 }
  
-/* Implement proc_setsid as described in <hurd/proc.defs>. */
+/* Implement proc_setsid as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_setsid (struct proc *p)
 {
@@ -123,11 +130,12 @@
   
   sess = new_session (p);
   p->p_pgrp = new_pgrp (p->p_pid, sess);
+  assert (p->p_pgrp);
   join_pgrp (p);
   return;
 }
 
-/* Implement proc_getsid as described in <hurd/proc.defs>. */
+/* Implement proc_getsid as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getsid (struct proc *callerp,
             pid_t pid,
@@ -143,7 +151,7 @@
   return 0;
 }
 
-/* Implement proc_getsessionpids as described in <hurd/proc.defs>. */
+/* Implement proc_getsessionpids as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getsessionpids (struct proc *callerp,
                       pid_t sid,
@@ -176,6 +184,9 @@
     {
       *pids = mmap (0, count * sizeof (pid_t), PROT_READ|PROT_WRITE,
                    MAP_ANON, 0, 0);
+      if (*pids == MAP_FAILED)
+       return errno;
+
       pp = *pids;
       for (pg = s->s_pgrps; pg; pg = pg->pg_next)
        for (p = pg->pg_plist; p; p = p->p_gnext)
@@ -216,6 +227,9 @@
     {
       *pgids = mmap (0, count * sizeof (pid_t), PROT_READ|PROT_WRITE,
                     MAP_ANON, 0, 0);
+      if (*pgids == MAP_FAILED)
+       return errno;
+
       pp = *pgids;
       for (pg = s->s_pgrps; pg; pg = pg->pg_next)
        *pp++ = pg->pg_pgid;
@@ -259,6 +273,9 @@
     {
       *pids = mmap (0, count * sizeof (pid_t), PROT_READ|PROT_WRITE,
                    MAP_ANON, 0, 0);
+      if (*pids == MAP_FAILED)
+       return errno;
+
       pp = *pids;
       for (p = pg->pg_plist; p; p = p->p_gnext)
        *pp++ = p->p_pid;
@@ -268,7 +285,7 @@
   return 0;
 }
 
-/* Implement proc_getsidport as described in <hurd/proc.defs>. */
+/* Implement proc_getsidport as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getsidport (struct proc *p,
                   mach_port_t *sessport, mach_msg_type_name_t *sessport_type)
@@ -291,7 +308,7 @@
   return err;
 }
 
-/* Implement proc_setpgrp as described in <hurd/proc.defs>. */
+/* Implement proc_setpgrp as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_setpgrp (struct proc *callerp,
              pid_t pid,
@@ -334,7 +351,7 @@
   return 0;
 }
 
-/* Implement proc_getpgrp as described in <hurd/proc.defs>. */
+/* Implement proc_getpgrp as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_getpgrp (struct proc *callerp,
              pid_t pid,
@@ -353,7 +370,7 @@
   return 0;
 }
 
-/* Implement proc_mark_exec as described in <hurd/proc.defs>. */
+/* Implement proc_mark_exec as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_mark_exec (struct proc *p)
 {
diff --exclude CVS -ur hurd-20010329-snapshot/proc/proc.h 
hurd-20010401/proc/proc.h
--- hurd-20010329-snapshot/proc/proc.h  Sun Mar 12 09:53:12 2000
+++ hurd-20010401/proc/proc.h   Tue Apr  3 21:45:17 2001
@@ -110,10 +110,9 @@
 
 struct ids
 {
-  int i_nuids, i_ngids;
-  uid_t *i_uids;
-  gid_t *i_gids;
   int i_refcnt;
+  int i_nuids;
+  uid_t i_uids[0];
 };
 
 /* Structure for an exception port we are listening on.  */
diff --exclude CVS -ur hurd-20010329-snapshot/proc/wait.c 
hurd-20010401/proc/wait.c
--- hurd-20010329-snapshot/proc/wait.c  Tue Jul 16 17:34:26 1996
+++ hurd-20010401/proc/wait.c   Tue Apr  3 23:14:59 2001
@@ -1,5 +1,5 @@
 /* Implementation of wait
-   Copyright (C) 1994, 1995, 1996 Free Software Foundation
+   Copyright (C) 1994, 1995, 1996, 2001 Free Software Foundation
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -113,7 +113,7 @@
          *sigcode = child->p_sigcode;
          if (child->p_dead)
            complete_exit (child);
-         bzero (ru, sizeof (struct rusage));
+         memset (ru, 0, sizeof (struct rusage));
          *pid_status = pid;
          return 0;
        }
@@ -136,7 +136,7 @@
                *pid_status = child->p_pid;
                if (child->p_dead)
                  complete_exit (child);
-               bzero (ru, sizeof (struct rusage));
+               memset (ru, 0, sizeof (struct rusage));
                return 0;
              }
          }
@@ -157,7 +157,7 @@
   goto start_over;
 }
 
-/* Implement proc_mark_stop as described in <hurd/proc.defs>. */
+/* Implement proc_mark_stop as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_mark_stop (struct proc *p,
                  int signo,
@@ -183,7 +183,7 @@
   return 0;
 }
 
-/* Implement proc_mark_exit as described in <hurd/proc.defs>. */
+/* Implement proc_mark_exit as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_mark_exit (struct proc *p,
                  int status,
@@ -201,7 +201,7 @@
   return 0;
 }
 
-/* Implement proc_mark_cont as described in <hurd/proc.defs>. */
+/* Implement proc_mark_cont as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_mark_cont (struct proc *p)
 {
@@ -211,7 +211,7 @@
   return 0;
 }
 
-/* Implement proc_mark_traced as described in <hurd/proc.defs>. */
+/* Implement proc_mark_traced as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_mark_traced (struct proc *p)
 {
@@ -221,7 +221,7 @@
   return 0;
 }
 
-/* Implement proc_mark_nostopchild as described in <hurd/proc.defs>. */
+/* Implement proc_mark_nostopchild as described in <hurd/process.defs>. */
 kern_return_t
 S_proc_mod_stopchild (struct proc *p,
                      int value)

Attachment: pgpuVvo8qy3OS.pgp
Description: PGP signature


reply via email to

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