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: Nicer patch Date: Mon, 16 Nov 2015 22:39:19 +0000 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1447721942 28035 80.91.229.3 (17 Nov 2015 00:59:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 17 Nov 2015 00:59:02 +0000 (UTC) To: 16579@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Nov 17 01:58:50 2015 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 1ZyUbS-0000Od-Gy for geb-bug-gnu-emacs@m.gmane.org; Tue, 17 Nov 2015 01:58:34 +0100 Original-Received: from localhost ([::1]:53694 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZyUbS-0003bN-2J for geb-bug-gnu-emacs@m.gmane.org; Mon, 16 Nov 2015 19:58:34 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54431) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZySRS-0001ck-E6 for bug-gnu-emacs@gnu.org; Mon, 16 Nov 2015 17:40:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZySRP-0006tr-74 for bug-gnu-emacs@gnu.org; Mon, 16 Nov 2015 17:40:06 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:50886) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZySRP-0006tY-2t for bug-gnu-emacs@gnu.org; Mon, 16 Nov 2015 17:40:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1ZySRO-0005ZO-I5 for bug-gnu-emacs@gnu.org; Mon, 16 Nov 2015 17:40:02 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: Resent-From: Magnus Henoch Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 16 Nov 2015 22:40:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 16579 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 16579-submit@debbugs.gnu.org id=B16579.144771356521363 (code B ref 16579); Mon, 16 Nov 2015 22:40:02 +0000 Original-Received: (at 16579) by debbugs.gnu.org; 16 Nov 2015 22:39:25 +0000 Original-Received: from localhost ([127.0.0.1]:40594 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZySQm-0005YV-W8 for submit@debbugs.gnu.org; Mon, 16 Nov 2015 17:39:25 -0500 Original-Received: from mail-wm0-f48.google.com ([74.125.82.48]:33399) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1ZySQk-0005YM-Dp for 16579@debbugs.gnu.org; Mon, 16 Nov 2015 17:39:23 -0500 Original-Received: by wmec201 with SMTP id c201so200569925wme.0 for <16579@debbugs.gnu.org>; Mon, 16 Nov 2015 14:39:21 -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=G+ff7Z4SnTG1+YPPj+oJbWqzXmPZ/5cvj/ByHAN6y+0=; b=rk7hFabKDKYWO1AB2VV3NkAF/sUMtLRIlQXWgVo3eg2VaSavmqfv9+fswBjaV5hcyA u6iPIqgSqclObufRu+lSqsToWAWCCtEmwFB76yj/W6pGLh9hqKqb9JWtIlBoUbwN0Aw0 h2R2e5A+2Fxj7ncEfGgYiWz1roqwsyBWv9w46w5U7nNlov0Xc6MXecRpy2cQ/jicMeOS +NJ/KUKO5T+MNG7Nxankyu/Lfi7lO0qqCdmeFUBrs93uc9DkVAas5rWKjepkiXlAy0Xk R94oS2yQDeFcs0COr5nebk/96Cfp9HRWy5/7VYOtNBP8KBkA9B5m8C45IyUvVCvfYnJA GZrw== X-Received: by 10.194.200.134 with SMTP id js6mr37818005wjc.114.1447713561825; Mon, 16 Nov 2015 14:39:21 -0800 (PST) Original-Received: from poki-sona-sin.local ([217.155.37.242]) by smtp.gmail.com with ESMTPSA id cv3sm35321558wjc.20.2015.11.16.14.39.20 for <16579@debbugs.gnu.org> (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Nov 2015 14:39:20 -0800 (PST) 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: 208.118.235.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:108796 Archived-At: --=-=-= Content-Type: text/plain; format=flowed 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 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Implement-process-attributes-for-Darwin.patch >From 6ab30d39b18027e141000446561d7a211776c681 Mon Sep 17 00:00:00 2001 From: Magnus Henoch 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 --=-=-=--