NetBSD Problem Report #50620

From wiz@yt.nih.at  Mon Jan  4 12:20:36 2016
Return-Path: <wiz@yt.nih.at>
Received: from mail.netbsd.org (mail.NetBSD.org [199.233.217.200])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.netbsd.org", Issuer "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 2A7457ABF5
	for <gnats-bugs@gnats.NetBSD.org>; Mon,  4 Jan 2016 12:20:36 +0000 (UTC)
Message-Id: <20160104122029.B27C12AC1BB@yt.nih.at>
Date: Mon,  4 Jan 2016 13:20:29 +0100 (CET)
From: Thomas Klausner <wiz@NetBSD.org>
Reply-To: Thomas Klausner <wiz@NetBSD.org>
To: gnats-bugs@NetBSD.org
Subject: Add interface to get process' working directory
X-Send-Pr-Version: 3.95

>Number:         50620
>Category:       kern
>Synopsis:       Add interface to get process' working directory
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kern-bug-people
>State:          closed
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Jan 04 12:25:00 +0000 2016
>Closed-Date:    Tue Jun 11 09:36:33 +0000 2019
>Last-Modified:  Tue Jun 11 09:36:33 +0000 2019
>Originator:     Thomas Klausner
>Release:        NetBSD 7.99.25
>Organization:
Curiosity is the very basis of education and if you tell me that 
curiosity killed the cat, I say only that the cat died nobly.
- Arnold Edinborough
>Environment:


Architecture: x86_64
Machine: amd64
>Description:
Please provide an interface to get a process' current working directory.

Both FreeBSD and OpenBSD provide this, and some applications want to use it
(py-psutil, tmux).

If I understood the FreeBSD code in psutil correctly, it is done this way:
    size_t size;
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = pid;

    size = sizeof(struct kinfo_proc);
    if (sysctl((int *)mib, 4, proc, &size, NULL, 0) == -1) {
...

    freep = kinfo_getfile(...)
    if (freep == NULL) {
...
    (iterate over results)
        if (kif->kf_fd == KF_FD_TYPE_CWD) {
            print("%s\n", kif->kf_path);
...


OpenBSD does it more directly:

    int name[] = { CTL_KERN, KERN_PROC_CWD, pid };
    if (sysctl(name, 3, path, &pathlen, NULL, 0) != 0) {
...

then path contains the path.


>How-To-Repeat:

>Fix:
Please.

>Release-Note:

>Audit-Trail:
From: "Kamil Rytarowski" <kamil@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/50620 CVS commit: src/sys
Date: Fri, 31 May 2019 23:01:39 +0000

 Module Name:	src
 Committed By:	kamil
 Date:		Fri May 31 23:01:39 UTC 2019

 Modified Files:
 	src/sys/kern: kern_proc.c
 	src/sys/sys: sysctl.h

 Log Message:
 Implement KERN_PROC_CWD in sysctl(3)

 Retrieve specified process current working directory.

 Fixes PR kern/50620 by Thomas Klausner.


 To generate a diff of this commit:
 cvs rdiff -u -r1.228 -r1.229 src/sys/kern/kern_proc.c
 cvs rdiff -u -r1.229 -r1.230 src/sys/sys/sysctl.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->feedback
State-Changed-By: kamil@NetBSD.org
State-Changed-When: Sat, 01 Jun 2019 02:23:26 +0200
State-Changed-Why:
src/sys/kern/kern_proc.c r.1.231 implements the interface as KERN_PROC_CWD.

Does it work for you?


State-Changed-From-To: feedback->closed
State-Changed-By: wiz@NetBSD.org
State-Changed-When: Tue, 11 Jun 2019 09:36:33 +0000
State-Changed-Why:
leot tested it, thank you both!


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.43 2018/01/16 07:36:43 maya Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2017 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.