emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [patch] system_process_attributes for OpenBSD


From: Timo Myyrä
Subject: Re: [patch] system_process_attributes for OpenBSD
Date: Sat, 02 Jan 2021 14:16:47 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1.90 (berkeley-unix)

Omar Polo <op@omarpolo.com> [2021-01-02, 11:36 +0100]:

> Timo Myyrä <timo.myyra@bittivirhe.fi> writes:
>
>> Omar Polo <op@omarpolo.com> [2021-01-01, 23:29 +0100]:
>>
>>[snip]
>>
>> Hi Omar,
>>
>> Are you sure you need kvm? I'm under impression its frowned upon to
>> access that from userland.
>
> Thanks for sharing!  I haven't followed closely the development of
> OpenBSD, but at least on -CURRENT trying to get a struct kinfo_proc for
> a specific pid via sysctl results in errors (both from sysctl(8) and
> sysctl(3)):
>
>     $ sysctl kern.proc
>     sysctl: use ps to view kern.proc information
>     $ sysctl kern.proc 75056  # a valid pid
>     sysctl: use ps to view kern.proc information
>     sysctl: top level name 75056 in 75056 is invalid
>
>     $ cat <<EOF > pid.c
>     #include <sys/types.h>
>     #include <sys/sysctl.h>
>     #include <err.h>
>     #include <stdlib.h>
>
>     int
>     main(void)
>     {
>             pid_t pid = 75056; /* valid pid */
>             int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
>             struct kinfo_proc proc;
>             size_t len = sizeof(proc);
>
>             if (sysctl(mib, 4, &proc, &len, NULL, 0) != 0 || len == 0)
>                     err(1, "sysctl");
>             return 0;
>     }
>     EOF
>     $ cc pid.c -o pid && ./pid
>     pid: sysctl: Invalid argument
>     $ ps 75056
>     75056 ??  S        0:38.85 emacs --daemon (emacs-27.1)
>
> This plus the fact that both top and ps in base uses kvm were the
> rationale for my choice.
>
> Regarding list_system_processes, I still have to check the differences,
> but the current version on master is fundamentally equivalent to yours
> (modulo a bunch of #ifdefs for freebsd and macos), so I guess it was
> merged at some point?  Here on emacs-27.1 (list-system-processes)
> returns nil, but on emacs compiled from master it works correctly.

Yeah, forgot to finish this so its been a while. Seems that the process
listing is already in emacs so that can be discarded.

But I got following sample program working without using kvm.

#include <sys/types.h>
#include <sys/sysctl.h>
#include <err.h>
#include <stdlib.h>
#include <stdio.h>

int
main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Must give PID number as an argument!\n");
    exit(1);
  }
  pid_t pid = atoi(argv[1]); /* valid pid */
  int mib[6] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid, sizeof(struct 
kinfo_proc), 1};
  struct kinfo_proc proc;
  size_t len = sizeof(proc);

  if (sysctl(mib, 6, &proc, &len, NULL, 0) != 0)
    err(1, "sysctl");
  printf("pid belongs to command: %s\n", proc.p_comm);
  return 0;
}

I'd say above would be prefered as there would be no need to add more
libraries. I recall the sysctl(8) is intentionally a bit limited but the
sysctl(2) should give full access.

Timo

reply via email to

[Prev in Thread] Current Thread [Next in Thread]