xref: /freebsd-src/sys/contrib/openzfs/lib/libspl/os/freebsd/getexecname.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy  * CDDL HEADER START
3*eda14cbcSMatt Macy  *
4*eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5*eda14cbcSMatt Macy  * Common Development and Distribution License, Version 1.0 only
6*eda14cbcSMatt Macy  * (the "License").  You may not use this file except in compliance
7*eda14cbcSMatt Macy  * with the License.
8*eda14cbcSMatt Macy  *
9*eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
11*eda14cbcSMatt Macy  * See the License for the specific language governing permissions
12*eda14cbcSMatt Macy  * and limitations under the License.
13*eda14cbcSMatt Macy  *
14*eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
15*eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
17*eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
18*eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
19*eda14cbcSMatt Macy  *
20*eda14cbcSMatt Macy  * CDDL HEADER END
21*eda14cbcSMatt Macy  */
22*eda14cbcSMatt Macy 
23*eda14cbcSMatt Macy 
24*eda14cbcSMatt Macy #include <errno.h>
25*eda14cbcSMatt Macy #include <limits.h>
26*eda14cbcSMatt Macy #include <pthread.h>
27*eda14cbcSMatt Macy #include <stdlib.h>
28*eda14cbcSMatt Macy #include <string.h>
29*eda14cbcSMatt Macy #include <sys/param.h>
30*eda14cbcSMatt Macy #include <sys/sysctl.h>
31*eda14cbcSMatt Macy #include <sys/types.h>
32*eda14cbcSMatt Macy #include <unistd.h>
33*eda14cbcSMatt Macy 
34*eda14cbcSMatt Macy const char *
35*eda14cbcSMatt Macy getexecname(void)
36*eda14cbcSMatt Macy {
37*eda14cbcSMatt Macy 	static char execname[PATH_MAX + 1] = "";
38*eda14cbcSMatt Macy 	static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
39*eda14cbcSMatt Macy 	char *ptr = NULL;
40*eda14cbcSMatt Macy 	ssize_t rc;
41*eda14cbcSMatt Macy 
42*eda14cbcSMatt Macy 	(void) pthread_mutex_lock(&mtx);
43*eda14cbcSMatt Macy 
44*eda14cbcSMatt Macy 	if (strlen(execname) == 0) {
45*eda14cbcSMatt Macy 		int error, name[4];
46*eda14cbcSMatt Macy 		size_t len;
47*eda14cbcSMatt Macy 
48*eda14cbcSMatt Macy 		name[0] = CTL_KERN;
49*eda14cbcSMatt Macy 		name[1] = KERN_PROC;
50*eda14cbcSMatt Macy 		name[2] = KERN_PROC_PATHNAME;
51*eda14cbcSMatt Macy 		name[3] = -1;
52*eda14cbcSMatt Macy 		len = PATH_MAX;
53*eda14cbcSMatt Macy 		error = sysctl(name, nitems(name), execname, &len, NULL, 0);
54*eda14cbcSMatt Macy 		if (error != 0) {
55*eda14cbcSMatt Macy 			rc = -1;
56*eda14cbcSMatt Macy 		} else {
57*eda14cbcSMatt Macy 			rc = len;
58*eda14cbcSMatt Macy 		}
59*eda14cbcSMatt Macy 		if (rc == -1) {
60*eda14cbcSMatt Macy 			execname[0] = '\0';
61*eda14cbcSMatt Macy 		} else {
62*eda14cbcSMatt Macy 			execname[rc] = '\0';
63*eda14cbcSMatt Macy 			ptr = execname;
64*eda14cbcSMatt Macy 		}
65*eda14cbcSMatt Macy 	} else {
66*eda14cbcSMatt Macy 		ptr = execname;
67*eda14cbcSMatt Macy 	}
68*eda14cbcSMatt Macy 
69*eda14cbcSMatt Macy 	(void) pthread_mutex_unlock(&mtx);
70*eda14cbcSMatt Macy 	return (ptr);
71*eda14cbcSMatt Macy }
72