unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Magnus Henoch <magnus.henoch@gmail.com>
To: 16579@debbugs.gnu.org
Subject: bug#16579: 24.3.50; Implement system_process_attributes on Darwin
Date: Tue, 28 Jan 2014 18:19:22 +0000	[thread overview]
Message-ID: <m2txcogead.fsf@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1176 bytes --]

Severity: wishlist
Tags: patch

The attached patch implements system_process_attributes on Darwin / Mac
OSX, and thus makes Proced show a list of processes on such systems.

The code is shared with FreeBSD, just like in list_system_processes,
though the differences between the two systems are so numerous that I'm
not sure if it's better this way or having a separate function for
Darwin.

This patch just "picks the low-hanging fruit", using the data that is
available in the kinfo_proc struct in a similar format to FreeBSD's.
Thus there are many things missing, in particular memory and CPU usage,
and command line arguments.  For the latter, you're apparently supposed
to use the undocumented KERN_PROCARGS and KERN_PROCARGS2 sysctls, but I
couldn't figure out how to get non-garbage from the result.

I haven't verified that this code still works on FreeBSD.

Regards,
Magnus

In GNU Emacs 24.3.50.15 (i386-apple-darwin12.5.0, NS apple-appkit-1187.40)
 of 2014-01-27 on poki-sona.local
Repository revision: 
Windowing system distributor `Apple', version 10.3.1187
Configured using:
 `configure --with-ns --without-rsvg --without-gsettings
 --without-file-notification'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 6545 bytes --]

diff --git a/src/sysdep.c b/src/sysdep.c
index 6ec8eec..78e23a0 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -3238,7 +3238,7 @@ system_process_attributes (Lisp_Object pid)
   return attrs;
 }
 
-#elif defined __FreeBSD__
+#elif defined __FreeBSD__ || defined DARWIN_OS
 
 static struct timespec
 timeval_to_timespec (struct timeval t)
@@ -3264,7 +3264,12 @@ system_process_attributes (Lisp_Object pid)
   char *ttyname;
   size_t len;
   char args[MAXPATHLEN];
+  struct timeval starttime;
   struct timespec t, now;
+  struct rusage *rusage;
+  dev_t tdev;
+  uid_t uid;
+  gid_t gid;
 
   int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID};
   struct kinfo_proc proc;
@@ -3283,30 +3288,52 @@ system_process_attributes (Lisp_Object pid)
 
   GCPRO2 (attrs, decoded_comm);
 
-  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (proc.ki_uid)), attrs);
+#ifdef DARWIN_OS
+  uid = proc.kp_eproc.e_ucred.cr_uid;
+#else
+  uid = proc.ki_uid;
+#endif
+  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid)), attrs);
 
   block_input ();
-  pw = getpwuid (proc.ki_uid);
+  pw = getpwuid (uid);
   unblock_input ();
   if (pw)
     attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
 
-  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (proc.ki_svgid)), attrs);
+#ifdef DARWIN_OS
+  gid = proc.kp_eproc.e_pcred.p_svgid;
+#else
+  gid = proc.ki_svgid;
+#endif
+  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid)), attrs);
 
   block_input ();
-  gr = getgrgid (proc.ki_svgid);
+  gr = getgrgid (gid);
   unblock_input ();
   if (gr)
     attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
 
+#ifdef DARWIN_OS
+  decoded_comm = (code_convert_string_norecord
+		  (build_unibyte_string (proc.kp_proc.p_comm),
+		   Vlocale_coding_system, 0));
+#else
   decoded_comm = (code_convert_string_norecord
 		  (build_unibyte_string (proc.ki_comm),
 		   Vlocale_coding_system, 0));
+#endif
 
   attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
   {
     char state[2] = {'\0', '\0'};
-    switch (proc.ki_stat)
+    char stat;
+#ifdef DARWIN_OS
+    stat = proc.kp_proc.p_stat;
+#else
+    stat = proc.ki_stat;
+#endif
+    switch (stat)
       {
       case SRUN:
 	state[0] = 'R';
@@ -3316,9 +3343,11 @@ system_process_attributes (Lisp_Object pid)
 	state[0] = 'S';
 	break;
 
+#ifdef SLOCK
       case SLOCK:
 	state[0] = 'D';
 	break;
+#endif
 
       case SZOMB:
 	state[0] = 'Z';
@@ -3327,34 +3356,66 @@ system_process_attributes (Lisp_Object pid)
       case SSTOP:
 	state[0] = 'T';
 	break;
+
+#ifdef SIDL
+      case SIDL:
+	state[0] = 'I';
+	break;
+#endif
       }
     attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
   }
 
+#ifdef DARWIN_OS
+  attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (proc.kp_eproc.e_ppid)), attrs);
+  attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (proc.kp_eproc.e_pgid)), attrs);
+#else
   attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (proc.ki_ppid)), attrs);
   attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (proc.ki_pgid)), attrs);
   attrs = Fcons (Fcons (Qsess, make_fixnum_or_float (proc.ki_sid)),  attrs);
+#endif
 
+#ifdef DARWIN_OS
+  tdev = proc.kp_eproc.e_tdev;
+#else
+  tdev = proc.ki_tdev;
+#endif
   block_input ();
-  ttyname = proc.ki_tdev == NODEV ? NULL : devname (proc.ki_tdev, S_IFCHR);
+  ttyname = tdev == NODEV ? NULL : devname (tdev, S_IFCHR);
   unblock_input ();
   if (ttyname)
     attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
 
+#ifdef DARWIN_OS
+  attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.kp_eproc.e_tpgid)), attrs);
+#else
   attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.ki_tpgid)), attrs);
-  attrs = Fcons (Fcons (Qminflt,  make_fixnum_or_float (proc.ki_rusage.ru_minflt)), attrs);
-  attrs = Fcons (Fcons (Qmajflt,  make_fixnum_or_float (proc.ki_rusage.ru_majflt)), attrs);
+#endif
+
+#ifdef DARWIN_OS
+  rusage = proc.kp_proc.p_ru;
+#else
+  rusage = &proc.ki_rusage;
+#endif
+  if (rusage)
+    {
+      attrs = Fcons (Fcons (Qminflt,  make_fixnum_or_float (rusage->ru_minflt)), attrs);
+      attrs = Fcons (Fcons (Qmajflt,  make_fixnum_or_float (rusage->ru_majflt)), attrs);
+
+      attrs = Fcons (Fcons (Qutime, make_lisp_timeval (rusage->ru_utime)),
+		     attrs);
+      attrs = Fcons (Fcons (Qstime, make_lisp_timeval (rusage->ru_stime)),
+		     attrs);
+      t = timespec_add (timeval_to_timespec (rusage->ru_utime),
+			timeval_to_timespec (rusage->ru_stime));
+      attrs = Fcons (Fcons (Qtime, make_lisp_time (t)), attrs);
+    }
+#ifndef DARWIN_OS
   attrs = Fcons (Fcons (Qcminflt, make_number (proc.ki_rusage_ch.ru_minflt)), attrs);
   attrs = Fcons (Fcons (Qcmajflt, make_number (proc.ki_rusage_ch.ru_majflt)), attrs);
+#endif
 
-  attrs = Fcons (Fcons (Qutime, make_lisp_timeval (proc.ki_rusage.ru_utime)),
-		 attrs);
-  attrs = Fcons (Fcons (Qstime, make_lisp_timeval (proc.ki_rusage.ru_stime)),
-		 attrs);
-  t = timespec_add (timeval_to_timespec (proc.ki_rusage.ru_utime),
-		    timeval_to_timespec (proc.ki_rusage.ru_stime));
-  attrs = Fcons (Fcons (Qtime, make_lisp_time (t)), attrs);
-
+#ifndef DARWIN_OS
   attrs = Fcons (Fcons (Qcutime,
 			make_lisp_timeval (proc.ki_rusage_ch.ru_utime)),
 		 attrs);
@@ -3368,16 +3429,24 @@ system_process_attributes (Lisp_Object pid)
   attrs = Fcons (Fcons (Qthcount, make_fixnum_or_float (proc.ki_numthreads)),
 		 attrs);
   attrs = Fcons (Fcons (Qpri,   make_number (proc.ki_pri.pri_native)), attrs);
+#endif
+#ifdef DARWIN_OS
+  starttime = proc.kp_proc.p_starttime;
+  attrs = Fcons (Fcons (Qnice,  make_number (proc.kp_proc.p_nice)), attrs);
+#else
+  starttime = proc.ki_start;
   attrs = Fcons (Fcons (Qnice,  make_number (proc.ki_nice)), attrs);
-  attrs = Fcons (Fcons (Qstart, make_lisp_timeval (proc.ki_start)), attrs);
   attrs = Fcons (Fcons (Qvsize, make_number (proc.ki_size >> 10)), attrs);
   attrs = Fcons (Fcons (Qrss,   make_number (proc.ki_rssize * pagesize >> 10)),
 		 attrs);
+#endif
+  attrs = Fcons (Fcons (Qstart, make_lisp_timeval (starttime)), attrs);
 
   now = current_timespec ();
-  t = timespec_sub (now, timeval_to_timespec (proc.ki_start));
+  t = timespec_sub (now, timeval_to_timespec (starttime));
   attrs = Fcons (Fcons (Qetime, make_lisp_time (t)), attrs);
 
+#ifndef DARWIN_OS
   len = sizeof fscale;
   if (sysctlbyname ("kern.fscale", &fscale, &len, NULL, 0) == 0)
     {
@@ -3419,6 +3488,7 @@ system_process_attributes (Lisp_Object pid)
 
       attrs = Fcons (Fcons (Qargs, decoded_comm), attrs);
     }
+#endif
 
   UNGCPRO;
   return attrs;

             reply	other threads:[~2014-01-28 18:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-28 18:19 Magnus Henoch [this message]
2015-11-16 22:39 ` bug#16579: Nicer patch Magnus Henoch
2015-11-17  4:07   ` Ivan Andrus
2016-02-24  3:29 ` bug#16579: 24.3.50; Implement system_process_attributes on Darwin Lars Ingebrigtsen
2016-04-22 23:15   ` Steve Purcell
2016-04-23  0:53 ` Magnus Henoch
2016-04-23  3:05   ` Steve Purcell
2016-04-24 12:33     ` Lars Magne Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2txcogead.fsf@mail.gmail.com \
    --to=magnus.henoch@gmail.com \
    --cc=16579@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).