Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F141040119
D27022.id78961.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
10 KB
Referenced Files
None
Subscribers
None
D27022.id78961.diff
View Options
Index: lib/libutil/Makefile
===================================================================
--- lib/libutil/Makefile
+++ lib/libutil/Makefile
@@ -12,7 +12,8 @@
LIB= util
SHLIB_MAJOR= 9
-SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \
+SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c \
+ getlocalbase.c gr_util.c \
hexdump.c humanize_number.c kinfo_getfile.c \
kinfo_getallproc.c kinfo_getproc.c kinfo_getvmmap.c \
kinfo_getvmobject.c kld.c \
@@ -30,7 +31,7 @@
CFLAGS+= -I${.CURDIR} -I${SRCTOP}/lib/libc/gen/
-MAN+= expand_number.3 flopen.3 fparseln.3 hexdump.3 \
+MAN+= expand_number.3 flopen.3 fparseln.3 getlocalbase.3 hexdump.3 \
humanize_number.3 kinfo_getallproc.3 kinfo_getfile.3 \
kinfo_getproc.3 kinfo_getvmmap.3 kinfo_getvmobject.3 kld.3 \
login_auth.3 login_cap.3 \
Index: lib/libutil/getlocalbase.3
===================================================================
--- /dev/null
+++ lib/libutil/getlocalbase.3
@@ -0,0 +1,103 @@
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+.\"
+.\" Copyright 2020 Scott Long
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd Oct 28, 2020
+.Dt GETLOCALBASE 3
+.Os
+.Sh NAME
+.Nm getlocalbase
+.Nd "return the path to the local software directory"
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In libutil.h
+.Ft int
+.Fn getlocalbase char* ssize_t
+.Sh DESCRIPTION
+The
+.Fn getlocalbase
+function returns the path to the local software base directory.
+Normally this is the
+.Pa /usr/local
+directory.
+First the
+.Ev LOCALBASE
+environment variable checked.
+If that doesn't exist then the
+.Em user.localbase
+sysctl is checked.
+If that also doesn't exist then the value of the
+.Em _PATH_LOCALBASE
+compile-time variable is used.
+If that is undefined then the default of
+.Pa /usr/local
+is used.
+.Pp
+The
+.Fn getlocalbase
+function takes two arguments:
+.Fa path
+and
+.Fa len .
+The
+.Fa path
+argument points to a caller-supplied buffer to hold the resulting pathname.
+The
+.Fa len
+argument is a pointer to the length of the caller-supplied buffer. At
+return it will be updated with the actual length of the string copied into
+the
+.Fa path
+buffer.
+.Sh IMPLEMENTATION NOTES
+Calls to
+.Fn getlocalpath
+will perform a setugid check on the running binary before checking the
+environment.
+.Sh RETURN VALUES
+.Rv -std getlocalbase
+.Sh ENVIRONMENT
+The
+.Fn getlocalbase
+library function retrieves the
+.Ev LOCALBASE
+environment variable.
+.El
+.Sh SEE ALSO
+.Xr env 1 ,
+.Xr src.conf 5
+.Sh HISTORY
+The
+.Nm
+manual page example first appeared in
+.Fx 13.0 .
+.Pp
+.Sh AUTHORS
+This
+manual page was written by
+.An Scott Long Aq Mt scottl@FreeBSD.org
Index: lib/libutil/getlocalbase.c
===================================================================
--- /dev/null
+++ lib/libutil/getlocalbase.c
@@ -0,0 +1,67 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright 2020 Scott Long <scottl@freebsd.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/errno.h>
+#include <sys/sysctl.h>
+#include <stdlib.h>
+#include <paths.h>
+#include <libutil.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+getlocalbase(char *path, size_t *pathlen)
+{
+ const char *tmppath;
+
+ if (path == NULL) {
+ *pathlen = 0;
+ return (ENOMEM);
+ }
+
+ tmppath = NULL;
+ if (issetugid() == 0)
+ tmppath = getenv("LOCALBASE");
+
+ if ((tmppath == NULL) &&
+ (sysctlbyname("user.localbase", path, pathlen, NULL, 0) == 0))
+ return (0);
+
+ if (path == NULL)
+#ifdef _PATH_LOCALBASE
+ tmppath = _PATH_LOCALBASE;
+#else
+ tmppath = "/usr/local";
+#endif
+
+ *pathlen = strlcpy(path, tmppath, *pathlen);
+ return (0);
+}
Index: lib/libutil/libutil.h
===================================================================
--- lib/libutil/libutil.h
+++ lib/libutil/libutil.h
@@ -98,6 +98,7 @@
int flopenat(int _dirfd, const char *_path, int _flags, ...);
int forkpty(int *_amaster, char *_name,
struct termios *_termp, struct winsize *_winp);
+int getlocalbase(char *path, size_t *pathlen);
void hexdump(const void *_ptr, int _length, const char *_hdr, int _flags);
int humanize_number(char *_buf, size_t _len, int64_t _number,
const char *_suffix, int _scale, int _flags);
Index: sbin/nvmecontrol/comnd.h
===================================================================
--- sbin/nvmecontrol/comnd.h
+++ sbin/nvmecontrol/comnd.h
@@ -96,7 +96,7 @@
int arg_parse(int argc, char * const *argv, const struct cmd *f);
void arg_help(int argc, char * const *argv, const struct cmd *f);
void cmd_init(void);
-void cmd_load_dir(const char *dir, cmd_load_cb_t *cb, void *argp);
+void cmd_load_dir(char *dir, cmd_load_cb_t *cb, void *argp);
int cmd_dispatch(int argc, char *argv[], const struct cmd *);
#endif /* COMND_H */
Index: sbin/nvmecontrol/comnd.c
===================================================================
--- sbin/nvmecontrol/comnd.c
+++ sbin/nvmecontrol/comnd.c
@@ -286,7 +286,7 @@
* Loads all the .so's from the specified directory.
*/
void
-cmd_load_dir(const char *dir __unused, cmd_load_cb_t cb __unused, void *argp __unused)
+cmd_load_dir(char *dir, cmd_load_cb_t cb, void *argp)
{
DIR *d;
struct dirent *dent;
Index: sbin/nvmecontrol/nvmecontrol.c
===================================================================
--- sbin/nvmecontrol/nvmecontrol.c
+++ sbin/nvmecontrol/nvmecontrol.c
@@ -38,6 +38,7 @@
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <libutil.h>
#include <paths.h>
#include <stdbool.h>
#include <stddef.h>
@@ -175,11 +176,18 @@
int
main(int argc, char *argv[])
{
+ char locallib[MAXPATHLEN];
+ size_t len;
cmd_init();
- cmd_load_dir("/lib/nvmecontrol", NULL, NULL);
- cmd_load_dir(_PATH_LOCALBASE "/lib/nvmecontrol", NULL, NULL);
+ snprintf(locallib, MAXPATHLEN, "/lib/nvmecontrol");
+ cmd_load_dir(locallib, NULL, NULL);
+ len = MAXPATHLEN;
+ if (getlocalbase(locallib, &len) == 0) {
+ strncat(locallib, "/lib/nvmecontrol", MAXPATHLEN - len);
+ cmd_load_dir(locallib, NULL, NULL);
+ }
cmd_dispatch(argc, argv, NULL);
Index: usr.sbin/mailwrapper/mailwrapper.c
===================================================================
--- usr.sbin/mailwrapper/mailwrapper.c
+++ usr.sbin/mailwrapper/mailwrapper.c
@@ -105,8 +105,11 @@
initarg(&al);
addarg(&al, argv[0]);
- snprintf(localmailerconf, MAXPATHLEN, "%s/etc/mail/mailer.conf",
- getenv("LOCALBASE") ? getenv("LOCALBASE") : _PATH_LOCALBASE);
+ len = MAXPATHLEN;
+ if (getlocalbase(localmailerconf, &len) != 0)
+ err(EX_OSERR, "cannot determine local path");
+
+ strncat(localmailerconf, "/etc/mail/mailer.conf", MAXPATHLEN - len);
mailerconf = localmailerconf;
if ((config = fopen(localmailerconf, "r")) == NULL)
Index: usr.sbin/pkg/Makefile
===================================================================
--- usr.sbin/pkg/Makefile
+++ usr.sbin/pkg/Makefile
@@ -25,6 +25,6 @@
CFLAGS+=-I${SRCTOP}/contrib/libucl/include
.PATH: ${SRCTOP}/contrib/libucl/include
-LIBADD= archive fetch ucl sbuf crypto ssl
+LIBADD= archive fetch ucl sbuf crypto ssl util
.include <bsd.prog.mk>
Index: usr.sbin/pkg/pkg.c
===================================================================
--- usr.sbin/pkg/pkg.c
+++ usr.sbin/pkg/pkg.c
@@ -43,6 +43,7 @@
#include <errno.h>
#include <fcntl.h>
#include <fetch.h>
+#include <libutil.h>
#include <paths.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -1037,6 +1038,7 @@
{
char pkgpath[MAXPATHLEN];
const char *pkgarg;
+ size_t len;
int i;
bool bootstrap_only, force, yes;
@@ -1045,8 +1047,12 @@
pkgarg = NULL;
yes = false;
- snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
- getenv("LOCALBASE") ? getenv("LOCALBASE") : _PATH_LOCALBASE);
+ len = MAXPATHLEN;
+ if (getlocalbase(pkgpath, &len) != 0) {
+ fprintf(stderr, "Cannot determine local path\n");
+ exit(EXIT_FAILURE);
+ }
+ strncat(pkgpath, "/sbin/pkg", MAXPATHLEN - len);
if (argc > 1 && strcmp(argv[1], "bootstrap") == 0) {
bootstrap_only = true;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Jan 1, 2:13 AM (1 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
27427497
Default Alt Text
D27022.id78961.diff (10 KB)
Attached To
Mode
D27022: Introduce getlocalbase.3
Attached
Detach File
Event Timeline
Log In to Comment