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