Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F147600817
D49850.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
D49850.diff
View Options
diff --git a/usr.bin/Makefile b/usr.bin/Makefile
--- a/usr.bin/Makefile
+++ b/usr.bin/Makefile
@@ -123,6 +123,7 @@
revoke \
rpcinfo \
rs \
+ runat \
rup \
ruptime \
rusers \
diff --git a/usr.bin/runat/Makefile b/usr.bin/runat/Makefile
new file mode 100644
--- /dev/null
+++ b/usr.bin/runat/Makefile
@@ -0,0 +1,3 @@
+PROG= runat
+
+.include <bsd.prog.mk>
diff --git a/usr.bin/runat/runat.1 b/usr.bin/runat/runat.1
new file mode 100644
--- /dev/null
+++ b/usr.bin/runat/runat.1
@@ -0,0 +1,63 @@
+.\"
+.\" Copyright (c) 2025 Rick Macklem
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.Dd April 15, 2025
+.Dt RUNAT 1
+.Os
+.Sh NAME
+.Nm runat
+.Nd run a shell command on a named attribute directory
+.Sh SYNOPSIS
+.Nm
+.Op Ar file
+.Op Ar shell command
+.Sh DESCRIPTION
+The
+.Nm
+utility runs the shell command on the named attribute directory for the
+.Ar file
+argument.
+It does a
+.Xr fchdir 2
+system call to change the current working directory into the
+named attribute directory for the
+.Ar file
+argument and then performs the shell command via
+.Xr sh 1 .
+.Pp
+If a named attribute directory does not exist for
+.Ar file ,
+an empty one will be created.
+If an application needs to determine if a named attribute
+exists for the
+.Ar file ,
+.Xr pathconf 2
+with the name
+.Fa _PC_HAS_NAMEDATTR
+may be used.
+This will not create an empty named attribute directory
+if one does not exist for
+.Ar file .
+.Sh EXAMPLES
+For a
+.Ar file
+called
+.Dq myfile :
+.Bd -literal
+$ runat myfile ls -l # lists the attributes for myfile
+$ runat myfile cp /etc/hosts attrhosts # creates attrhosts
+$ runat myfile cat attrhosts # displays contents of attrhosts
+.Ed
+.Sh SEE ALSO
+.Xr sh 1 ,
+.Xr fchdir 2 ,
+.Xr pathconf 2 ,
+.Xr open 2 ,
+.Xr named_attribute 7
+.Sh HISTORY
+The
+.Nm
+utility first appeared in
+.Fx 15.0 .
diff --git a/usr.bin/runat/runat.c b/usr.bin/runat/runat.c
new file mode 100644
--- /dev/null
+++ b/usr.bin/runat/runat.c
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2025 Rick Macklem
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/param.h>
+#include <sys/wait.h>
+#include <err.h>
+#include <fcntl.h>
+#include <paths.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static void
+usage(void)
+{
+ (void)fprintf(stderr, "usage: runat <file> "
+ "<shell command>\n");
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int i, file_fd, nameddir_fd, outsiz;
+ char *buf;
+ long named_enabled;
+ size_t pos, siz;
+
+ if (argc <= 2)
+ usage();
+ argv++;
+ argc--;
+ if (argc < 2)
+ usage();
+
+ named_enabled = pathconf(argv[0], _PC_NAMEDATTR_ENABLED);
+ if (named_enabled <= 0)
+ errx(1, "Named attributes not enabled for %s", argv[0]);
+
+ /* Generate the command string for "sh". */
+ siz = 0;
+ for (i = 1; i < argc; i++)
+ siz += strlen(argv[i]) + 1;
+ buf = malloc(siz);
+ if (buf == NULL)
+ errx(1, "Cannot malloc");
+ pos = 0;
+ for (i = 1; i < argc; i++) {
+ outsiz = snprintf(&buf[pos], siz, "%s ", argv[i]);
+ if ((size_t)outsiz > siz)
+ errx(1, "Arguments too large");
+ pos += outsiz;
+ siz -= outsiz;
+ }
+ buf[pos - 1] = '\0';
+
+ file_fd = open(argv[0], O_RDONLY | O_CLOEXEC, 0);
+ if (file_fd < 0)
+ err(1, "Cannot open %s", argv[0]);
+ nameddir_fd = openat(file_fd, ".", O_RDONLY | O_CLOEXEC | O_NAMEDATTR,
+ 0);
+ if (nameddir_fd < 0)
+ err(1, "Cannot open named attribute directory "
+ "for %s", argv[0]);
+
+ if (fchdir(nameddir_fd) < 0)
+ err(1, "Cannot fchdir to named attribute dir");
+
+ execl(_PATH_BSHELL, "sh", "-c", buf, NULL);
+ err(1, "Could not exec %s", _PATH_BSHELL);
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Mar 13, 6:18 AM (20 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
29611998
Default Alt Text
D49850.diff (3 KB)
Attached To
Mode
D49850: runat: A utility that executes a shell command on a named attribute
Attached
Detach File
Event Timeline
Log In to Comment