aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/sysdep.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/src/sysdep.c b/src/sysdep.c
index 6ede06b1aa3..a49f1775ebd 100644
--- a/src/sysdep.c
+++ b/src/sysdep.c
@@ -53,6 +53,10 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
53# include <sys/sysctl.h> 53# include <sys/sysctl.h>
54#endif 54#endif
55 55
56#if defined __OpenBSD__
57# include <sys/proc.h>
58#endif
59
56#ifdef DARWIN_OS 60#ifdef DARWIN_OS
57# include <libproc.h> 61# include <libproc.h>
58#endif 62#endif
@@ -2972,6 +2976,14 @@ make_lisp_timeval (struct timeval t)
2972 return make_lisp_time (timeval_to_timespec (t)); 2976 return make_lisp_time (timeval_to_timespec (t));
2973} 2977}
2974 2978
2979#elif defined __OpenBSD__
2980
2981static Lisp_Object
2982make_lisp_timeval (long sec, long usec)
2983{
2984 return make_lisp_time(make_timespec(sec, usec * 1000));
2985}
2986
2975#endif 2987#endif
2976 2988
2977#ifdef GNU_LINUX 2989#ifdef GNU_LINUX
@@ -3661,6 +3673,189 @@ system_process_attributes (Lisp_Object pid)
3661 return attrs; 3673 return attrs;
3662} 3674}
3663 3675
3676#elif defined __OpenBSD__
3677
3678Lisp_Object
3679system_process_attributes (Lisp_Object pid)
3680{
3681 int proc_id, nentries, fscale, i;
3682 int pagesize = getpagesize ();
3683 int mib[6];
3684 size_t len;
3685 double pct;
3686 char *ttyname, args[ARG_MAX];
3687 struct kinfo_proc proc;
3688 struct passwd *pw;
3689 struct group *gr;
3690 struct timespec t;
3691 struct uvmexp uvmexp;
3692
3693 Lisp_Object attrs = Qnil;
3694 Lisp_Object decoded_comm;
3695
3696 CHECK_NUMBER (pid);
3697 CONS_TO_INTEGER (pid, int, proc_id);
3698
3699 len = sizeof proc;
3700 mib[0] = CTL_KERN;
3701 mib[1] = KERN_PROC;
3702 mib[2] = KERN_PROC_PID;
3703 mib[3] = proc_id;
3704 mib[4] = len;
3705 mib[5] = 1;
3706 if (sysctl (mib, 6, &proc, &len, NULL, 0) != 0)
3707 return attrs;
3708
3709 attrs = Fcons (Fcons (Qeuid, INT_TO_INTEGER (proc.p_uid)), attrs);
3710
3711 block_input ();
3712 pw = getpwuid (proc.p_uid);
3713 unblock_input ();
3714 if (pw)
3715 attrs = Fcons (Fcons (Quser, build_string(pw->pw_name)), attrs);
3716
3717 attrs = Fcons (Fcons (Qegid, INT_TO_INTEGER(proc.p_svgid)), attrs);
3718
3719 block_input ();
3720 gr = getgrgid (proc.p_svgid);
3721 unblock_input ();
3722 if (gr)
3723 attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
3724
3725 AUTO_STRING (comm, proc.p_comm);
3726 decoded_comm = code_convert_string_norecord (comm, Vlocale_coding_system, 0);
3727 attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
3728
3729 {
3730 char state[2] = {'\0', '\0'};
3731 switch (proc.p_stat) {
3732 case SIDL:
3733 state[0] = 'I';
3734 break;
3735 case SRUN:
3736 state[0] = 'R';
3737 break;
3738 case SSLEEP:
3739 state[0] = 'S';
3740 break;
3741 case SSTOP:
3742 state[0] = 'T';
3743 break;
3744 case SZOMB:
3745 state[0] = 'Z';
3746 break;
3747 case SDEAD:
3748 state[0] = 'D';
3749 break;
3750 }
3751 attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
3752 }
3753
3754 attrs = Fcons (Fcons (Qppid, INT_TO_INTEGER (proc.p_ppid)), attrs);
3755 attrs = Fcons (Fcons (Qpgrp, INT_TO_INTEGER (proc.p_gid)), attrs);
3756 attrs = Fcons (Fcons (Qsess, INT_TO_INTEGER (proc.p_sid)), attrs);
3757
3758 block_input ();
3759 ttyname = proc.p_tdev == NODEV ? NULL : devname (proc.p_tdev, S_IFCHR);
3760 unblock_input ();
3761 if (ttyname)
3762 attrs = Fcons (Fcons (Qttname, build_string (ttyname)), attrs);
3763
3764 attrs = Fcons (Fcons (Qtpgid, INT_TO_INTEGER (proc.p_tpgid)), attrs);
3765 attrs = Fcons (Fcons (Qminflt, INT_TO_INTEGER (proc.p_uru_minflt)),
3766 attrs);
3767 attrs = Fcons (Fcons (Qmajflt, INT_TO_INTEGER (proc.p_uru_majflt)),
3768 attrs);
3769
3770 /* FIXME: missing cminflt, cmajflt. */
3771
3772 attrs = Fcons (Fcons (Qutime, make_lisp_timeval (proc.p_uutime_sec,
3773 proc.p_uutime_usec)),
3774 attrs);
3775 attrs = Fcons (Fcons (Qstime, make_lisp_timeval (proc.p_ustime_sec,
3776 proc.p_ustime_usec)),
3777 attrs);
3778 t = timespec_add (make_timespec (proc.p_uutime_sec,
3779 proc.p_uutime_usec * 1000),
3780 make_timespec (proc.p_ustime_sec,
3781 proc.p_ustime_usec * 1000));
3782 attrs = Fcons (Fcons (Qtime, make_lisp_time (t)), attrs);
3783
3784 attrs = Fcons (Fcons (Qcutime, make_lisp_timeval (proc.p_uctime_sec,
3785 proc.p_uctime_usec)),
3786 attrs);
3787
3788 /* FIXME: missing cstime and thus ctime. */
3789
3790 attrs = Fcons (Fcons (Qpri, make_fixnum (proc.p_priority)), attrs);
3791 attrs = Fcons (Fcons (Qnice, make_fixnum (proc.p_nice)), attrs);
3792
3793 /* FIXME: missing thcount (thread count) */
3794
3795 attrs = Fcons (Fcons (Qstart, make_lisp_timeval (proc.p_ustart_sec,
3796 proc.p_ustart_usec)),
3797 attrs);
3798
3799 len = (proc.p_vm_tsize + proc.p_vm_dsize + proc.p_vm_ssize) * pagesize >> 10;
3800 attrs = Fcons (Fcons (Qvsize, make_fixnum (len)), attrs);
3801
3802 attrs = Fcons (Fcons (Qrss, make_fixnum (proc.p_vm_rssize * pagesize >> 10)),
3803 attrs);
3804
3805 t = make_timespec (proc.p_ustart_sec,
3806 proc.p_ustart_usec * 1000);
3807 t = timespec_sub (current_timespec (), t);
3808 attrs = Fcons (Fcons (Qetime, make_lisp_time (t)), attrs);
3809
3810 len = sizeof (fscale);
3811 mib[0] = CTL_KERN;
3812 mib[1] = KERN_FSCALE;
3813 if (sysctl (mib, 2, &fscale, &len, NULL, 0) != -1)
3814 {
3815 pct = (double)proc.p_pctcpu / fscale * 100.0;
3816 attrs = Fcons (Fcons (Qpcpu, make_float (pct)), attrs);
3817 }
3818
3819 len = sizeof (uvmexp);
3820 mib[0] = CTL_VM;
3821 mib[1] = VM_UVMEXP;
3822 if (sysctl (mib, 2, &uvmexp, &len, NULL, 0) != -1)
3823 {
3824 pct = (100.0 * (double)proc.p_vm_rssize / uvmexp.npages);
3825 attrs = Fcons (Fcons (Qpmem, make_float (pct)), attrs);
3826 }
3827
3828 len = sizeof args;
3829 mib[0] = CTL_KERN;
3830 mib[1] = KERN_PROC_ARGS;
3831 mib[2] = proc_id;
3832 mib[3] = KERN_PROC_ARGV;
3833 if (sysctl (mib, 4, &args, &len, NULL, 0) == 0 && len != 0)
3834 {
3835 char **argv = (char**)args;
3836
3837 /* concatenate argv reusing the existing storage storage.
3838 sysctl(8) guarantees that "the buffer pointed to by oldp is
3839 filled with an array of char pointers followed by the strings
3840 themselves." */
3841 for (i = 0; argv[i] != NULL; ++i)
3842 {
3843 if (argv[i+1] != NULL)
3844 {
3845 len = strlen (argv[i]);
3846 argv[i][len] = ' ';
3847 }
3848 }
3849
3850 AUTO_STRING (comm, *argv);
3851 decoded_comm = code_convert_string_norecord (comm,
3852 Vlocale_coding_system, 0);
3853 attrs = Fcons (Fcons (Qargs, decoded_comm), attrs);
3854 }
3855
3856 return attrs;
3857}
3858
3664#elif defined DARWIN_OS 3859#elif defined DARWIN_OS
3665 3860
3666Lisp_Object 3861Lisp_Object