diff options
| author | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-02-28 14:49:51 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-02-28 14:49:51 +0300 |
| commit | 9503da4882e2004a9c0287de9bda9e1b8a0c62df (patch) | |
| tree | 69d4daae57666b0e320c60453ddd6f163627a0ce | |
| download | Project-Tick-9503da4882e2004a9c0287de9bda9e1b8a0c62df.tar.gz Project-Tick-9503da4882e2004a9c0287de9bda9e1b8a0c62df.zip | |
init Standalone musl-libc-based Linux port of FreeBSD `chmod` for
Project Tick BSD/Linux Distribution.
Signed-off-by: Mehmet Samet Duman <yongdohyun@projecttick.org>
| -rw-r--r-- | .gitignore | 25 | ||||
| -rw-r--r-- | GNUmakefile | 38 | ||||
| -rw-r--r-- | LICENSE | 32 | ||||
| -rw-r--r-- | LICENSES/BSD-3-Clause.txt | 11 | ||||
| -rw-r--r-- | Makefile | 9 | ||||
| -rw-r--r-- | Makefile.depend | 15 | ||||
| -rw-r--r-- | README.md | 28 | ||||
| -rw-r--r-- | chmod.1 | 364 | ||||
| -rw-r--r-- | chmod.c | 438 | ||||
| -rw-r--r-- | mode.c | 424 | ||||
| -rw-r--r-- | mode.h | 8 | ||||
| -rw-r--r-- | tests/Makefile | 3 | ||||
| -rw-r--r-- | tests/Makefile.depend | 10 | ||||
| -rwxr-xr-x | tests/chmod_test.sh | 179 | ||||
| -rw-r--r-- | tests/test.sh | 81 |
15 files changed, 1665 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..a74d30b48c --- /dev/null +++ b/.gitignore @@ -0,0 +1,25 @@ +*.a +*.core +*.lo +*.nossppico +*.o +*.orig +*.pico +*.pieo +*.po +*.rej +*.so +*.so.[0-9]* +*.sw[nop] +*~ +.*DS_Store +.cache +.clangd +.ccls-cache +.depend* +compile_commands.json +compile_commands.events.json +tags +build/ +out/ +.linux-obj/ diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 0000000000..c18eb087ff --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,38 @@ +.DEFAULT_GOAL := all + +CC ?= cc +CPPFLAGS += -D_GNU_SOURCE +CFLAGS ?= -O2 +CFLAGS += -g -Wall -Wextra -Wno-unused-parameter +LDFLAGS ?= +LDLIBS ?= + +OBJDIR := $(CURDIR)/build +OUTDIR := $(CURDIR)/out +TARGET := $(OUTDIR)/chmod +OBJS := $(OBJDIR)/chmod.o $(OBJDIR)/mode.o + +.PHONY: all clean dirs test status + +all: $(TARGET) + +dirs: + @mkdir -p "$(OBJDIR)" "$(OUTDIR)" + +$(TARGET): $(OBJS) | dirs + $(CC) $(LDFLAGS) -o "$@" $(OBJS) $(LDLIBS) + +$(OBJDIR)/chmod.o: $(CURDIR)/chmod.c $(CURDIR)/mode.h | dirs + $(CC) $(CPPFLAGS) $(CFLAGS) -c "$(CURDIR)/chmod.c" -o "$@" + +$(OBJDIR)/mode.o: $(CURDIR)/mode.c $(CURDIR)/mode.h | dirs + $(CC) $(CPPFLAGS) $(CFLAGS) -c "$(CURDIR)/mode.c" -o "$@" + +test: $(TARGET) + CHMOD_BIN="$(TARGET)" sh "$(CURDIR)/tests/test.sh" + +status: + @printf '%s\n' "$(TARGET)" + +clean: + @rm -rf "$(CURDIR)/build" "$(CURDIR)/out" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..4181bb7a7a --- /dev/null +++ b/LICENSE @@ -0,0 +1,32 @@ +Copyright (c) 1989, 1993 + The Regents of the University of California. All rights reserved. + +Copyright (c) 2026 + Project Tick. All rights reserved. + +This code is derived from software contributed to Berkeley by +Kevin Fall. + +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. +3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +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.
\ No newline at end of file diff --git a/LICENSES/BSD-3-Clause.txt b/LICENSES/BSD-3-Clause.txt new file mode 100644 index 0000000000..ea890afbc7 --- /dev/null +++ b/LICENSES/BSD-3-Clause.txt @@ -0,0 +1,11 @@ +Copyright (c) <year> <owner>. + +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. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..6ad01ed4b7 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +.include <src.opts.mk> + +PACKAGE=runtime +PROG= chmod + +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests + +.include <bsd.prog.mk> diff --git a/Makefile.depend b/Makefile.depend new file mode 100644 index 0000000000..6ef78fac5c --- /dev/null +++ b/Makefile.depend @@ -0,0 +1,15 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcompiler_rt \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/README.md b/README.md new file mode 100644 index 0000000000..a217b2dfd2 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# chmod + +Standalone musl-libc-based Linux port of FreeBSD `chmod` for Project Tick BSD/Linux Distribution. + +## Status + +Project layout is detached from the top-level FreeBSD build. +It builds as a standalone Linux-native utility. + +## Build + +```sh +gmake -f GNUmakefile +gmake -f GNUmakefile CC=musl-gcc +``` + +## Test + +```sh +gmake -f GNUmakefile test +``` + +## Notes + +- Mode parsing lives in local project code, not a shared compat layer. +- The current implementation keeps the original recursive traversal logic. +- Recursive walking no longer depends on `fts(3)`, so it builds with musl. +- Verified with `gmake -f GNUmakefile clean test` and `gmake -f GNUmakefile clean test CC=musl-gcc`. diff --git a/chmod.1 b/chmod.1 new file mode 100644 index 0000000000..ff96b24d30 --- /dev/null +++ b/chmod.1 @@ -0,0 +1,364 @@ +.\"- +.\" Copyright (c) 1989, 1990, 1993, 1994 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Copyright (c) 2026 +.\" Project Tick. All rights reserved. +.\" +.\" This code is derived from software contributed to Berkeley by +.\" the Institute of Electrical and Electronics Engineers, Inc. +.\" +.\" 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. +.\" 3. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" 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. +.\" +.Dd January 7, 2017 +.Dt CHMOD 1 +.Os +.Sh NAME +.Nm chmod +.Nd change file modes +.Sh SYNOPSIS +.Nm +.Op Fl fhv +.Op Fl R Op Fl H | L | P +.Ar mode +.Ar +.Sh DESCRIPTION +The +.Nm +utility modifies the file mode bits of the listed files +as specified by the +.Ar mode +operand. +.Pp +The options are as follows: +.Bl -tag -width indent +.It Fl f +Do not display a diagnostic message if +.Nm +could not modify the mode for +.Va file , +nor modify the exit status to reflect such failures. +.It Fl H +If the +.Fl R +option is specified, symbolic links on the command line are followed +and hence unaffected by the command. +(Symbolic links encountered during tree traversal are not followed.) +.It Fl h +If the file is a symbolic link, change the mode of the link itself +rather than the file that the link points to. +.It Fl L +If the +.Fl R +option is specified, all symbolic links are followed. +.It Fl P +If the +.Fl R +option is specified, no symbolic links are followed. +This is the default. +.It Fl R +Change the modes of the file hierarchies rooted in the files, +instead of just the files themselves. +Beware of unintentionally matching the +.Dq Pa ".." +hard link to the parent directory when using wildcards like +.Dq Li ".*" . +.It Fl v +Cause +.Nm +to be verbose, showing filenames as the mode is modified. +If the +.Fl v +flag is specified more than once, the old and new modes of the file +will also be printed, in both octal and symbolic notation. +.El +.Pp +The +.Fl H , +.Fl L +and +.Fl P +options are ignored unless the +.Fl R +option is specified. +In addition, these options override each other and the +command's actions are determined by the last one specified. +.Pp +If +.Nm +receives a +.Dv SIGINFO +signal (see the +.Cm status +argument for +.Xr stty 1 ) , +then the current filename as well as the old and new modes are displayed. +.Pp +Only the owner of a file or the super-user is permitted to change +the mode of a file. +.Sh EXIT STATUS +.Ex -std +.Sh MODES +Modes may be absolute or symbolic. +An absolute mode is an octal number constructed from the sum of +one or more of the following values: +.Pp +.Bl -tag -width 6n -compact -offset indent +.It Li 4000 +(the setuid bit). +Executable files with this bit set +will run with effective uid set to the uid of the file owner. +Directories with this bit set will force all files and +sub-directories created in them to be owned by the directory owner +and not by the uid of the creating process, if the underlying file +system supports this feature: see +.Xr chmod 2 +and the +.Cm suiddir +option to +.Xr mount 8 . +.It Li 2000 +(the setgid bit). +Executable files with this bit set +will run with effective gid set to the gid of the file owner. +.It Li 1000 +(the sticky bit). +See +.Xr chmod 2 +and +.Xr sticky 7 . +.It Li 0400 +Allow read by owner. +.It Li 0200 +Allow write by owner. +.It Li 0100 +For files, allow execution by owner. +For directories, allow the owner to +search in the directory. +.It Li 0040 +Allow read by group members. +.It Li 0020 +Allow write by group members. +.It Li 0010 +For files, allow execution by group members. +For directories, allow +group members to search in the directory. +.It Li 0004 +Allow read by others. +.It Li 0002 +Allow write by others. +.It Li 0001 +For files, allow execution by others. +For directories allow others to +search in the directory. +.El +.Pp +For example, the absolute mode that permits read, write and execute by +the owner, read and execute by group members, read and execute by +others, and no set-uid or set-gid behaviour is 755 +(400+200+100+040+010+004+001). +.Pp +The symbolic mode is described by the following grammar: +.Bd -literal -offset indent +mode ::= clause [, clause ...] +clause ::= [who ...] [action ...] action +action ::= op [perm ...] +who ::= a | u | g | o +op ::= + | \- | = +perm ::= r | s | t | w | x | X | u | g | o +.Ed +.Pp +The +.Ar who +symbols ``u'', ``g'', and ``o'' specify the user, group, and other parts +of the mode bits, respectively. +The +.Ar who +symbol ``a'' is equivalent to ``ugo''. +.Pp +The +.Ar perm +symbols represent the portions of the mode bits as follows: +.Pp +.Bl -tag -width Ds -compact -offset indent +.It r +The read bits. +.It s +The set-user-ID-on-execution and set-group-ID-on-execution bits. +.It t +The sticky bit. +.It w +The write bits. +.It x +The execute/search bits. +.It X +The execute/search bits if the file is a directory or any of the +execute/search bits are set in the original (unmodified) mode. +Operations with the +.Ar perm +symbol ``X'' are only meaningful in conjunction with the +.Ar op +symbol ``+'', and are ignored in all other cases. +.It u +The user permission bits in the original mode of the file. +.It g +The group permission bits in the original mode of the file. +.It o +The other permission bits in the original mode of the file. +.El +.Pp +The +.Ar op +symbols represent the operation performed, as follows: +.Bl -tag -width 4n +.It + +If no value is supplied for +.Ar perm , +the ``+'' operation has no effect. +If no value is supplied for +.Ar who , +each permission bit specified in +.Ar perm , +for which the corresponding bit in the file mode creation mask +(see +.Xr umask 2 ) +is clear, is set. +Otherwise, the mode bits represented by the specified +.Ar who +and +.Ar perm +values are set. +.It \&\- +If no value is supplied for +.Ar perm , +the ``\-'' operation has no effect. +If no value is supplied for +.Ar who , +each permission bit specified in +.Ar perm , +for which the corresponding bit in the file mode creation mask +is set, is cleared. +Otherwise, the mode bits represented by the specified +.Ar who +and +.Ar perm +values are cleared. +.It = +The mode bits specified by the +.Ar who +value are cleared, or, if no +.Ar who +value is specified, the owner, group +and other mode bits are cleared. +Then, if no value is supplied for +.Ar who , +each permission bit specified in +.Ar perm , +for which the corresponding bit in the file mode creation mask +is clear, is set. +Otherwise, the mode bits represented by the specified +.Ar who +and +.Ar perm +values are set. +.El +.Pp +Each +.Ar clause +specifies one or more operations to be performed on the mode +bits, and each operation is applied to the mode bits in the +order specified. +.Pp +Operations upon the other permissions only (specified by the symbol +``o'' by itself), in combination with the +.Ar perm +symbols ``s'' or ``t'', are ignored. +.Pp +The ``w'' permission on directories will permit file creation, relocation, +and copy into that directory. +Files created within the directory itself will inherit its group ID. +.Sh EXAMPLES +.Bl -tag -width "u=rwx,go=u-w" -compact +.It Li 644 +make a file readable by anyone and writable by the owner only. +.Pp +.It Li go-w +deny write permission to group and others. +.Pp +.It Li =rw,+X +set the read and write permissions to the usual defaults, but +retain any execute permissions that are currently set. +.Pp +.It Li +X +make a directory or file searchable/executable by everyone if it is +already searchable/executable by anyone. +.Pp +.It Li 755 +.It Li u=rwx,go=rx +.It Li u=rwx,go=u-w +make a file readable/executable by everyone and writable by the owner only. +.Pp +.It Li go= +clear all mode bits for group and others. +.Pp +.It Li g=u-w +set the group bits equal to the user bits, but clear the group write bit. +.El +.Sh COMPATIBILITY +The +.Fl v +option is non-standard and its use in scripts is not recommended. +.Sh SEE ALSO +.Xr chflags 1 , +.Xr install 1 , +.Xr setfacl 1 , +.Xr chmod 2 , +.Xr stat 2 , +.Xr umask 2 , +.Xr fts 3 , +.Xr setmode 3 , +.Xr sticky 7 , +.Xr symlink 7 , +.Xr chown 8 , +.Xr mount 8 +.Sh STANDARDS +The +.Nm +utility is expected to be +.St -p1003.2 +compatible with the exception of the +.Ar perm +symbol +.Dq t +which is not included in that standard. +.Sh HISTORY +A +.Nm +command appeared in +.At v1 . +.Sh BUGS +There is no +.Ar perm +option for the naughty bits of a horse. diff --git a/chmod.c b/chmod.c new file mode 100644 index 0000000000..faae1c6ca3 --- /dev/null +++ b/chmod.c @@ -0,0 +1,438 @@ +/*- + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Copyright (c) 2026 + * Project Tick. All rights reserved. + * + * 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. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * 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/param.h> +#include <sys/stat.h> + +#include <dirent.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <signal.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "mode.h" + +#ifndef SIGINFO +#define SIGINFO SIGUSR1 +#endif + +#ifndef NODEV +#define NODEV ((dev_t)-1) +#endif + +#ifndef ALLPERMS +#define ALLPERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) +#endif + +struct chmod_options { + int fflag; + int hflag; + int recursive; + int vflag; + bool root_follow; + bool follow_links; +}; + +struct visit { + dev_t dev; + ino_t ino; +}; + +struct visit_set { + struct visit *items; + size_t len; + size_t cap; +}; + +static volatile sig_atomic_t siginfo; + +static void usage(void) __attribute__((noreturn)); +static void siginfo_handler(int sig); +static bool stat_path(const char *path, bool follow, struct stat *st); +static bool should_skip_acl_check(const char *path, int hflag, dev_t dev); +static int apply_mode(const char *display_path, const char *apply_path, + const struct stat *st, const struct chmod_options *opts, void *set, + bool dereference); +static int walk_path(const char *path, const struct chmod_options *opts, + void *set, bool dereference, struct visit_set *visited); +static int walk_dir(const char *path, const struct chmod_options *opts, + void *set, struct visit_set *visited); +static bool visited_contains(const struct visit_set *visited, dev_t dev, + ino_t ino); +static bool visited_add(struct visit_set *visited, dev_t dev, ino_t ino); +static void visited_pop(struct visit_set *visited); +static char *join_path(const char *parent, const char *name); + +static void +siginfo_handler(int sig) +{ + (void)sig; + siginfo = 1; +} + +int +main(int argc, char *argv[]) +{ + struct chmod_options opts; + struct visit_set visited; + void *set; + int Hflag, Lflag, Rflag, ch, rval; + char *mode; + int i; + + set = NULL; + memset(&opts, 0, sizeof(opts)); + memset(&visited, 0, sizeof(visited)); + Hflag = Lflag = Rflag = 0; + + while ((ch = getopt(argc, argv, "HLPRXfghorstuvwx")) != -1) + switch (ch) { + case 'H': + Hflag = 1; + Lflag = 0; + break; + case 'L': + Lflag = 1; + Hflag = 0; + break; + case 'P': + Hflag = Lflag = 0; + break; + case 'R': + Rflag = 1; + break; + case 'f': + opts.fflag = 1; + break; + case 'h': + opts.hflag = 1; + break; + case 'g': case 'o': case 'r': case 's': + case 't': case 'u': case 'w': case 'X': case 'x': + if (argv[optind - 1][0] == '-' && + argv[optind - 1][1] == ch && + argv[optind - 1][2] == '\0') + --optind; + goto done; + case 'v': + opts.vflag++; + break; + case '?': + default: + usage(); + } +done: + argv += optind; + argc -= optind; + + if (argc < 2) + usage(); + + (void)signal(SIGINFO, siginfo_handler); + + opts.recursive = Rflag; + if (Rflag) { + if (opts.hflag) + errx(1, "the -R and -h options may not be specified together."); + if (Lflag) { + opts.root_follow = true; + opts.follow_links = true; + } else if (Hflag) { + opts.root_follow = true; + opts.follow_links = false; + } else { + opts.root_follow = false; + opts.follow_links = false; + } + } else { + opts.root_follow = (opts.hflag == 0); + opts.follow_links = false; + } + + mode = *argv; + if ((set = mode_compile(mode)) == NULL) + errx(1, "invalid file mode: %s", mode); + + rval = 0; + for (i = 1; i < argc; i++) { + if (walk_path(argv[i], &opts, set, opts.root_follow, &visited) != 0) + rval = 1; + } + + mode_free(set); + free(visited.items); + return (rval); +} + +static int +walk_path(const char *path, const struct chmod_options *opts, void *set, + bool dereference, struct visit_set *visited) +{ + struct stat st; + int rval; + + if (!stat_path(path, dereference, &st)) { + warn("%s", path); + return (1); + } + + rval = apply_mode(path, path, &st, opts, set, dereference); + if (rval != 0 && opts->fflag) + rval = 0; + + if (!opts->recursive || !S_ISDIR(st.st_mode)) + return (rval); + + if (visited_contains(visited, st.st_dev, st.st_ino)) { + warnx("%s: directory causes a cycle", path); + return (1); + } + if (!visited_add(visited, st.st_dev, st.st_ino)) { + if (!opts->fflag) + warnx("%s: insufficient memory", path); + return (1); + } + + if (walk_dir(path, opts, set, visited) != 0) + rval = 1; + visited_pop(visited); + return (rval); +} + +static int +walk_dir(const char *path, const struct chmod_options *opts, void *set, + struct visit_set *visited) +{ + DIR *dirp; + struct dirent *dp; + int rval; + + dirp = opendir(path); + if (dirp == NULL) { + warn("%s", path); + return (1); + } + + rval = 0; + while ((dp = readdir(dirp)) != NULL) { + char *child; + bool dereference; + struct stat st; + + if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) + continue; + + child = join_path(path, dp->d_name); + if (child == NULL) { + if (!opts->fflag) + warnx("%s: insufficient memory", path); + rval = 1; + break; + } + + dereference = opts->follow_links; + if (opts->follow_links) { + if (!stat_path(child, true, &st)) { + warn("%s", child); + free(child); + rval = 1; + continue; + } + } + + if (walk_path(child, opts, set, dereference, visited) != 0) + rval = 1; + free(child); + } + + if (closedir(dirp) != 0) { + warn("%s", path); + rval = 1; + } + + return (rval); +} + +static int +apply_mode(const char *display_path, const char *apply_path, const struct stat *st, + const struct chmod_options *opts, void *set, bool dereference) +{ + mode_t newmode; + int atflag; + + newmode = mode_apply(set, st->st_mode); + if (should_skip_acl_check(apply_path, opts->hflag, st->st_dev) && + (newmode & ALLPERMS) == (st->st_mode & ALLPERMS)) + return (0); + + atflag = dereference ? 0 : AT_SYMLINK_NOFOLLOW; + if (fchmodat(AT_FDCWD, apply_path, newmode, atflag) == -1) { + if (!opts->fflag) + warn("%s", display_path); + return (1); + } + + if (opts->vflag || siginfo) { + (void)printf("%s", display_path); + if (opts->vflag > 1 || siginfo) { + char before[12], after[12]; + + strmode(st->st_mode, before); + strmode((st->st_mode & S_IFMT) | newmode, after); + (void)printf(": 0%o [%s] -> 0%o [%s]", + st->st_mode, before, + (st->st_mode & S_IFMT) | newmode, after); + } + (void)printf("\n"); + siginfo = 0; + } + + return (0); +} + +static bool +stat_path(const char *path, bool follow, struct stat *st) +{ + if (follow) + return (stat(path, st) == 0); + return (lstat(path, st) == 0); +} + +static bool +should_skip_acl_check(const char *path, int hflag, dev_t dev) +{ + static dev_t previous_dev = NODEV; + static int supports_acls = -1; + + if (previous_dev != dev) { + previous_dev = dev; + supports_acls = 0; +#ifdef _PC_ACL_NFS4 + { + int ret; + + if (hflag) + ret = lpathconf(path, _PC_ACL_NFS4); + else + ret = pathconf(path, _PC_ACL_NFS4); + if (ret > 0) + supports_acls = 1; + else if (ret < 0 && errno != EINVAL) + warn("%s", path); + } +#else + (void)hflag; + (void)path; +#endif + } + + return (supports_acls == 0); +} + +static bool +visited_contains(const struct visit_set *visited, dev_t dev, ino_t ino) +{ + size_t i; + + for (i = 0; i < visited->len; i++) { + if (visited->items[i].dev == dev && visited->items[i].ino == ino) + return (true); + } + return (false); +} + +static bool +visited_add(struct visit_set *visited, dev_t dev, ino_t ino) +{ + struct visit *new_items; + + if (visited->len == visited->cap) { + size_t new_cap; + + new_cap = visited->cap == 0 ? 16 : visited->cap * 2; + new_items = reallocarray(visited->items, new_cap, + sizeof(*visited->items)); + if (new_items == NULL) + return (false); + visited->items = new_items; + visited->cap = new_cap; + } + + visited->items[visited->len].dev = dev; + visited->items[visited->len].ino = ino; + visited->len++; + return (true); +} + +static void +visited_pop(struct visit_set *visited) +{ + if (visited->len > 0) + visited->len--; +} + +static char * +join_path(const char *parent, const char *name) +{ + char *path; + size_t parent_len, name_len; + bool need_slash; + + parent_len = strlen(parent); + name_len = strlen(name); + need_slash = parent_len > 0 && parent[parent_len - 1] != '/'; + path = malloc(parent_len + need_slash + name_len + 1); + if (path == NULL) + return (NULL); + + memcpy(path, parent, parent_len); + if (need_slash) + path[parent_len++] = '/'; + memcpy(path + parent_len, name, name_len + 1); + return (path); +} + +static void +usage(void) +{ + (void)fprintf(stderr, + "usage: chmod [-fhv] [-R [-H | -L | -P]] mode file ...\n"); + exit(1); +} diff --git a/mode.c b/mode.c new file mode 100644 index 0000000000..03f3538f0b --- /dev/null +++ b/mode.c @@ -0,0 +1,424 @@ +#include "mode.h" + +#include <ctype.h> +#include <errno.h> +#include <limits.h> +#include <signal.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <unistd.h> + +typedef struct bitcmd { + char cmd; + char cmd2; + mode_t bits; +} bitcmd_t; + +#define SET_LEN 6 +#define SET_LEN_INCR 4 + +#define CMD2_CLR 0x01 +#define CMD2_SET 0x02 +#define CMD2_GBITS 0x04 +#define CMD2_OBITS 0x08 +#define CMD2_UBITS 0x10 + +#define STANDARD_BITS (S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO) + +static mode_t get_current_umask(void); +static bitcmd_t *addcmd(bitcmd_t *set, mode_t op, mode_t who, mode_t oparg, + mode_t mask); +static void compress_mode(bitcmd_t *set); + +mode_t +mode_apply(const void *compiled, mode_t old_mode) +{ + const bitcmd_t *set; + mode_t clear_value, new_mode, value; + + set = compiled; + new_mode = old_mode; + for (value = 0;; set++) { + switch (set->cmd) { + case 'u': + value = (new_mode & S_IRWXU) >> 6; + goto common; + case 'g': + value = (new_mode & S_IRWXG) >> 3; + goto common; + case 'o': + value = new_mode & S_IRWXO; +common: + if (set->cmd2 & CMD2_CLR) { + clear_value = (set->cmd2 & CMD2_SET) ? S_IRWXO : value; + if (set->cmd2 & CMD2_UBITS) + new_mode &= ~((clear_value << 6) & set->bits); + if (set->cmd2 & CMD2_GBITS) + new_mode &= ~((clear_value << 3) & set->bits); + if (set->cmd2 & CMD2_OBITS) + new_mode &= ~(clear_value & set->bits); + } + if (set->cmd2 & CMD2_SET) { + if (set->cmd2 & CMD2_UBITS) + new_mode |= (value << 6) & set->bits; + if (set->cmd2 & CMD2_GBITS) + new_mode |= (value << 3) & set->bits; + if (set->cmd2 & CMD2_OBITS) + new_mode |= value & set->bits; + } + break; + case '+': + new_mode |= set->bits; + break; + case '-': + new_mode &= ~set->bits; + break; + case 'X': + if (old_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH)) + new_mode |= set->bits; + break; + case '\0': + default: + return (new_mode); + } + } +} + +void * +mode_compile(const char *mode_string) +{ + char op, *endp; + bitcmd_t *endset, *saveset, *set; + mode_t mask, perm, perm_xbits, who; + long perm_value; + unsigned int setlen; + int saved_errno; + int equal_done; + + if (*mode_string == '\0') { + errno = EINVAL; + return (NULL); + } + + mask = ~get_current_umask(); + setlen = SET_LEN + 2; + set = malloc(setlen * sizeof(*set)); + if (set == NULL) + return (NULL); + saveset = set; + endset = set + (setlen - 2); + + if (isdigit((unsigned char)*mode_string)) { + errno = 0; + perm_value = strtol(mode_string, &endp, 8); + if (*endp != '\0') { + errno = EINVAL; + goto fail; + } + if (errno == ERANGE && + (perm_value == LONG_MAX || perm_value == LONG_MIN)) { + goto fail; + } + if (perm_value & ~(STANDARD_BITS | S_ISVTX)) { + errno = EINVAL; + goto fail; + } + perm = (mode_t)perm_value; + set = addcmd(set, '=', STANDARD_BITS | S_ISVTX, perm, mask); + set->cmd = '\0'; + return (saveset); + } + + equal_done = 0; + for (;;) { + for (who = 0;; mode_string++) { + switch (*mode_string) { + case 'a': + who |= STANDARD_BITS; + break; + case 'u': + who |= S_ISUID | S_IRWXU; + break; + case 'g': + who |= S_ISGID | S_IRWXG; + break; + case 'o': + who |= S_IRWXO; + break; + default: + goto getop; + } + } +getop: + op = *mode_string++; + if (op != '+' && op != '-' && op != '=') { + errno = EINVAL; + goto fail; + } + if (op == '=') + equal_done = 0; + + who &= ~S_ISVTX; + for (perm = 0, perm_xbits = 0;; mode_string++) { + switch (*mode_string) { + case 'r': + perm |= S_IRUSR | S_IRGRP | S_IROTH; + break; + case 'w': + perm |= S_IWUSR | S_IWGRP | S_IWOTH; + break; + case 'x': + perm |= S_IXUSR | S_IXGRP | S_IXOTH; + break; + case 'X': + perm_xbits = S_IXUSR | S_IXGRP | S_IXOTH; + break; + case 's': + if (who == 0 || (who & ~S_IRWXO) != 0) + perm |= S_ISUID | S_ISGID; + break; + case 't': + if (who == 0 || (who & ~S_IRWXO) != 0) { + who |= S_ISVTX; + perm |= S_ISVTX; + } + break; + case 'u': + case 'g': + case 'o': + if (perm != 0) { + set = addcmd(set, op, who, perm, mask); + perm = 0; + } + if (op == '=') + equal_done = 1; + if (op == '+' && perm_xbits != 0) { + set = addcmd(set, 'X', who, perm_xbits, mask); + perm_xbits = 0; + } + set = addcmd(set, *mode_string, who, op, mask); + break; + default: + if (perm != 0 || (op == '=' && !equal_done)) { + if (op == '=') + equal_done = 1; + set = addcmd(set, op, who, perm, mask); + perm = 0; + } + if (perm_xbits != 0) { + set = addcmd(set, 'X', who, perm_xbits, mask); + perm_xbits = 0; + } + goto apply; + } + + if (set >= endset) { + ptrdiff_t offset; + bitcmd_t *newset; + + offset = set - saveset; + setlen += SET_LEN_INCR; + newset = realloc(saveset, setlen * sizeof(*newset)); + if (newset == NULL) + goto fail; + saveset = newset; + set = newset + offset; + endset = newset + (setlen - 2); + } + } +apply: + if (*mode_string == '\0') + break; + if (*mode_string != ',') { + errno = EINVAL; + goto fail; + } + mode_string++; + } + + set->cmd = '\0'; + compress_mode(saveset); + return (saveset); + +fail: + saved_errno = errno; + free(saveset); + errno = saved_errno; + return (NULL); +} + +void +mode_free(void *compiled) +{ + free(compiled); +} + +void +strmode(mode_t mode, char *buf) +{ + switch (mode & S_IFMT) { + case S_IFDIR: + *buf++ = 'd'; + break; + case S_IFCHR: + *buf++ = 'c'; + break; + case S_IFBLK: + *buf++ = 'b'; + break; + case S_IFREG: + *buf++ = '-'; + break; + case S_IFLNK: + *buf++ = 'l'; + break; + case S_IFSOCK: + *buf++ = 's'; + break; +#ifdef S_IFIFO + case S_IFIFO: + *buf++ = 'p'; + break; +#endif + default: + *buf++ = '?'; + break; + } + + *buf++ = (mode & S_IRUSR) ? 'r' : '-'; + *buf++ = (mode & S_IWUSR) ? 'w' : '-'; + switch (mode & (S_IXUSR | S_ISUID)) { + case 0: *buf++ = '-'; break; + case S_IXUSR: *buf++ = 'x'; break; + case S_ISUID: *buf++ = 'S'; break; + default: *buf++ = 's'; break; + } + *buf++ = (mode & S_IRGRP) ? 'r' : '-'; + *buf++ = (mode & S_IWGRP) ? 'w' : '-'; + switch (mode & (S_IXGRP | S_ISGID)) { + case 0: *buf++ = '-'; break; + case S_IXGRP: *buf++ = 'x'; break; + case S_ISGID: *buf++ = 'S'; break; + default: *buf++ = 's'; break; + } + *buf++ = (mode & S_IROTH) ? 'r' : '-'; + *buf++ = (mode & S_IWOTH) ? 'w' : '-'; + switch (mode & (S_IXOTH | S_ISVTX)) { + case 0: *buf++ = '-'; break; + case S_IXOTH: *buf++ = 'x'; break; + case S_ISVTX: *buf++ = 'T'; break; + default: *buf++ = 't'; break; + } + *buf++ = ' '; + *buf = '\0'; +} + +static mode_t +get_current_umask(void) +{ + sigset_t all_signals, original_signals; + mode_t mask; + + sigfillset(&all_signals); + (void)sigprocmask(SIG_BLOCK, &all_signals, &original_signals); + mask = umask(0); + (void)umask(mask); + (void)sigprocmask(SIG_SETMASK, &original_signals, NULL); + return (mask); +} + +static bitcmd_t * +addcmd(bitcmd_t *set, mode_t op, mode_t who, mode_t oparg, mode_t mask) +{ + switch (op) { + case '=': + set->cmd = '-'; + set->bits = who ? who : STANDARD_BITS; + set++; + op = '+'; + /* FALLTHROUGH */ + case '+': + case '-': + case 'X': + set->cmd = op; + set->cmd2 = 0; + set->bits = (who ? who : mask) & oparg; + break; + case 'u': + case 'g': + case 'o': + set->cmd = op; + if (who != 0) { + set->cmd2 = ((who & S_IRUSR) ? CMD2_UBITS : 0) | + ((who & S_IRGRP) ? CMD2_GBITS : 0) | + ((who & S_IROTH) ? CMD2_OBITS : 0); + set->bits = (mode_t)~0; + } else { + set->cmd2 = CMD2_UBITS | CMD2_GBITS | CMD2_OBITS; + set->bits = mask; + } + + if (oparg == '+') + set->cmd2 |= CMD2_SET; + else if (oparg == '-') + set->cmd2 |= CMD2_CLR; + else if (oparg == '=') + set->cmd2 |= CMD2_SET | CMD2_CLR; + break; + } + + return (set + 1); +} + +static void +compress_mode(bitcmd_t *set) +{ + bitcmd_t *next_set; + int clear_bits, op, set_bits, x_bits; + + for (next_set = set;;) { + while ((op = next_set->cmd) != '+' && op != '-' && op != 'X') { + *set++ = *next_set++; + if (op == '\0') + return; + } + + for (set_bits = clear_bits = x_bits = 0;; next_set++) { + op = next_set->cmd; + if (op == '-') { + clear_bits |= next_set->bits; + set_bits &= ~next_set->bits; + x_bits &= ~next_set->bits; + } else if (op == '+') { + set_bits |= next_set->bits; + clear_bits &= ~next_set->bits; + x_bits &= ~next_set->bits; + } else if (op == 'X') { + x_bits |= next_set->bits & ~set_bits; + } else { + break; + } + } + + if (clear_bits != 0) { + set->cmd = '-'; + set->cmd2 = 0; + set->bits = clear_bits; + set++; + } + if (set_bits != 0) { + set->cmd = '+'; + set->cmd2 = 0; + set->bits = set_bits; + set++; + } + if (x_bits != 0) { + set->cmd = 'X'; + set->cmd2 = 0; + set->bits = x_bits; + set++; + } + } +} diff --git a/mode.h b/mode.h new file mode 100644 index 0000000000..2fe04221dd --- /dev/null +++ b/mode.h @@ -0,0 +1,8 @@ +#pragma once + +#include <sys/types.h> + +void *mode_compile(const char *mode_string); +mode_t mode_apply(const void *compiled, mode_t old_mode); +void mode_free(void *compiled); +void strmode(mode_t mode, char *buf); diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000000..a27b7c3aec --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,3 @@ +ATF_TESTS_SH+= chmod_test + +.include <bsd.test.mk> diff --git a/tests/Makefile.depend b/tests/Makefile.depend new file mode 100644 index 0000000000..11aba52f82 --- /dev/null +++ b/tests/Makefile.depend @@ -0,0 +1,10 @@ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + + +.include <dirdeps.mk> + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif diff --git a/tests/chmod_test.sh b/tests/chmod_test.sh new file mode 100755 index 0000000000..faf1d70113 --- /dev/null +++ b/tests/chmod_test.sh @@ -0,0 +1,179 @@ +# +# Copyright (c) 2017 Dell EMC +# All rights reserved. +# +# Copyright (c) 2026 +# Project Tick. All rights reserved. +# +# 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. +# + +get_filesystem() +{ + local mountpoint=$1 + + df -T $mountpoint | tail -n 1 | cut -wf 2 +} + +atf_test_case RH_flag +RH_flag_head() +{ + atf_set "descr" "Verify that setting modes recursively via -R doesn't " \ + "affect symlinks specified via the arguments when -H " \ + "is specified" +} +RH_flag_body() +{ + atf_check mkdir -m 0777 -p A/B + atf_check ln -s B A/C + atf_check chmod -h 0777 A/C + atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C + atf_check chmod -RH 0700 A + atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C + atf_check chmod -RH 0600 A/C + atf_check -o inline:'40700\n40600\n120700\n' stat -f '%p' A A/B A/C +} + +atf_test_case RL_flag +RL_flag_head() +{ + atf_set "descr" "Verify that setting modes recursively via -R doesn't " \ + "affect symlinks specified via the arguments when -L " \ + "is specified" +} +RL_flag_body() +{ + atf_check mkdir -m 0777 -p A/B + atf_check ln -s B A/C + atf_check chmod -h 0777 A/C + atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C + atf_check chmod -RL 0700 A + atf_check -o inline:'40700\n40700\n120777\n' stat -f '%p' A A/B A/C + atf_check chmod -RL 0600 A/C + atf_check -o inline:'40700\n40600\n120777\n' stat -f '%p' A A/B A/C +} + +atf_test_case RP_flag +RP_flag_head() +{ + atf_set "descr" "Verify that setting modes recursively via -R doesn't " \ + "affect symlinks specified via the arguments when -P " \ + "is specified" +} +RP_flag_body() +{ + atf_check mkdir -m 0777 -p A/B + atf_check ln -s B A/C + atf_check chmod -h 0777 A/C + atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C + atf_check chmod -RP 0700 A + atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C + atf_check chmod -RP 0600 A/C + atf_check -o inline:'40700\n40700\n120600\n' stat -f '%p' A A/B A/C +} + +atf_test_case f_flag cleanup +f_flag_head() +{ + atf_set "descr" "Verify that setting a mode for a file with -f " \ + "doesn't emit an error message/exit with a non-zero " \ + "code" +} + +f_flag_body() +{ + atf_check truncate -s 0 foo bar + atf_check chmod 0750 foo bar + case "$(get_filesystem .)" in + zfs) + atf_expect_fail "ZFS doesn't support UF_IMMUTABLE; returns EPERM - bug 221189" + ;; + esac + atf_check chflags uchg foo + atf_check -e not-empty -s not-exit:0 chmod 0700 foo bar + atf_check -o inline:'100750\n100700\n' stat -f '%p' foo bar + atf_check -s exit:0 chmod -f 0600 foo bar + atf_check -o inline:'100750\n100600\n' stat -f '%p' foo bar +} + +f_flag_cleanup() +{ + chflags 0 foo || : +} + +atf_test_case h_flag +h_flag_head() +{ + atf_set "descr" "Verify that setting a mode for a file with -f " \ + "doesn't emit an error message/exit with a non-zero " \ + "code" +} + +h_flag_body() +{ + atf_check truncate -s 0 foo + atf_check chmod 0600 foo + atf_check -o inline:'100600\n' stat -f '%p' foo + umask 0077 + atf_check ln -s foo bar + atf_check -o inline:'100600\n120700\n' stat -f '%p' foo bar + atf_check chmod -h 0500 bar + atf_check -o inline:'100600\n120500\n' stat -f '%p' foo bar + atf_check chmod 0660 bar + atf_check -o inline:'100660\n120500\n' stat -f '%p' foo bar +} + +atf_test_case v_flag +v_flag_head() +{ + atf_set "descr" "Verify that setting a mode with -v emits the file when " \ + "doesn't emit an error message/exit with a non-zero " \ + "code" +} +v_flag_body() +{ + atf_check truncate -s 0 foo bar + atf_check chmod 0600 foo + atf_check chmod 0750 bar + case "$(get_filesystem .)" in + zfs) + atf_expect_fail "ZFS updates mode for foo unnecessarily - bug 221188" + ;; + esac + atf_check -o 'inline:bar\n' chmod -v 0600 foo bar + atf_check chmod -v 0600 foo bar + for f in foo bar; do + echo "$f: 0100600 [-rw------- ] -> 0100700 [-rwx------ ]"; + done > output.txt + atf_check -o file:output.txt chmod -vv 0700 foo bar + atf_check chmod -vv 0700 foo bar +} + +atf_init_test_cases() +{ + atf_add_test_case RH_flag + atf_add_test_case RL_flag + atf_add_test_case RP_flag + atf_add_test_case f_flag + atf_add_test_case h_flag + atf_add_test_case v_flag +} diff --git a/tests/test.sh b/tests/test.sh new file mode 100644 index 0000000000..87d984d1d3 --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,81 @@ +#!/bin/sh +# +# Copyright (c) 2026 +# Project Tick. All rights reserved. +# +# 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. +# +set -eu + +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +CHMOD_BIN=${CHMOD_BIN:-"$ROOT/out/chmod"} +TMPDIR=${TMPDIR:-/tmp} +WORKDIR=$(mktemp -d "$TMPDIR/chmod-test.XXXXXX") +trap 'rm -rf "$WORKDIR"' EXIT INT TERM + +fail() { + printf '%s\n' "FAIL: $1" >&2 + exit 1 +} + +assert_mode() { + expected=$1 + path=$2 + actual=$(stat -c '%a' "$path") + [ "$actual" = "$expected" ] || fail "$path mode expected $expected got $actual" +} + +[ -x "$CHMOD_BIN" ] || fail "missing binary: $CHMOD_BIN" + +touch "$WORKDIR/file" +"$CHMOD_BIN" 600 "$WORKDIR/file" +assert_mode 600 "$WORKDIR/file" + +"$CHMOD_BIN" u+x "$WORKDIR/file" +assert_mode 700 "$WORKDIR/file" + +"$CHMOD_BIN" go-rwx "$WORKDIR/file" +assert_mode 700 "$WORKDIR/file" + +"$CHMOD_BIN" a+r "$WORKDIR/file" +assert_mode 744 "$WORKDIR/file" + +mkdir -p "$WORKDIR/tree/sub" +touch "$WORKDIR/tree/sub/file" +"$CHMOD_BIN" -R 755 "$WORKDIR/tree" +assert_mode 755 "$WORKDIR/tree" +assert_mode 755 "$WORKDIR/tree/sub" +assert_mode 755 "$WORKDIR/tree/sub/file" + +verbose_output=$("$CHMOD_BIN" -vv 700 "$WORKDIR/file") +case $verbose_output in + *"$WORKDIR/file: "*) ;; + *) fail "verbose output missing file path" ;; +esac + +usage_output=$("$CHMOD_BIN" 2>&1 || true) +case $usage_output in + *"usage: chmod "* ) ;; + *) fail "usage output missing" ;; +esac + +printf '%s\n' "PASS" |
