syslogd: fix "readpath bug" by using readlink instead
authorDenis Vlasenko <vda.linux@googlemail.com>
Sun, 11 Feb 2007 16:19:28 +0000 (16:19 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Sun, 11 Feb 2007 16:19:28 +0000 (16:19 -0000)
libbb: rename xgetcwd and xreadlink

15 files changed:
applets/busybox.c
archival/tar.c
coreutils/ls.c
coreutils/pwd.c
coreutils/stat.c
debianutils/readlink.c
include/libbb.h
libbb/copy_file.c
libbb/lineedit.c
libbb/simplify_path.c
libbb/xgetcwd.c
libbb/xreadlink.c
shell/hush.c
shell/lash.c
sysklogd/syslogd.c

index 99af9ca024c6e12b35d6d6bf063599e8c6b82451..0387d79b7c9b968bcc49ae9a4c01dcc9bf5fcc3c 100644 (file)
@@ -92,7 +92,7 @@ int busybox_main(int argc, char **argv)
 
                /* link */
 // XXX: FIXME: this is broken. Why not just use argv[0] ?
-               busybox = xreadlink("/proc/self/exe");
+               busybox = xmalloc_readlink_or_warn("/proc/self/exe");
                if (!busybox)
                        return 1;
                install_links(busybox, use_symbolic_links);
index d8e36749e5a4c375eeb0ca899624a3868dd3bda5..57da0b6b83d68d181fad47a234063c48382a40a4 100644 (file)
@@ -273,8 +273,8 @@ static int writeTarHeader(struct TarBallInfo *tbInfo,
                                        tbInfo->hlInfo->name, 0);
 #endif
        } else if (S_ISLNK(statbuf->st_mode)) {
-               char *lpath = xreadlink(fileName);
-               if (!lpath)             /* Already printed err msg inside xreadlink() */
+               char *lpath = xmalloc_readlink_or_warn(fileName);
+               if (!lpath)
                        return FALSE;
                header.typeflag = SYMTYPE;
                strncpy(header.linkname, lpath, sizeof(header.linkname));
index 798bc8201c3e95a2773ecebf0a11fe2e0295ae59..34fae502691359d9772812e37bd8cfe8c2ba07a2 100644 (file)
@@ -664,7 +664,7 @@ static int list_single(struct dnode *dn)
                        break;
                case LIST_SYMLINK:
                        if (S_ISLNK(dn->dstat.st_mode)) {
-                               char *lpath = xreadlink(dn->fullname);
+                               char *lpath = xmalloc_readlink_or_warn(dn->fullname);
                                if (!lpath) break;
                                printf(" -> ");
 #if ENABLE_FEATURE_LS_FILETYPES || ENABLE_FEATURE_LS_COLOR
index b4599b4f32dbcf9e48421c6503ede61fdf6fe08b..d96f6a8e5219fb67b9624b7d47884ec2c7beee8f 100644 (file)
@@ -16,7 +16,8 @@ int pwd_main(int argc, char **argv)
 {
        char *buf;
 
-       if ((buf = xgetcwd(NULL)) != NULL) {
+       buf = xrealloc_getcwd_or_warn(NULL);
+       if (buf != NULL) {
                puts(buf);
                fflush_stdout_and_exit(EXIT_SUCCESS);
        }
index ff14a15992413080d1db1e9e5d6fef46c3084c65..20ade947257a7dff9d52b0aa90796e8c67f2a357 100644 (file)
@@ -188,7 +188,7 @@ static void print_stat(char *pformat, size_t buf_len, char m,
        case 'N':
                strncat(pformat, "s", buf_len);
                if (S_ISLNK(statbuf->st_mode)) {
-                       char *linkname = xreadlink(filename);
+                       char *linkname = xmalloc_readlink_or_warn(filename);
                        if (linkname == NULL) {
                                bb_perror_msg("cannot read symbolic link '%s'", filename);
                                return;
@@ -477,7 +477,7 @@ static int do_stat(char const *filename, char const *format)
                pw_ent = getpwuid(statbuf.st_uid);
 
                if (S_ISLNK(statbuf.st_mode))
-                       linkname = xreadlink(filename);
+                       linkname = xmalloc_readlink_or_warn(filename);
                if (linkname)
                        printf("  File: \"%s\" -> \"%s\"\n", filename, linkname);
                else
index 8f9cfe4313e1a39e9e3dee97d8698e2eccb32c28..1a2c1efb7d64dc47e2ba0f3e36f86d1944a7a738 100644 (file)
@@ -38,7 +38,7 @@ int readlink_main(int argc, char **argv)
        if (opt) {
                buf = realpath(fname, bb_common_bufsiz1);
        } else {
-               buf = xreadlink(fname);
+               buf = xmalloc_readlink_or_warn(fname);
        }
 
        if (!buf)
index 218a1935c2b21a863e79b2de52dfb5939dc20663..077b6584047485312cac466ec972a20ce91e5692 100644 (file)
@@ -258,8 +258,8 @@ extern int ndelay_off(int fd);
 extern DIR *xopendir(const char *path);
 extern DIR *warn_opendir(const char *path);
 
-char *xgetcwd(char *cwd);
-char *xreadlink(const char *path);
+char *xrealloc_getcwd_or_warn(char *cwd);
+char *xmalloc_readlink_or_warn(const char *path);
 char *xmalloc_realpath(const char *path);
 extern void xstat(const char *filename, struct stat *buf);
 extern pid_t spawn(char **argv);
index 632064eaa58e8e9b8eb325fdebfd0302b40bd19d..bd785b71c6a6d992138efc97b43ff4aa1d1114bd 100644 (file)
@@ -233,7 +233,7 @@ int copy_file(const char *source, const char *dest, int flags)
                } else if (S_ISLNK(source_stat.st_mode)) {
                        char *lpath;
 
-                       lpath = xreadlink(source);
+                       lpath = xmalloc_readlink_or_warn(source);
                        if (symlink(lpath, dest) < 0) {
                                bb_perror_msg("cannot create symlink '%s'", dest);
                                free(lpath);
index 937d70d1ffdd76852491f0427a4b2976a4832cda..16256f726f90fe67004403730cb7f25534423445 100644 (file)
@@ -1090,7 +1090,7 @@ static void parse_prompt(const char *prmt_ptr)
        size_t cur_prmt_len = 0;
        char flg_not_length = '[';
        char *prmt_mem_ptr = xzalloc(1);
-       char *pwd_buf = xgetcwd(0);
+       char *pwd_buf = xrealloc_getcwd_or_warn(NULL);
        char buf2[PATH_MAX + 1];
        char buf[2];
        char c;
index b714c66b6c586e96636c9b352d6df7e0efffd150..7e68e3911562d9621c4d49b2e4282f227eb04674 100644 (file)
@@ -16,7 +16,7 @@ char *bb_simplify_path(const char *path)
        if (path[0] == '/')
                start = xstrdup(path);
        else {
-               s = xgetcwd(NULL);
+               s = xrealloc_getcwd_or_warn(NULL);
                start = concat_path_file(s, path);
                free(s);
        }
index 0ac450d3bbed94d61d96d8f0698f944940650a4d..ec1d8f7a4231669a6bff3534c311a28444d600b4 100644 (file)
@@ -18,7 +18,7 @@
 */
 
 char *
-xgetcwd(char *cwd)
+xrealloc_getcwd_or_warn(char *cwd)
 {
        char *ret;
        unsigned path_max;
@@ -26,7 +26,7 @@ xgetcwd(char *cwd)
        path_max = (unsigned) PATH_MAX;
        path_max += 2;                /* The getcwd docs say to do this. */
 
-       if (cwd==0)
+       if (cwd == NULL)
                cwd = xmalloc(path_max);
 
        while ((ret = getcwd(cwd, path_max)) == NULL && errno == ERANGE) {
index fb67cdef189701967b7bbbbc3ba415f862eed81b..18a8b9467bdde152f7a9f55da324aeb42dfaace0 100644 (file)
@@ -11,7 +11,7 @@
  * yourself. You have been warned.
  */
 
-char *xreadlink(const char *path)
+char *xmalloc_readlink_or_warn(const char *path)
 {
        enum { GROWBY = 80 }; /* how large we will grow strings by */
 
index 5737240750f72973d481d32c1efba5f06cc13ac8..7658aebed8af9a6b426ab76888fba78aee19cf83 100644 (file)
@@ -423,8 +423,8 @@ static const struct built_in_command bltins[] = {
 static const char *set_cwd(void)
 {
        if (cwd == bb_msg_unknown)
-               cwd = NULL;     /* xgetcwd(arg) called free(arg) */
-       cwd = xgetcwd((char *)cwd);
+               cwd = NULL;     /* xrealloc_getcwd_or_warn(arg) called free(arg) */
+       cwd = xrealloc_getcwd_or_warn((char *)cwd);
        if (!cwd)
                cwd = bb_msg_unknown;
        return cwd;
index 502e0d8292904c56af3357ebfb683595f6614b31..09067fda0943a1d0c09ddaed196a37e94ed73b11 100644 (file)
@@ -217,7 +217,7 @@ static int builtin_cd(struct child_prog *child)
                bb_perror_msg("cd: %s", newdir);
                return EXIT_FAILURE;
        }
-       cwd = xgetcwd((char *)cwd);
+       cwd = xrealloc_getcwd_or_warn((char *)cwd);
        if (!cwd)
                cwd = bb_msg_unknown;
        return EXIT_SUCCESS;
@@ -342,7 +342,7 @@ static int builtin_jobs(struct child_prog *child)
 /* built-in 'pwd' handler */
 static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy)
 {
-       cwd = xgetcwd((char *)cwd);
+       cwd = xrealloc_getcwd_or_warn((char *)cwd);
        if (!cwd)
                cwd = bb_msg_unknown;
        puts(cwd);
@@ -1569,7 +1569,7 @@ int lash_main(int argc_l, char **argv_l)
        }
 
        /* initialize the cwd -- this is never freed...*/
-       cwd = xgetcwd(0);
+       cwd = xrealloc_getcwd_or_warn(NULL);
        if (!cwd)
                cwd = bb_msg_unknown;
 
index 97ddf09b5fa7e6ea2e60ae79e5eb2badf3e9ff9d..53290f1ccfcbbb7d46141dd1446ec3391ce7f9fd 100644 (file)
@@ -24,9 +24,6 @@
 
 #define DEBUG 0
 
-/* Path to the unix socket */
-static const char *dev_log_name;
-
 /* Path for the file where all log messages are written */
 static const char *logFilePath = "/var/log/messages";
 static int logFD = -1;
@@ -446,7 +443,6 @@ static void quit_signal(int sig)
 {
        timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"syslogd exiting", 0);
        puts("syslogd exiting");
-       unlink(dev_log_name);
        if (ENABLE_FEATURE_IPC_SYSLOG)
                ipcsyslog_cleanup();
        exit(1);
@@ -464,9 +460,9 @@ static void do_syslogd(void) ATTRIBUTE_NORETURN;
 static void do_syslogd(void)
 {
        struct sockaddr_un sunx;
-       socklen_t addr_len;
        int sock_fd;
        fd_set fds;
+       char *dev_log_name;
 
        /* Set up signal handlers */
        signal(SIGINT, quit_signal);
@@ -480,22 +476,33 @@ static void do_syslogd(void)
        signal(SIGALRM, do_mark);
        alarm(markInterval);
 
-       dev_log_name = xmalloc_realpath(_PATH_LOG);
-       if (!dev_log_name)
-               dev_log_name = _PATH_LOG;
-
-       /* Unlink old /dev/log (or object it points to) */
-       unlink(dev_log_name);
-
        memset(&sunx, 0, sizeof(sunx));
        sunx.sun_family = AF_UNIX;
-       strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
+       strcpy(sunx.sun_path, "/dev/log");
+
+       /* Unlink old /dev/log or object it points to. */
+       /* (if it exists, bind will fail) */
+       logmode = LOGMODE_NONE;
+       dev_log_name = xmalloc_readlink_or_warn("/dev/log");
+       logmode = LOGMODE_STDIO;
+       if (dev_log_name) {
+               int fd = xopen(".", O_NONBLOCK);
+               xchdir("/dev");
+               /* we do not check whether this is a link also */
+               unlink(dev_log_name);
+               fchdir(fd);
+               close(fd);
+               safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path));
+               free(dev_log_name);
+       } else {
+               unlink("/dev/log");
+       }
+
        sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0);
-       addr_len = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
-       xbind(sock_fd, (struct sockaddr *) &sunx, addr_len);
+       xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx));
 
-       if (chmod(dev_log_name, 0666) < 0) {
-               bb_perror_msg_and_die("cannot set permission on %s", dev_log_name);
+       if (chmod("/dev/log", 0666) < 0) {
+               bb_perror_msg_and_die("cannot set permission on /dev/log");
        }
        if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) {
                ipcsyslog_init();