From d9cf7ac781c1274d6d23fa5674b6b90baaf4cf61 Mon Sep 17 00:00:00 2001 From: Bernhard Reutner-Fischer Date: Wed, 12 Apr 2006 18:39:58 +0000 Subject: [PATCH] - patch from Denis Vlasenko to add and use bb_xchdir() --- archival/dpkg_deb.c | 18 ++++-------------- archival/tar.c | 7 +++---- archival/unzip.c | 5 ++--- coreutils/chroot.c | 18 +++--------------- include/libbb.h | 1 + libbb/Makefile.in | 2 +- libbb/bb_xchdir.c | 17 +++++++++++++++++ miscutils/crond.c | 4 +--- miscutils/crontab.c | 9 +++------ miscutils/makedevs.c | 18 ++++++++---------- networking/httpd.c | 4 +--- procps/top.c | 5 ++--- 12 files changed, 46 insertions(+), 62 deletions(-) create mode 100644 libbb/bb_xchdir.c diff --git a/archival/dpkg_deb.c b/archival/dpkg_deb.c index fa89e44a0..fc8b9221d 100644 --- a/archival/dpkg_deb.c +++ b/archival/dpkg_deb.c @@ -1,18 +1,8 @@ +/* vi: set sw=4 ts=4: */ /* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * dpkg-deb packs, unpacks and provides information about Debian archives. * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ #include #include @@ -100,7 +90,7 @@ int dpkg_deb_main(int argc, char **argv) } if (extract_dir) { mkdir(extract_dir, 0777); - chdir(extract_dir); + chdir(extract_dir); /* error check? */ } unpack_ar_archive(ar_archive); diff --git a/archival/tar.c b/archival/tar.c index 9d0a7c5e4..dcc2f163e 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -19,9 +19,8 @@ * * Based in part on the tar implementation from busybox-0.28 * Copyright (C) 1995 Bruce Perens - * This is free software under the GNU General Public License. * - * Licensed under GPL v2 (or later), see file LICENSE in this tarball. + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ #include @@ -834,8 +833,8 @@ int tar_main(int argc, char **argv) } } - if ((base_dir) && (chdir(base_dir))) - bb_perror_msg_and_die("Couldnt chdir to %s", base_dir); + if (base_dir) + bb_xchdir(base_dir); /* create an archive */ if (ENABLE_FEATURE_TAR_CREATE && (opt & CTX_CREATE)) { diff --git a/archival/unzip.c b/archival/unzip.c index bb7197d3e..b9dff1985 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -236,9 +236,8 @@ int unzip_main(int argc, char **argv) } /* Change dir if necessary */ - if (base_dir && chdir(base_dir)) { - bb_perror_msg_and_die("Cannot chdir"); - } + if (base_dir) + bb_xchdir(base_dir); if (verbosity != v_silent) printf("Archive: %s\n", src_fn); diff --git a/coreutils/chroot.c b/coreutils/chroot.c index 62257021d..8ad680c02 100644 --- a/coreutils/chroot.c +++ b/coreutils/chroot.c @@ -4,20 +4,7 @@ * * Copyright (C) 1999-2004 by Erik Andersen * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ /* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */ @@ -35,9 +22,10 @@ int chroot_main(int argc, char **argv) } ++argv; - if (chroot(*argv) || (chdir("/"))) { + if (chroot(*argv)) { bb_perror_msg_and_die("cannot change root directory to %s", *argv); } + bb_xchdir("/"); ++argv; if (argc == 2) { diff --git a/include/libbb.h b/include/libbb.h index d70c71cc0..e1ac912f6 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -149,6 +149,7 @@ extern int bb_xsocket(int domain, int type, int protocol); extern void bb_xdaemon(int nochdir, int noclose); extern void bb_xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen); extern void bb_xlisten(int s, int backlog); +extern void bb_xchdir(const char *path); #define BB_GETOPT_ERROR 0x80000000UL extern const char *bb_opt_complementally; diff --git a/libbb/Makefile.in b/libbb/Makefile.in index f05bf80ed..eac14c60c 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in @@ -30,7 +30,7 @@ LIBBB-y:= \ trim.c u_signal_names.c vdprintf.c verror_msg.c \ vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c xstat.c \ xgethostbyname.c xgethostbyname2.c xreadlink.c xregcomp.c xgetlarg.c \ - bb_xsocket.c bb_xdaemon.c bb_xbind.c bb_xlisten.c \ + bb_xsocket.c bb_xdaemon.c bb_xbind.c bb_xlisten.c bb_xchdir.c \ get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \ getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \ perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \ diff --git a/libbb/bb_xchdir.c b/libbb/bb_xchdir.c new file mode 100644 index 000000000..65b01534d --- /dev/null +++ b/libbb/bb_xchdir.c @@ -0,0 +1,17 @@ +/* vi: set sw=4 ts=4: */ +/* + * bb_xchdir.c - a chdir() which dies on failure with error message + * + * Copyright (C) 2006 Denis Vlasenko + * + * Licensed under LGPL, see file docs/lesser.txt in this tarball for details. + */ +#include +#include "libbb.h" + +void bb_xchdir(const char *path) +{ + if (chdir(path)) + bb_perror_msg_and_die("chdir(%s)", path); +} + diff --git a/miscutils/crond.c b/miscutils/crond.c index 06b8769fa..4d479bc0d 100644 --- a/miscutils/crond.c +++ b/miscutils/crond.c @@ -197,9 +197,7 @@ int crond_main(int ac, char **av) * change directory */ - if (chdir(CDir) != 0) { - bb_perror_msg_and_die("%s", CDir); - } + bb_xchdir(CDir); signal(SIGHUP, SIG_IGN); /* hmm.. but, if kill -HUP original * version - his died. ;( */ diff --git a/miscutils/crontab.c b/miscutils/crontab.c index df94c855a..703d01ecc 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c @@ -1,3 +1,4 @@ +/* vi: set sw=4 ts=4: */ /* * CRONTAB * @@ -148,8 +149,7 @@ crontab_main(int ac, char **av) * Change directory to our crontab directory */ - if (chdir(CDir) < 0) - bb_perror_msg_and_die("cannot change dir to %s", CDir); + bb_xchdir(CDir); /* * Handle options as appropriate @@ -358,10 +358,7 @@ ChangeUser(const char *user, short dochdir) if (dochdir) { if (chdir(pas->pw_dir) < 0) { bb_perror_msg("chdir failed: %s %s", user, pas->pw_dir); - if (chdir(TMPDIR) < 0) { - bb_perror_msg_and_die("chdir failed: %s %s", user, TMPDIR); - return(-1); - } + bb_xchdir(TMPDIR); } } return(pas->pw_uid); diff --git a/miscutils/makedevs.c b/miscutils/makedevs.c index 999b99331..24d07ebe0 100644 --- a/miscutils/makedevs.c +++ b/miscutils/makedevs.c @@ -1,4 +1,11 @@ /* vi: set sw=4 ts=4: */ +/* + * public domain -- Dave 'Kill a Cop' Cinege + * + * makedevs + * Make ranges of device files quickly. + * known bugs: can't deal with alpha ranges + */ #include #include @@ -12,13 +19,6 @@ #include "busybox.h" #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF -/* - * public domain -- Dave 'Kill a Cop' Cinege - * - * makedevs - * Make ranges of device files quickly. - * known bugs: can't deal with alpha ranges - */ int makedevs_main(int argc, char **argv) { mode_t mode; @@ -94,9 +94,7 @@ int makedevs_main(int argc, char **argv) bb_error_msg_and_die("root directory not specified"); } - if (chdir(rootdir) != 0) { - bb_perror_msg_and_die("could not chdir to %s", rootdir); - } + bb_xchdir(rootdir); umask(0); diff --git a/networking/httpd.c b/networking/httpd.c index 0f6174140..df280ccf5 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -2077,9 +2077,7 @@ int httpd_main(int argc, char *argv[]) #endif #endif - if(chdir(home_httpd)) { - bb_perror_msg_and_die("can`t chdir to %s", home_httpd); - } + bb_xchdir(home_httpd); #ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY server = openServer(); # ifdef CONFIG_FEATURE_HTTPD_SETUID diff --git a/procps/top.c b/procps/top.c index afd419ba5..d140924d8 100644 --- a/procps/top.c +++ b/procps/top.c @@ -1,3 +1,4 @@ +/* vi: set sw=4 ts=4: */ /* * A tiny 'top' utility. * @@ -468,9 +469,7 @@ int top_main(int argc, char **argv) col = 35; #endif /* change to /proc */ - if (chdir("/proc") < 0) { - bb_perror_msg_and_die("chdir('/proc')"); - } + bb_xchdir("/proc"); #ifdef CONFIG_FEATURE_USE_TERMIOS tcgetattr(0, (void *) &initial_settings); memcpy(&new_settings, &initial_settings, sizeof(struct termios));