xref: /freebsd/sys/contrib/openzfs/lib/libspl/getexecname.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
116038816SMartin Matuska /*
216038816SMartin Matuska  * CDDL HEADER START
316038816SMartin Matuska  *
416038816SMartin Matuska  * The contents of this file are subject to the terms of the
516038816SMartin Matuska  * Common Development and Distribution License, Version 1.0 only
616038816SMartin Matuska  * (the "License").  You may not use this file except in compliance
716038816SMartin Matuska  * with the License.
816038816SMartin Matuska  *
916038816SMartin Matuska  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
1116038816SMartin Matuska  * See the License for the specific language governing permissions
1216038816SMartin Matuska  * and limitations under the License.
1316038816SMartin Matuska  *
1416038816SMartin Matuska  * When distributing Covered Code, include this CDDL HEADER in each
1516038816SMartin Matuska  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1616038816SMartin Matuska  * If applicable, add the following below this CDDL HEADER, with the
1716038816SMartin Matuska  * fields enclosed by brackets "[]" replaced with your own identifying
1816038816SMartin Matuska  * information: Portions Copyright [yyyy] [name of copyright owner]
1916038816SMartin Matuska  *
2016038816SMartin Matuska  * CDDL HEADER END
2116038816SMartin Matuska  */
2216038816SMartin Matuska /*
2316038816SMartin Matuska  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2416038816SMartin Matuska  * Use is subject to license terms.
2516038816SMartin Matuska  */
2616038816SMartin Matuska 
2716038816SMartin Matuska 
2816038816SMartin Matuska #include <limits.h>
2916038816SMartin Matuska #include <pthread.h>
3016038816SMartin Matuska #include <stdlib.h>
3116038816SMartin Matuska #include <string.h>
3216038816SMartin Matuska #include <unistd.h>
3316038816SMartin Matuska #include "libspl_impl.h"
3416038816SMartin Matuska 
3516038816SMartin Matuska 
3616038816SMartin Matuska const char *
getexecname(void)3716038816SMartin Matuska getexecname(void)
3816038816SMartin Matuska {
3916038816SMartin Matuska 	static char execname[PATH_MAX + 1] = "";
4016038816SMartin Matuska 	static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
4116038816SMartin Matuska 
4216038816SMartin Matuska 	char *ptr = execname;
4316038816SMartin Matuska 	ssize_t rc;
4416038816SMartin Matuska 
4516038816SMartin Matuska 	(void) pthread_mutex_lock(&mtx);
4616038816SMartin Matuska 
4716038816SMartin Matuska 	if (strlen(execname) == 0) {
4816038816SMartin Matuska 		rc = getexecname_impl(execname);
4916038816SMartin Matuska 		if (rc == -1) {
5016038816SMartin Matuska 			execname[0] = '\0';
5116038816SMartin Matuska 			ptr = NULL;
5216038816SMartin Matuska 		} else {
5316038816SMartin Matuska 			execname[rc] = '\0';
5416038816SMartin Matuska 		}
5516038816SMartin Matuska 	}
5616038816SMartin Matuska 
5716038816SMartin Matuska 	(void) pthread_mutex_unlock(&mtx);
5816038816SMartin Matuska 	return (ptr);
5916038816SMartin Matuska }
60