* coding.c (make_conversion_work_buffer): Disable buffer modification
[bpt/emacs.git] / src / process.c
index 6bbdc53..5084bc8 100644 (file)
@@ -600,6 +600,7 @@ make_process (name)
   p->raw_status_new = 0;
   p->status = Qrun;
   p->mark = Fmake_marker ();
+  p->kill_without_query = 0;
 
 #ifdef ADAPTIVE_READ_BUFFERING
   p->adaptive_read_buffering = 0;
@@ -1220,7 +1221,7 @@ a socket connection.  */)
   return XPROCESS (process)->type;
 }
 #endif
-  
+
 DEFUN ("process-type", Fprocess_type, Sprocess_type, 1, 1, 0,
        doc: /* Return the connection type of PROCESS.
 The value is either the symbol `real', `network', or `serial'.
@@ -4136,9 +4137,9 @@ It is read into the process' buffers or given to their filter functions.
 Non-nil arg PROCESS means do not return until some output has been received
 from PROCESS.
 
-Non-nil second arg SECONDS and third arg MILLISEC are number of
-seconds and milliseconds to wait; return after that much time whether
-or not there is input.  If SECONDS is a floating point number,
+Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
+and milliseconds to wait; return after that much time whether or not
+there is any subprocess output.  If SECONDS is a floating point number,
 it specifies a fractional number of seconds to wait.
 The MILLISEC argument is obsolete and should be avoided.
 
@@ -7135,7 +7136,7 @@ static void
 get_up_time (time_t *sec, unsigned *usec)
 {
   FILE *fup;
-  
+
   *sec = *usec = 0;
 
   BLOCK_INPUT;
@@ -7245,10 +7246,10 @@ procfs_system_process_attributes (pid)
   char procbuf[1025], *p, *q;
   int fd;
   ssize_t nread;
-  char cmd[PATH_MAX];
+  const char *cmd = NULL;
   char *cmdline = NULL;
-  size_t cmdsize;
-  int c;
+  size_t cmdsize = 0, cmdline_size;
+  unsigned char c;
   int proc_id, ppid, uid, gid, pgrp, sess, tty, tpgid, thcount;
   unsigned long long utime, stime, cutime, cstime, start;
   long priority, nice, rss;
@@ -7260,6 +7261,7 @@ procfs_system_process_attributes (pid)
   Lisp_Object attrs = Qnil;
   Lisp_Object cmd_str, decoded_cmd, tem;
   struct gcpro gcpro1, gcpro2;
+  EMACS_INT uid_eint, gid_eint;
 
   CHECK_NUMBER_OR_FLOAT (pid);
   proc_id = FLOATP (pid) ? XFLOAT_DATA (pid) : XINT (pid);
@@ -7271,17 +7273,20 @@ procfs_system_process_attributes (pid)
 
   /* euid egid */
   uid = st.st_uid;
-  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid)), attrs);
+  /* Use of EMACS_INT stops GCC whining about limited range of data type.  */
+  uid_eint = uid;
+  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid_eint)), attrs);
   BLOCK_INPUT;
-  pw = (struct passwd *) getpwuid (uid);
+  pw = getpwuid (uid);
   UNBLOCK_INPUT;
   if (pw)
     attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
-  
+
   gid = st.st_gid;
-  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid)), attrs);
+  gid_eint = gid;
+  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid_eint)), attrs);
   BLOCK_INPUT;
-  gr = (struct group *) getgrgid (gid);
+  gr = getgrgid (gid);
   UNBLOCK_INPUT;
   if (gr)
     attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
@@ -7295,24 +7300,33 @@ procfs_system_process_attributes (pid)
       procbuf[nread] = '\0';
       p = procbuf;
 
-      p = strchr (p, '(') + 1;
-      q = strchr (p, ')');
-      /* comm */
-      if (q > p)
+      p = strchr (p, '(');
+      if (p != NULL)
        {
-         memcpy (cmd, p, q - p);
-         cmd[q - p] = '\0';
+         q = strrchr (p + 1, ')');
+         /* comm */
+         if (q != NULL)
+           {
+             cmd = p + 1;
+             cmdsize = q - cmd;
+           }
        }
       else
-       strcpy (cmd, "???");
+       q = NULL;
+      if (cmd == NULL)
+       {
+         cmd = "???";
+         cmdsize = 3;
+       }
       /* Command name is encoded in locale-coding-system; decode it.  */
-      cmd_str = make_unibyte_string (cmd, q ? q - p : 3);
+      cmd_str = make_unibyte_string (cmd, cmdsize);
       decoded_cmd = code_convert_string_norecord (cmd_str,
                                                  Vlocale_coding_system, 0);
       attrs = Fcons (Fcons (Qcomm, decoded_cmd), attrs);
 
       if (q)
        {
+         EMACS_INT ppid_eint, pgrp_eint, sess_eint, tpgid_eint, thcount_eint;
          p = q + 2;
          /* state ppid pgrp sess tty tpgid . minflt cminflt majflt cmajflt utime stime cutime cstime priority nice thcount . start vsize rss */
          sscanf (p, "%c %d %d %d %d %d %*u %lu %lu %lu %lu %Lu %Lu %Lu %Lu %ld %ld %d %*d %Lu %lu %ld",
@@ -7328,11 +7342,17 @@ procfs_system_process_attributes (pid)
            tem =  build_string (state_str);
            attrs = Fcons (Fcons (Qstate, tem), attrs);
          }
-         attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (ppid)), attrs);
-         attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (pgrp)), attrs);
-         attrs = Fcons (Fcons (Qsess, make_fixnum_or_float (sess)), attrs);
+         /* Stops GCC whining about limited range of data type.  */
+         ppid_eint = ppid;
+         pgrp_eint = pgrp;
+         sess_eint = sess;
+         tpgid_eint = tpgid;
+         thcount_eint = thcount;
+         attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (ppid_eint)), attrs);
+         attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (pgrp_eint)), attrs);
+         attrs = Fcons (Fcons (Qsess, make_fixnum_or_float (sess_eint)), attrs);
          attrs = Fcons (Fcons (Qttname, procfs_ttyname (tty)), attrs);
-         attrs = Fcons (Fcons (Qtpgid, make_fixnum_or_float (tpgid)), attrs);
+         attrs = Fcons (Fcons (Qtpgid, make_fixnum_or_float (tpgid_eint)), attrs);
          attrs = Fcons (Fcons (Qminflt, make_fixnum_or_float (minflt)), attrs);
          attrs = Fcons (Fcons (Qmajflt, make_fixnum_or_float (majflt)), attrs);
          attrs = Fcons (Fcons (Qcminflt, make_fixnum_or_float (cminflt)), attrs);
@@ -7354,7 +7374,7 @@ procfs_system_process_attributes (pid)
                         attrs);
          attrs = Fcons (Fcons (Qpri, make_number (priority)), attrs);
          attrs = Fcons (Fcons (Qnice, make_number (nice)), attrs);
-         attrs = Fcons (Fcons (Qthcount, make_fixnum_or_float (thcount)), attrs);
+         attrs = Fcons (Fcons (Qthcount, make_fixnum_or_float (thcount_eint)), attrs);
          EMACS_GET_TIME (tnow);
          get_up_time (&sec, &usec);
          EMACS_SET_SECS (telapsed, sec);
@@ -7385,7 +7405,9 @@ procfs_system_process_attributes (pid)
                         attrs);
          time_from_jiffies (utime + stime, clocks_per_sec, &sec, &usec);
          pcpu = (sec + usec / 1000000.0) / (EMACS_SECS (telapsed) + EMACS_USECS (telapsed) / 1000000.0);
-         attrs = Fcons (Fcons (Qpcpu, make_float (pcpu)), attrs);
+         if (pcpu > 1.0)
+           pcpu = 1.0;
+         attrs = Fcons (Fcons (Qpcpu, make_float (100 * pcpu)), attrs);
          pmem = 4.0 * 100 * rss / procfs_get_total_memory ();
          if (pmem > 100)
            pmem = 100;
@@ -7400,18 +7422,26 @@ procfs_system_process_attributes (pid)
   fd = emacs_open (fn, O_RDONLY, 0);
   if (fd >= 0)
     {
-      for (cmdsize = 0; emacs_read (fd, (char *)&c, 1) == 1; cmdsize++)
+      for (cmdline_size = 0; emacs_read (fd, &c, 1) == 1; cmdline_size++)
        {
          if (isspace (c) || c == '\\')
-           cmdsize++;  /* for later quoting, see below */
+           cmdline_size++;     /* for later quoting, see below */
        }
-      if (cmdsize)
+      if (cmdline_size)
        {
-         cmdline = xmalloc (cmdsize + 1);
+         cmdline = xmalloc (cmdline_size + 1);
          lseek (fd, 0L, SEEK_SET);
          cmdline[0] = '\0';
-         if ((nread = read (fd, cmdline, cmdsize)) >= 0)
+         if ((nread = read (fd, cmdline, cmdline_size)) >= 0)
            cmdline[nread++] = '\0';
+         else
+           {
+             /* Assigning zero to `nread' makes us skip the following
+                two loops, assign zero to cmdline_size, and enter the
+                following `if' clause that handles unknown command
+                lines.  */
+             nread = 0;
+           }
          /* We don't want trailing null characters.  */
          for (p = cmdline + nread - 1; p > cmdline && !*p; p--)
            nread--;
@@ -7427,18 +7457,22 @@ procfs_system_process_attributes (pid)
              else if (*p == '\0')
                *p = ' ';
            }
-         cmdsize = nread;
+         cmdline_size = nread;
        }
-      else
+      if (!cmdline_size)
        {
-         cmdsize = strlen (cmd) + 2;
-         cmdline = xmalloc (cmdsize + 1);
+         if (!cmd)
+           cmd = "???";
+         if (!cmdsize)
+           cmdsize = strlen (cmd);
+         cmdline_size = cmdsize + 2;
+         cmdline = xmalloc (cmdline_size + 1);
          strcpy (cmdline, "[");
-         strcat (strcat (cmdline, cmd), "]");
+         strcat (strncat (cmdline, cmd, cmdsize), "]");
        }
       emacs_close (fd);
       /* Command line is encoded in locale-coding-system; decode it.  */
-      cmd_str = make_unibyte_string (cmdline, cmdsize);
+      cmd_str = make_unibyte_string (cmdline, cmdline_size);
       decoded_cmd = code_convert_string_norecord (cmd_str,
                                                  Vlocale_coding_system, 0);
       xfree (cmdline);
@@ -7473,14 +7507,14 @@ DEFUN ("system-process-attributes", Fsystem_process_attributes,
 
 Value is an alist where each element is a cons cell of the form
 
-    \(ATTR . VALUE)
+    \(KEY . VALUE)
 
 If this functionality is unsupported, the value is nil.
 
 See `list-system-processes' for getting a list of all process IDs.
 
-The attributes that this function may return are listed below,
-together with the type of the associated value (in parentheses).
+The KEYs of the attributes that this function may return are listed
+below, together with the type of the associated VALUE (in parentheses).
 Not all platforms support all of these attributes; unsupported
 attributes will not appear in the returned alist.
 Unless explicitly indicated otherwise, numbers can have either
@@ -7921,7 +7955,7 @@ extern int frame_garbaged;
 extern EMACS_TIME timer_check ();
 extern int timers_run;
 
-Lisp_Object QCtype;
+Lisp_Object QCtype, QCname;
 
 /* As described above, except assuming that there are no subprocesses:
 
@@ -8201,14 +8235,14 @@ DEFUN ("system-process-attributes", Fsystem_process_attributes,
 
 Value is an alist where each element is a cons cell of the form
 
-    \(ATTR . VALUE)
+    \(KEY . VALUE)
 
 If this functionality is unsupported, the value is nil.
 
 See `list-system-processes' for getting a list of all process IDs.
 
-The attributes that this function may return are listed below,
-together with the type of the associated value (in parentheses).
+The KEYs of the attributes that this function may return are listed
+below, together with the type of the associated VALUE (in parentheses).
 Not all platforms support all of these attributes; unsupported
 attributes will not appear in the returned alist.
 Unless explicitly indicated otherwise, numbers can have either
@@ -8261,6 +8295,8 @@ syms_of_process ()
 {
   QCtype = intern (":type");
   staticpro (&QCtype);
+  QCname = intern (":name");
+  staticpro (&QCname);
 
   defsubr (&Sget_buffer_process);
   defsubr (&Sprocess_inherit_coding_system_flag);