1*f765c471Schristos /*-
2*f765c471Schristos * Copyright (c) 2017 The NetBSD Foundation, Inc.
3*f765c471Schristos * All rights reserved.
4*f765c471Schristos *
5*f765c471Schristos * This code is derived from software contributed to The NetBSD Foundation
6*f765c471Schristos * by Christos Zoulas.
7*f765c471Schristos *
8*f765c471Schristos * Redistribution and use in source and binary forms, with or without
9*f765c471Schristos * modification, are permitted provided that the following conditions
10*f765c471Schristos * are met:
11*f765c471Schristos * 1. Redistributions of source code must retain the above copyright
12*f765c471Schristos * notice, this list of conditions and the following disclaimer.
13*f765c471Schristos * 2. Redistributions in binary form must reproduce the above copyright
14*f765c471Schristos * notice, this list of conditions and the following disclaimer in the
15*f765c471Schristos * documentation and/or other materials provided with the distribution.
16*f765c471Schristos *
17*f765c471Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18*f765c471Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19*f765c471Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20*f765c471Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21*f765c471Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*f765c471Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*f765c471Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*f765c471Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*f765c471Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*f765c471Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*f765c471Schristos * POSSIBILITY OF SUCH DAMAGE.
28*f765c471Schristos */
29*f765c471Schristos
30*f765c471Schristos #include <stdio.h>
31*f765c471Schristos #include <stdlib.h>
32*f765c471Schristos #include <err.h>
33*f765c471Schristos #include <sys/sysctl.h>
34*f765c471Schristos
35*f765c471Schristos static int
getprocpath(char * path,size_t len,pid_t pid)36*f765c471Schristos getprocpath(char *path, size_t len, pid_t pid)
37*f765c471Schristos {
38*f765c471Schristos const int name[] = {
39*f765c471Schristos CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_PATHNAME,
40*f765c471Schristos };
41*f765c471Schristos return sysctl(name, __arraycount(name), path, &len, NULL, 0);
42*f765c471Schristos }
43*f765c471Schristos
44*f765c471Schristos int
main(int argc,char * argv[])45*f765c471Schristos main(int argc, char *argv[])
46*f765c471Schristos {
47*f765c471Schristos if (argc != 2) {
48*f765c471Schristos fprintf(stderr, "Usage: %s <pid>\n", getprogname());
49*f765c471Schristos return EXIT_FAILURE;
50*f765c471Schristos }
51*f765c471Schristos
52*f765c471Schristos char buf[MAXPATHLEN];
53*f765c471Schristos pid_t pid = atoi(argv[1]);
54*f765c471Schristos
55*f765c471Schristos if (getprocpath(buf, sizeof(buf), pid) == -1)
56*f765c471Schristos err(EXIT_FAILURE, "sysctl: %d", pid);
57*f765c471Schristos printf("%s\n", buf);
58*f765c471Schristos return EXIT_SUCCESS;
59*f765c471Schristos }
60