From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Magnus Henoch Newsgroups: gmane.emacs.bugs Subject: bug#16579: 24.3.50; Implement system_process_attributes on Darwin Date: Tue, 28 Jan 2014 18:19:22 +0000 Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1390933221 27384 80.91.229.3 (28 Jan 2014 18:20:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 28 Jan 2014 18:20:21 +0000 (UTC) To: 16579@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jan 28 19:20:28 2014 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1W8DGt-0006cA-I7 for geb-bug-gnu-emacs@m.gmane.org; Tue, 28 Jan 2014 19:20:27 +0100 Original-Received: from localhost ([::1]:38874 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8DGs-0000VN-K5 for geb-bug-gnu-emacs@m.gmane.org; Tue, 28 Jan 2014 13:20:26 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59933) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8DGf-0000Tr-I5 for bug-gnu-emacs@gnu.org; Tue, 28 Jan 2014 13:20:22 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W8DGU-0006E5-UJ for bug-gnu-emacs@gnu.org; Tue, 28 Jan 2014 13:20:13 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:53730) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8DGU-0006Du-Ql for bug-gnu-emacs@gnu.org; Tue, 28 Jan 2014 13:20:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1W8DGU-0007E7-9T for bug-gnu-emacs@gnu.org; Tue, 28 Jan 2014 13:20:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Magnus Henoch Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 28 Jan 2014 18:20:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 16579 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.139093320027765 (code B ref -1); Tue, 28 Jan 2014 18:20:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 28 Jan 2014 18:20:00 +0000 Original-Received: from localhost ([127.0.0.1]:39516 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1W8DGR-0007Dk-LQ for submit@debbugs.gnu.org; Tue, 28 Jan 2014 13:20:00 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:58100) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1W8DGO-0007DT-4u for submit@debbugs.gnu.org; Tue, 28 Jan 2014 13:19:57 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W8DGC-000658-FN for submit@debbugs.gnu.org; Tue, 28 Jan 2014 13:19:52 -0500 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:48666) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8DGC-000654-Bl for submit@debbugs.gnu.org; Tue, 28 Jan 2014 13:19:44 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59785) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8DG3-0000FF-Th for bug-gnu-emacs@gnu.org; Tue, 28 Jan 2014 13:19:44 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W8DFv-00063A-E4 for bug-gnu-emacs@gnu.org; Tue, 28 Jan 2014 13:19:35 -0500 Original-Received: from mail-wg0-x234.google.com ([2a00:1450:400c:c00::234]:60460) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8DFv-000631-3n for bug-gnu-emacs@gnu.org; Tue, 28 Jan 2014 13:19:27 -0500 Original-Received: by mail-wg0-f52.google.com with SMTP id b13so1527769wgh.7 for ; Tue, 28 Jan 2014 10:19:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:mime-version:content-type; bh=YYio/kGDbhaQ8rAnw8aM0fyh0sjPb6HfJM20Wu25v6o=; b=J5O1wYGNgXBoXrIAvEYx0qANva6wn9dDXfXvgSu8ynLitXfL4BUMAPGSIYjmw0ZyNR 8a3nNQZX80Bv6UIyLGGY4NT8o458h+KIyC44WKv3bHwor/3+kfy8cPrlO7NFkR2TMWhU BEB2IO1tKwZ0YPDXoOz/FeZJ2FkD5j0BrLKDQ+jf+OvbmUByj/pBjvjZTZeS/xD2q9KO O/2zaJU4fMZceW7SgZN7qRz9PUFRj0F3xijj8yeYXfPncSP0EkUtW6zUYZ9vOp8zq6Dk RGrdz1nlNgjQYKhJlJpe1Vl+lI5KlSHXByPnNZ8XATf+d3058sGIc3tEdiR9fe5XUfeK vEag== X-Received: by 10.194.119.168 with SMTP id kv8mr1934076wjb.41.1390933166045; Tue, 28 Jan 2014 10:19:26 -0800 (PST) Original-Received: from poki-sona.local ([212.169.34.114]) by mx.google.com with ESMTPSA id bj3sm34533248wjb.14.2014.01.28.10.19.23 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Jan 2014 10:19:24 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:84182 Archived-At: --=-=-= Content-Type: text/plain 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' --=-=-= Content-Type: text/x-patch Content-Disposition: inline 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; --=-=-=--