unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Timo Myyrä" <timo.myyra@bittivirhe.fi>
To: emacs-devel@gnu.org
Subject: Re: [patch] system_process_attributes for OpenBSD
Date: Sat, 02 Jan 2021 14:16:47 +0200	[thread overview]
Message-ID: <87v9cfo7o0.fsf@asteroid.bittivirhe.fi> (raw)
In-Reply-To: 87o8i7pqvt.fsf@omarpolo.com

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	other threads:[~2021-01-02 12:16 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-01 22:29 [patch] system_process_attributes for OpenBSD Omar Polo
2021-01-02  6:18 ` Lars Ingebrigtsen
2021-01-02  7:29 ` Timo Myyrä
2021-01-02 10:36   ` Omar Polo
2021-01-02 12:16     ` Timo Myyrä [this message]
2021-01-02 12:30       ` Omar Polo
2021-01-02  7:43 ` Timo Myyrä
2021-01-05 19:39 ` Omar Polo
2021-01-07 11:23   ` Lars Ingebrigtsen
2021-01-07 11:30     ` Omar Polo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v9cfo7o0.fsf@asteroid.bittivirhe.fi \
    --to=timo.myyra@bittivirhe.fi \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).