Skip to content

Commit 160f5a2

Browse files
nabijaczleweliRyan Moeller
authored andcommitted
libspl: lift common bits of getexecname()
Merge the actual implementations of getexecname() and slightly clean up the FreeBSD one. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes openzfs#11879
1 parent 5c8067a commit 160f5a2

File tree

5 files changed

+99
-72
lines changed

5 files changed

+99
-72
lines changed

lib/libspl/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ libspl_assert_la_SOURCES = \
2121
assert.c
2222

2323
USER_C = \
24+
libspl_impl.h \
25+
getexecname.c \
2426
list.c \
2527
mkdirp.c \
2628
page.c \

lib/libspl/getexecname.c

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

lib/libspl/libspl_impl.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License, Version 1.0 only
6+
* (the "License"). You may not use this file except in compliance
7+
* with the License.
8+
*
9+
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10+
* or http://www.opensolaris.org/os/licensing.
11+
* See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*
14+
* When distributing Covered Code, include this CDDL HEADER in each
15+
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16+
* If applicable, add the following below this CDDL HEADER, with the
17+
* fields enclosed by brackets "[]" replaced with your own identifying
18+
* information: Portions Copyright [yyyy] [name of copyright owner]
19+
*
20+
* CDDL HEADER END
21+
*/
22+
23+
24+
extern ssize_t getexecname_impl(char *execname);

lib/libspl/os/freebsd/getexecname.c

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,52 +20,21 @@
2020
* CDDL HEADER END
2121
*/
2222

23-
24-
#include <errno.h>
23+
#include <stdint.h>
2524
#include <limits.h>
26-
#include <pthread.h>
27-
#include <stdlib.h>
28-
#include <string.h>
2925
#include <sys/param.h>
3026
#include <sys/sysctl.h>
3127
#include <sys/types.h>
32-
#include <unistd.h>
28+
#include "../../libspl_impl.h"
3329

34-
const char *
35-
getexecname(void)
30+
__attribute__((visibility("hidden"))) ssize_t
31+
getexecname_impl(char *execname)
3632
{
37-
static char execname[PATH_MAX + 1] = "";
38-
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
39-
char *ptr = NULL;
40-
ssize_t rc;
41-
42-
(void) pthread_mutex_lock(&mtx);
43-
44-
if (strlen(execname) == 0) {
45-
int error, name[4];
46-
size_t len;
33+
size_t len = PATH_MAX;
34+
int name[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
4735

48-
name[0] = CTL_KERN;
49-
name[1] = KERN_PROC;
50-
name[2] = KERN_PROC_PATHNAME;
51-
name[3] = -1;
52-
len = PATH_MAX;
53-
error = sysctl(name, nitems(name), execname, &len, NULL, 0);
54-
if (error != 0) {
55-
rc = -1;
56-
} else {
57-
rc = len;
58-
}
59-
if (rc == -1) {
60-
execname[0] = '\0';
61-
} else {
62-
execname[rc] = '\0';
63-
ptr = execname;
64-
}
65-
} else {
66-
ptr = execname;
67-
}
36+
if (sysctl(name, nitems(name), execname, &len, NULL, 0) != 0)
37+
return (-1);
6838

69-
(void) pthread_mutex_unlock(&mtx);
70-
return (ptr);
39+
return (len);
7140
}

lib/libspl/os/linux/getexecname.c

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,14 @@
1919
*
2020
* CDDL HEADER END
2121
*/
22-
/*
23-
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24-
* Use is subject to license terms.
25-
*/
26-
2722

2823
#include <limits.h>
29-
#include <pthread.h>
30-
#include <stdlib.h>
31-
#include <string.h>
24+
#include <stdint.h>
3225
#include <unistd.h>
26+
#include "../../libspl_impl.h"
3327

34-
const char *
35-
getexecname(void)
28+
__attribute__((visibility("hidden"))) ssize_t
29+
getexecname_impl(char *execname)
3630
{
37-
static char execname[PATH_MAX + 1] = "";
38-
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
39-
char *ptr = NULL;
40-
ssize_t rc;
41-
42-
(void) pthread_mutex_lock(&mtx);
43-
44-
if (strlen(execname) == 0) {
45-
rc = readlink("/proc/self/exe",
46-
execname, sizeof (execname) - 1);
47-
if (rc == -1) {
48-
execname[0] = '\0';
49-
} else {
50-
execname[rc] = '\0';
51-
ptr = execname;
52-
}
53-
} else {
54-
ptr = execname;
55-
}
56-
57-
(void) pthread_mutex_unlock(&mtx);
58-
return (ptr);
31+
return (readlink("/proc/self/exe", execname, PATH_MAX));
5932
}

0 commit comments

Comments
 (0)