all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#16579: 24.3.50; Implement system_process_attributes on Darwin
@ 2014-01-28 18:19 Magnus Henoch
  2015-11-16 22:39 ` bug#16579: Nicer patch Magnus Henoch
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Magnus Henoch @ 2014-01-28 18:19 UTC (permalink / raw)
  To: 16579

[-- 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;

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#16579: Nicer patch
  2014-01-28 18:19 bug#16579: 24.3.50; Implement system_process_attributes on Darwin Magnus Henoch
@ 2015-11-16 22:39 ` 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-23  0:53 ` Magnus Henoch
  2 siblings, 1 reply; 8+ messages in thread
From: Magnus Henoch @ 2015-11-16 22:39 UTC (permalink / raw)
  To: 16579

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

So I had another look at this, and realised that the patch looks 
pretty ugly, being all interleaved with the FreeBSD code... 
Here's another patch, that just adds a new section for Darwin.  I 
feel much better about it now.

Any thoughts or objections?

Regards,
Magnus

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Implement-process-attributes-for-Darwin.patch --]
[-- Type: text/x-patch, Size: 4447 bytes --]

From 6ab30d39b18027e141000446561d7a211776c681 Mon Sep 17 00:00:00 2001
From: Magnus Henoch <magnus.henoch@gmail.com>
Date: Sun, 15 Nov 2015 02:05:00 +0000
Subject: [PATCH] Implement process-attributes for Darwin

Mostly copied from the FreeBSD implementation, but it's different enough
to warrant a separate preprocessor if clause.
---
 src/sysdep.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 139 insertions(+)

diff --git a/src/sysdep.c b/src/sysdep.c
index 91036f0..f2b2b16 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -3512,6 +3512,145 @@ system_process_attributes (Lisp_Object pid)
   return attrs;
 }
 
+#elif defined DARWIN_OS
+
+static struct timespec
+timeval_to_timespec (struct timeval t)
+{
+  return make_timespec (t.tv_sec, t.tv_usec * 1000);
+}
+
+static Lisp_Object
+make_lisp_timeval (struct timeval t)
+{
+  return make_lisp_time (timeval_to_timespec (t));
+}
+
+Lisp_Object
+system_process_attributes (Lisp_Object pid)
+{
+  int proc_id;
+  int pagesize = getpagesize ();
+  unsigned long npages;
+  int fscale;
+  struct passwd *pw;
+  struct group  *gr;
+  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;
+  size_t proclen = sizeof proc;
+
+  struct gcpro gcpro1, gcpro2;
+  Lisp_Object attrs = Qnil;
+  Lisp_Object decoded_comm;
+
+  CHECK_NUMBER_OR_FLOAT (pid);
+  CONS_TO_INTEGER (pid, int, proc_id);
+  mib[3] = proc_id;
+
+  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
+    return attrs;
+
+  GCPRO2 (attrs, decoded_comm);
+
+  uid = proc.kp_eproc.e_ucred.cr_uid;
+  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid)), attrs);
+
+  block_input ();
+  pw = getpwuid (uid);
+  unblock_input ();
+  if (pw)
+    attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
+
+  gid = proc.kp_eproc.e_pcred.p_svgid;
+  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid)), attrs);
+
+  block_input ();
+  gr = getgrgid (gid);
+  unblock_input ();
+  if (gr)
+    attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
+
+  decoded_comm = (code_convert_string_norecord
+		  (build_unibyte_string (proc.kp_proc.p_comm),
+		   Vlocale_coding_system, 0));
+
+  attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
+  {
+    char state[2] = {'\0', '\0'};
+    switch (proc.kp_proc.p_stat)
+      {
+      case SRUN:
+	state[0] = 'R';
+	break;
+
+      case SSLEEP:
+	state[0] = 'S';
+	break;
+
+      case SZOMB:
+	state[0] = 'Z';
+	break;
+
+      case SSTOP:
+	state[0] = 'T';
+	break;
+
+      case SIDL:
+	state[0] = 'I';
+	break;
+      }
+    attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
+  }
+
+  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);
+
+  tdev = proc.kp_eproc.e_tdev;
+  block_input ();
+  ttyname = tdev == NODEV ? NULL : devname (tdev, S_IFCHR);
+  unblock_input ();
+  if (ttyname)
+    attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
+
+  attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.kp_eproc.e_tpgid)), attrs);
+
+  rusage = proc.kp_proc.p_ru;
+  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);
+    }
+
+  starttime = proc.kp_proc.p_starttime;
+  attrs = Fcons (Fcons (Qnice,  make_number (proc.kp_proc.p_nice)), attrs);
+  attrs = Fcons (Fcons (Qstart, make_lisp_timeval (starttime)), attrs);
+
+  now = current_timespec ();
+  t = timespec_sub (now, timeval_to_timespec (starttime));
+  attrs = Fcons (Fcons (Qetime, make_lisp_time (t)), attrs);
+
+  UNGCPRO;
+  return attrs;
+}
+
 /* The WINDOWSNT implementation is in w32.c.
    The MSDOS implementation is in dosfns.c.  */
 #elif !defined (WINDOWSNT) && !defined (MSDOS)
-- 
2.2.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#16579: Nicer patch
  2015-11-16 22:39 ` bug#16579: Nicer patch Magnus Henoch
@ 2015-11-17  4:07   ` Ivan Andrus
  0 siblings, 0 replies; 8+ messages in thread
From: Ivan Andrus @ 2015-11-17  4:07 UTC (permalink / raw)
  To: Magnus Henoch; +Cc: 16579

I’ve been using your previous patch for a while and I think it would be great to have this feature in.  I’m a very casual contributor, so I can’t say go ahead, but I think it would be great to have.  I haven’t found any problems with it (though I don’t use proced all that often—partly because it never worked until I found this patch).

-Ivan

> On Nov 16, 2015, at 3:39 PM, Magnus Henoch <magnus.henoch@gmail.com> wrote:
> 
> So I had another look at this, and realised that the patch looks pretty ugly, being all interleaved with the FreeBSD code... Here's another patch, that just adds a new section for Darwin.  I feel much better about it now.
> 
> Any thoughts or objections?
> 
> Regards,
> Magnus
> From 6ab30d39b18027e141000446561d7a211776c681 Mon Sep 17 00:00:00 2001
> From: Magnus Henoch <magnus.henoch@gmail.com>
> Date: Sun, 15 Nov 2015 02:05:00 +0000
> Subject: [PATCH] Implement process-attributes for Darwin
> 
> Mostly copied from the FreeBSD implementation, but it's different enough
> to warrant a separate preprocessor if clause.
> ---
> src/sysdep.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 139 insertions(+)
> 
> diff --git a/src/sysdep.c b/src/sysdep.c
> index 91036f0..f2b2b16 100644
> --- a/src/sysdep.c
> +++ b/src/sysdep.c
> @@ -3512,6 +3512,145 @@ system_process_attributes (Lisp_Object pid)
>   return attrs;
> }
> 
> +#elif defined DARWIN_OS
> +
> +static struct timespec
> +timeval_to_timespec (struct timeval t)
> +{
> +  return make_timespec (t.tv_sec, t.tv_usec * 1000);
> +}
> +
> +static Lisp_Object
> +make_lisp_timeval (struct timeval t)
> +{
> +  return make_lisp_time (timeval_to_timespec (t));
> +}
> +
> +Lisp_Object
> +system_process_attributes (Lisp_Object pid)
> +{
> +  int proc_id;
> +  int pagesize = getpagesize ();
> +  unsigned long npages;
> +  int fscale;
> +  struct passwd *pw;
> +  struct group  *gr;
> +  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;
> +  size_t proclen = sizeof proc;
> +
> +  struct gcpro gcpro1, gcpro2;
> +  Lisp_Object attrs = Qnil;
> +  Lisp_Object decoded_comm;
> +
> +  CHECK_NUMBER_OR_FLOAT (pid);
> +  CONS_TO_INTEGER (pid, int, proc_id);
> +  mib[3] = proc_id;
> +
> +  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
> +    return attrs;
> +
> +  GCPRO2 (attrs, decoded_comm);
> +
> +  uid = proc.kp_eproc.e_ucred.cr_uid;
> +  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid)), attrs);
> +
> +  block_input ();
> +  pw = getpwuid (uid);
> +  unblock_input ();
> +  if (pw)
> +    attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
> +
> +  gid = proc.kp_eproc.e_pcred.p_svgid;
> +  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid)), attrs);
> +
> +  block_input ();
> +  gr = getgrgid (gid);
> +  unblock_input ();
> +  if (gr)
> +    attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
> +
> +  decoded_comm = (code_convert_string_norecord
> +		  (build_unibyte_string (proc.kp_proc.p_comm),
> +		   Vlocale_coding_system, 0));
> +
> +  attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
> +  {
> +    char state[2] = {'\0', '\0'};
> +    switch (proc.kp_proc.p_stat)
> +      {
> +      case SRUN:
> +	state[0] = 'R';
> +	break;
> +
> +      case SSLEEP:
> +	state[0] = 'S';
> +	break;
> +
> +      case SZOMB:
> +	state[0] = 'Z';
> +	break;
> +
> +      case SSTOP:
> +	state[0] = 'T';
> +	break;
> +
> +      case SIDL:
> +	state[0] = 'I';
> +	break;
> +      }
> +    attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
> +  }
> +
> +  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);
> +
> +  tdev = proc.kp_eproc.e_tdev;
> +  block_input ();
> +  ttyname = tdev == NODEV ? NULL : devname (tdev, S_IFCHR);
> +  unblock_input ();
> +  if (ttyname)
> +    attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
> +
> +  attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.kp_eproc.e_tpgid)), attrs);
> +
> +  rusage = proc.kp_proc.p_ru;
> +  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);
> +    }
> +
> +  starttime = proc.kp_proc.p_starttime;
> +  attrs = Fcons (Fcons (Qnice,  make_number (proc.kp_proc.p_nice)), attrs);
> +  attrs = Fcons (Fcons (Qstart, make_lisp_timeval (starttime)), attrs);
> +
> +  now = current_timespec ();
> +  t = timespec_sub (now, timeval_to_timespec (starttime));
> +  attrs = Fcons (Fcons (Qetime, make_lisp_time (t)), attrs);
> +
> +  UNGCPRO;
> +  return attrs;
> +}
> +
> /* The WINDOWSNT implementation is in w32.c.
>    The MSDOS implementation is in dosfns.c.  */
> #elif !defined (WINDOWSNT) && !defined (MSDOS)
> -- 
> 2.2.1
> 






^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16579: 24.3.50; Implement system_process_attributes on Darwin
  2014-01-28 18:19 bug#16579: 24.3.50; Implement system_process_attributes on Darwin Magnus Henoch
  2015-11-16 22:39 ` bug#16579: Nicer patch Magnus Henoch
@ 2016-02-24  3:29 ` Lars Ingebrigtsen
  2016-04-22 23:15   ` Steve Purcell
  2016-04-23  0:53 ` Magnus Henoch
  2 siblings, 1 reply; 8+ messages in thread
From: Lars Ingebrigtsen @ 2016-02-24  3:29 UTC (permalink / raw)
  To: Magnus Henoch; +Cc: 16579

Magnus Henoch <magnus.henoch@gmail.com> writes:

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

[...]

> So I had another look at this, and realised that the patch looks
> pretty ugly, being all interleaved with the FreeBSD code... Here's
> another patch, that just adds a new section for Darwin.  I feel much
> better about it now.
>
> Any thoughts or objections?

It looks fine to me, and that sounds like useful functionality, but I
don't have access to any Darwin systems, so I can't even compile it.
Could somebody try applying it and see whether it (still) works?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16579: 24.3.50; Implement system_process_attributes on Darwin
  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
  0 siblings, 0 replies; 8+ messages in thread
From: Steve Purcell @ 2016-04-22 23:15 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Magnus Henoch, 16579

I'm on OS X 10.11.4, and the patch applies cleanly for me but doesn't
compile:


  CC       sysdep.o
sysdep.c:3607:16: error: variable has incomplete type 'struct gcpro'
  struct gcpro gcpro1, gcpro2;
               ^
sysdep.c:3607:10: note: forward declaration of 'struct gcpro'
  struct gcpro gcpro1, gcpro2;
         ^
sysdep.c:3607:24: error: variable has incomplete type 'struct gcpro'
  struct gcpro gcpro1, gcpro2;
                       ^
sysdep.c:3607:10: note: forward declaration of 'struct gcpro'
  struct gcpro gcpro1, gcpro2;
         ^
sysdep.c:3618:3: warning: implicit declaration of function 'GCPRO2' is invalid in C99
      [-Wimplicit-function-declaration]
  GCPRO2 (attrs, decoded_comm);
  ^
sysdep.c:3705:3: error: use of undeclared identifier 'UNGCPRO'
  UNGCPRO;
  ^
1 warning and 3 errors generated.
make[2]: *** [sysdep.o] Error 1
make[1]: *** [src] Error 2
make: *** [bootstrap] Error 2





^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16579: 24.3.50; Implement system_process_attributes on Darwin
  2014-01-28 18:19 bug#16579: 24.3.50; Implement system_process_attributes on Darwin Magnus Henoch
  2015-11-16 22:39 ` bug#16579: Nicer patch Magnus Henoch
  2016-02-24  3:29 ` bug#16579: 24.3.50; Implement system_process_attributes on Darwin Lars Ingebrigtsen
@ 2016-04-23  0:53 ` Magnus Henoch
  2016-04-23  3:05   ` Steve Purcell
  2 siblings, 1 reply; 8+ messages in thread
From: Magnus Henoch @ 2016-04-23  0:53 UTC (permalink / raw)
  To: Steve Purcell; +Cc: Lars Ingebrigtsen, Magnus Henoch, 16579

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

Steve Purcell <steve@sanityinc.com> writes:

> I'm on OS X 10.11.4, and the patch applies cleanly for me but 
> doesn't compile:
>  
>   CC       sysdep.o
> sysdep.c:3607:16: error: variable has incomplete type 'struct 
> gcpro'
>   struct gcpro gcpro1, gcpro2;

Right, here's a new version, without the gcpro stuff.  If I 
understand correctly, those things were removed since I wrote the 
first version of the patch, and are not necessary any more.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Implement-process-attributes-for-Darwin.patch --]
[-- Type: text/x-patch, Size: 4368 bytes --]

From 0bc6119d618bcfec99ab3ef16739472f8628abe4 Mon Sep 17 00:00:00 2001
From: Magnus Henoch <magnus.henoch@gmail.com>
Date: Sun, 15 Nov 2015 02:05:00 +0000
Subject: [PATCH] Implement process-attributes for Darwin

Mostly copied from the FreeBSD implementation, but it's different enough
to warrant a separate preprocessor if clause.
---
 src/sysdep.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 135 insertions(+)

diff --git a/src/sysdep.c b/src/sysdep.c
index 3faa696..50e0b01 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -3567,6 +3567,141 @@ system_process_attributes (Lisp_Object pid)
   return attrs;
 }
 
+#elif defined DARWIN_OS
+
+static struct timespec
+timeval_to_timespec (struct timeval t)
+{
+  return make_timespec (t.tv_sec, t.tv_usec * 1000);
+}
+
+static Lisp_Object
+make_lisp_timeval (struct timeval t)
+{
+  return make_lisp_time (timeval_to_timespec (t));
+}
+
+Lisp_Object
+system_process_attributes (Lisp_Object pid)
+{
+  int proc_id;
+  int pagesize = getpagesize ();
+  unsigned long npages;
+  int fscale;
+  struct passwd *pw;
+  struct group  *gr;
+  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;
+  size_t proclen = sizeof proc;
+
+  Lisp_Object attrs = Qnil;
+  Lisp_Object decoded_comm;
+
+  CHECK_NUMBER_OR_FLOAT (pid);
+  CONS_TO_INTEGER (pid, int, proc_id);
+  mib[3] = proc_id;
+
+  if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
+    return attrs;
+
+  uid = proc.kp_eproc.e_ucred.cr_uid;
+  attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid)), attrs);
+
+  block_input ();
+  pw = getpwuid (uid);
+  unblock_input ();
+  if (pw)
+    attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
+
+  gid = proc.kp_eproc.e_pcred.p_svgid;
+  attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid)), attrs);
+
+  block_input ();
+  gr = getgrgid (gid);
+  unblock_input ();
+  if (gr)
+    attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
+
+  decoded_comm = (code_convert_string_norecord
+		  (build_unibyte_string (proc.kp_proc.p_comm),
+		   Vlocale_coding_system, 0));
+
+  attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
+  {
+    char state[2] = {'\0', '\0'};
+    switch (proc.kp_proc.p_stat)
+      {
+      case SRUN:
+	state[0] = 'R';
+	break;
+
+      case SSLEEP:
+	state[0] = 'S';
+	break;
+
+      case SZOMB:
+	state[0] = 'Z';
+	break;
+
+      case SSTOP:
+	state[0] = 'T';
+	break;
+
+      case SIDL:
+	state[0] = 'I';
+	break;
+      }
+    attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
+  }
+
+  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);
+
+  tdev = proc.kp_eproc.e_tdev;
+  block_input ();
+  ttyname = tdev == NODEV ? NULL : devname (tdev, S_IFCHR);
+  unblock_input ();
+  if (ttyname)
+    attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
+
+  attrs = Fcons (Fcons (Qtpgid,   make_fixnum_or_float (proc.kp_eproc.e_tpgid)), attrs);
+
+  rusage = proc.kp_proc.p_ru;
+  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);
+    }
+
+  starttime = proc.kp_proc.p_starttime;
+  attrs = Fcons (Fcons (Qnice,  make_number (proc.kp_proc.p_nice)), attrs);
+  attrs = Fcons (Fcons (Qstart, make_lisp_timeval (starttime)), attrs);
+
+  now = current_timespec ();
+  t = timespec_sub (now, timeval_to_timespec (starttime));
+  attrs = Fcons (Fcons (Qetime, make_lisp_time (t)), attrs);
+
+  return attrs;
+}
+
 /* The WINDOWSNT implementation is in w32.c.
    The MSDOS implementation is in dosfns.c.  */
 #elif !defined (WINDOWSNT) && !defined (MSDOS)
-- 
2.8.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* bug#16579: 24.3.50; Implement system_process_attributes on Darwin
  2016-04-23  0:53 ` Magnus Henoch
@ 2016-04-23  3:05   ` Steve Purcell
  2016-04-24 12:33     ` Lars Magne Ingebrigtsen
  0 siblings, 1 reply; 8+ messages in thread
From: Steve Purcell @ 2016-04-23  3:05 UTC (permalink / raw)
  To: Magnus Henoch; +Cc: Lars Ingebrigtsen, 16579

Just tried that revised patch and it works great for me! Would be great if this could be merged.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* bug#16579: 24.3.50; Implement system_process_attributes on Darwin
  2016-04-23  3:05   ` Steve Purcell
@ 2016-04-24 12:33     ` Lars Magne Ingebrigtsen
  0 siblings, 0 replies; 8+ messages in thread
From: Lars Magne Ingebrigtsen @ 2016-04-24 12:33 UTC (permalink / raw)
  To: Steve Purcell; +Cc: Magnus Henoch, 16579

Steve Purcell <steve@sanityinc.com> writes:

> Just tried that revised patch and it works great for me! Would be
> great if this could be merged.

Ok; applied to the trunk.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-04-24 12:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-28 18:19 bug#16579: 24.3.50; Implement system_process_attributes on Darwin Magnus Henoch
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

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.