add/remove-shell: use O_TRUNC instead of O_EXCL
authorDenys Vlasenko <dvlasenk@redhat.com>
Wed, 20 Oct 2010 13:14:32 +0000 (15:14 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Wed, 20 Oct 2010 13:14:32 +0000 (15:14 +0200)
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
loginutils/add-remove-shell.c

index 986fe57c5bbc321e33afd184b375bb1d0e24fcca..757e5050366dd84ec34d99f4a9676b75583fd341 100644 (file)
@@ -63,11 +63,19 @@ int add_remove_shell_main(int argc UNUSED_PARAM, char **argv)
        orig_fp = fopen_for_read(orig_fn);
 
        new_fn = xasprintf("%s.tmp", orig_fn);
-       xmove_fd(xopen(new_fn, O_WRONLY | O_CREAT | O_EXCL), STDOUT_FILENO);
+       /*
+        * O_TRUNC or O_EXCL? At the first glance, O_EXCL looks better,
+        * since it prevents races. But: (1) it requires a retry loop,
+        * (2) if /etc/shells.tmp is *stale*, then retry loop
+        * with O_EXCL will never succeed - it should have a timeout,
+        * after which it should revert to O_TRUNC.
+        * For now, I settle for O_TRUNC instead.
+        */
+       xmove_fd(xopen(new_fn, O_WRONLY | O_CREAT | O_TRUNC), STDOUT_FILENO);
 
        /* TODO:
        struct stat sb;
-       fstat(fileno(orig_fp), &sb);
+       xfstat(fileno(orig_fp), &sb);
        xfchown(STDOUT_FILENO, sb.st_uid, sb.st_gid);
        xfchmod(STDOUT_FILENO, sb.st_mode);
        */