Ignore:
Timestamp:
Jun 26, 2020, 7:16:26 PM (5 years ago)
Author:
bird
Message:

kmk: Avoid setting umask just to get it, store the current value in a global variable (g_fUMask). The umask(0777) call in cp.c raced other code (kmk_append) that created files and directories, leaving us with read-only files sometimes.

Location:
trunk/src/kmk/kmkbuiltin
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/chmod.c

    r3220 r3389  
    278278
    279279#ifdef KMK_BUILTIN_STANDALONE
     280mode_t g_fUMask;
    280281int main(int argc, char **argv, char **envp)
    281282{
    282283    KMKBUILTINCTX Ctx = { "kmk_chmod", NULL };
     284    umask(g_fUMask = umask(0077));
    283285    return kmk_builtin_chmod(argc, argv, envp, &Ctx);
    284286}
  • trunk/src/kmk/kmkbuiltin/cp.c

    r3221 r3389  
    6969#include <sys/stat.h>
    7070
     71#include <assert.h>
    7172#include "err.h"
    7273#include <errno.h>
     
    175176volatile sig_atomic_t g_cp_info;
    176177#endif
     178
     179extern mode_t g_fUMask;
    177180
    178181
     
    423426
    424427#ifdef KMK_BUILTIN_STANDALONE
     428mode_t g_fUMask;
    425429int main(int argc, char **argv, char **envp)
    426430{
    427431    KMKBUILTINCTX Ctx = { "kmk_cp", NULL };
     432    umask(g_fUMask = umask(0077));
    428433    return kmk_builtin_cp(argc, argv, envp, &Ctx);
    429434}
     
    445450         * permissions on created directories when not using -p.
    446451         */
    447         mask = ~umask(0777);
    448         umask(~mask);
     452        mask = g_fUMask;
     453        assert(mask == umask(mask));
     454        mask = ~mask;
    449455
    450456        if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
  • trunk/src/kmk/kmkbuiltin/install.c

    r3241 r3389  
    396396
    397397#ifdef KMK_BUILTIN_STANDALONE
     398mode_t g_fUMask;
    398399int main(int argc, char **argv, char **envp)
    399400{
    400401        KMKBUILTINCTX Ctx = { "kmk_install", NULL };
     402        umask(g_fUMask = umask(0077));
    401403        return kmk_builtin_install(argc, argv, envp, &Ctx);
    402404}
  • trunk/src/kmk/kmkbuiltin/mkdir.c

    r3215 r3389  
    4949#include "err.h"
    5050#include <errno.h>
     51#include <assert.h>
    5152#ifndef _MSC_VER
    5253# include <libgen.h>
     
    8081};
    8182
     83extern mode_t g_fUMask;
    8284
    8385extern void * bsd_setmode(const char *p);
     
    167169
    168170#ifdef KMK_BUILTIN_STANDALONE
     171mode_t g_fUMask;
    169172int main(int argc, char **argv, char **envp)
    170173{
    171174    KMKBUILTINCTX Ctx = { "kmk_mkdir", NULL };
     175    umask(g_fUMask = umask(0077));
    172176    return kmk_builtin_mkdir(argc, argv, envp, &Ctx);
    173177}
     
    240244                         * instead of doing chmod's.
    241245                         */
    242                         oumask = umask(0);
     246#ifdef KMK_BUILTIN_STANDALONE
     247                        oumask = umask(0077);
     248#else
     249                        oumask = g_fUMask;
     250                        assert(oumask == umask(g_fUMask));
     251#endif
    243252                        numask = oumask & ~(S_IWUSR | S_IXUSR);
    244                         (void)umask(numask);
     253                        if (numask != oumask)
     254                            (void)umask(numask);
    245255                        first = 0;
    246256                }
    247                 if (last)
     257                if (last && oumask != numask)
    248258                        (void)umask(oumask);
    249259                if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
     
    255265                                        retval = 1;
    256266                                        break;
    257                                 } else if (!S_ISDIR(sb.st_mode)) {
     267                                }
     268                                if (!S_ISDIR(sb.st_mode)) {
    258269                                        if (last)
    259270                                                errno = EEXIST;
     
    274285                    *p = '/';
    275286        }
    276         if (!first && !last)
     287        if (!first && !last && oumask != numask)
    277288                (void)umask(oumask);
    278289        return (retval);
  • trunk/src/kmk/kmkbuiltin/setmode.c

    r2113 r3389  
    100100#endif /* !S_ISTXT */
    101101
     102extern mode_t g_fUMask; /* Initialize in main() and keep up to date. */
     103
     104
    102105/*
    103106 * Given the old mode and an array of bitcmd structures, apply the operations
     
    223226        (void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
    224227#endif
    225         (void)umask(mask = umask(0));
     228        mask = g_fUMask;
     229        assert(mask == umask(g_fUMask));
    226230        mask = ~mask;
    227231#ifndef _MSC_VER
Note: See TracChangeset for help on using the changeset viewer.