2 * add-shell and remove-shell implementation for busybox
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 * Written by Alexander Shishkin <virtuoso@slind.org>
7 * Licensed under GPLv2 or later, see the LICENSE file in this source tree
10 //config:config ADD_SHELL
11 //config: bool "add-shell (2.8 kb)"
12 //config: default y if DESKTOP
14 //config: Add shells to /etc/shells.
16 //config:config REMOVE_SHELL
17 //config: bool "remove-shell (2.7 kb)"
18 //config: default y if DESKTOP
20 //config: Remove shells from /etc/shells.
22 // APPLET_NOEXEC:name main location suid_type help
23 //applet:IF_ADD_SHELL( APPLET_NOEXEC(add-shell , add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, add_shell ))
24 //applet:IF_REMOVE_SHELL(APPLET_NOEXEC(remove-shell, add_remove_shell, BB_DIR_USR_SBIN, BB_SUID_DROP, remove_shell))
26 //kbuild:lib-$(CONFIG_ADD_SHELL) += add-remove-shell.o
27 //kbuild:lib-$(CONFIG_REMOVE_SHELL) += add-remove-shell.o
29 //usage:#define add_shell_trivial_usage
31 //usage:#define add_shell_full_usage "\n\n"
32 //usage: "Add SHELLs to /etc/shells"
34 //usage:#define remove_shell_trivial_usage
36 //usage:#define remove_shell_full_usage "\n\n"
37 //usage: "Remove SHELLs from /etc/shells"
41 #define SHELLS_FILE "/etc/shells"
43 #define REMOVE_SHELL (ENABLE_REMOVE_SHELL && (!ENABLE_ADD_SHELL || applet_name[0] == 'r'))
44 #define ADD_SHELL (ENABLE_ADD_SHELL && (!ENABLE_REMOVE_SHELL || applet_name[0] == 'a'))
46 #define dont_add ((char*)(uintptr_t)1)
48 int add_remove_shell_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
49 int add_remove_shell_main(int argc UNUSED_PARAM, char **argv)
60 orig_fn = xmalloc_follow_symlinks(SHELLS_FILE);
63 orig_fp = fopen_for_read(orig_fn);
65 xfstat(fileno(orig_fp), &sb, orig_fn);
68 new_fn = xasprintf("%s.tmp", orig_fn);
70 * O_TRUNC or O_EXCL? At the first glance, O_EXCL looks better,
71 * since it prevents races. But: (1) it requires a retry loop,
72 * (2) if /etc/shells.tmp is *stale*, then retry loop
73 * with O_EXCL will never succeed - it should have a timeout,
74 * after which it should revert to O_TRUNC.
75 * For now, I settle for O_TRUNC instead.
77 xmove_fd(xopen3(new_fn, O_WRONLY | O_CREAT | O_TRUNC, sb.st_mode), STDOUT_FILENO);
79 xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid);
83 /* Copy old file, possibly skipping removed shell names */
85 while ((line = xmalloc_fgetline(orig_fp)) != NULL) {
88 if (*cpp != dont_add && strcmp(*cpp, line) == 0) {
89 /* Old file has this shell name */
91 /* we are remove-shell */
92 /* delete this name by not copying it */
95 /* we are add-shell */
96 /* mark this name as "do not add" */
101 /* copy shell name from old to new file */
106 if (ENABLE_FEATURE_CLEAN_UP)
113 if (*cpp != dont_add)
119 /* Ensure we wrote out everything */
120 if (fclose(stdout) != 0) {
122 bb_perror_msg_and_die("%s: write error", new_fn);
125 /* Small hole: if rename fails, /etc/shells.tmp is not removed */
126 xrename(new_fn, orig_fn);
128 if (ENABLE_FEATURE_CLEAN_UP) {