From 0965568950706cb265a672501a52143cf9cee10b Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 19 Nov 2026 08:03:35 -0600 Subject: [PATCH 1/4] doc: design umask modulefile command Describe how the file mode creation mask of the shell session is set by a modulefile, how the mask in effect is read by modulecmd.tcl and when the previous mask is restored on unload. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- .hunspell.en.dic | 3 + doc/source/design/umask.rst | 144 ++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 doc/source/design/umask.rst diff --git a/.hunspell.en.dic b/.hunspell.en.dic index 15e354f84..d7acd730f 100644 --- a/.hunspell.en.dic +++ b/.hunspell.en.dic @@ -519,6 +519,7 @@ indepth inhibitErrorReport init initStateClockSeconds +initStateUmask initStateUsergroups initadd initclear @@ -1674,3 +1675,5 @@ TMOD getPathElementWeight withdrawPriorityDemands getPathWeightList +0o0077 +o0700 diff --git a/doc/source/design/umask.rst b/doc/source/design/umask.rst new file mode 100644 index 000000000..426811909 --- /dev/null +++ b/doc/source/design/umask.rst @@ -0,0 +1,144 @@ +.. _umask: + +umask modulefile command +======================== + +This design document describes the :mfcmd:`umask` Tcl command that sets the +file mode creation mask of the shell session a modulefile is loaded into. + +The file mode creation mask is a property of the shell process. It cannot be +set through an environment variable, so it requires a dedicated modulefile +command that produces the shell code changing it, like :mfcmd:`chdir` does for +the current working directory. + +Command specification +--------------------- + +The command has following syntax:: + + umask value + +*value* is the mask to set, expressed as an octal number made of one to four +digits. An error is raised otherwise. Symbolic modes are not supported: only +the *sh* family shells understand them and they are relative to the mask in +effect, whereas an octal number is rendered the same way for every shell. + +*value* is normalized to a four-digit number (``22`` becomes ``0022``). This +canonical form is the one recorded, compared and rendered. + +This command is only available in modulefile evaluation contexts. It is +advised not to use it in global rc files. + +Evaluation modes +---------------- + +* ``load``: the mask is set +* ``unload``: the previous mask is restored (see `Restoring previous mask`_) +* ``display``: the command and its argument are reported +* ``scan``: the mask is recorded, as written in modulefile, to be matched by + the ``umask`` extra specifier +* ``help``, ``test``, ``whatis``, ``refresh``: *no-operation* + +Nothing has to be done on ``refresh`` mode. Unlike shell aliases or functions, +the file mode creation mask is inherited by the processes started from the +shell session, sub-shells included. + +When a modulefile calls ``umask`` several times, the last mask set is the one +retained. + +If the evaluation of the modulefile fails after a ``umask`` command, the mask +is not changed, like for any other environment change of this modulefile. + +Reading current mask +-------------------- + +The mask in effect has to be known to restore it later on. Tcl provides no +command to read it. :file:`modulecmd.tcl` is run as a child process of the +shell session, so it inherits its mask. The ``umask`` state holds this value, +which is fetched the first time it is needed: + +* by the ``initStateUmask`` command of the Modules Tcl extension library, that + relies on the :manpage:`umask(2)` system call +* by running the ``umask`` command through :command:`sh` when the extension + library is not available + +The mask is only read when a modulefile sets a mask and no mask has been saved +yet. Modulefiles that do not use ``umask`` are not impacted. + +Restoring previous mask +----------------------- + +Masks set by loaded modules are recorded in the :envvar:`__MODULES_UMASK` +environment variable. It has the same structure than the +:envvar:`__MODULES_PUSHENV_\` variables used by :mfcmd:`pushenv`: a +colon-separated stack whose first entry is the last mask set. An entry is +formed by the name of the module, an ampersand character and the mask. The +mask in effect prior the first ``umask`` command is saved in an entry with an +empty module name. + +On ``load``: + +* if stack is empty, the current mask is pushed with an empty module name +* any entry of the loading module is removed from the stack +* an entry for the loading module is pushed + +On ``unload``: + +* the entry of the unloading module is removed from the stack +* if this entry was on top of the stack and if the current mask equals the + mask recorded in it, the mask of the entry that is now on top is set +* if the only entry remaining in the stack is the one with an empty module + name, it is removed, which clears the stack + +The previous mask is thus restored only if the mask set by the unloading +module is still in effect. Two situations lead to keep the current mask: + +* another module has set a mask after the unloading one: its mask remains +* the mask has been changed in the shell session with the ``umask`` shell + command after the module load: this explicit choice is preserved + +The current mask is the one of the shell session, unless a modulefile +evaluated earlier in the same :command:`module` command run has set or +restored a mask. When several modules are unloaded at once (e.g., with +:subcmd:`purge` sub-command), each one finds the mask restored by the +previous one, so the stack is unwound up to the initial mask. + +Shell code produced +------------------- + +Mask is rendered after the change of current working directory. + ++-------------------------+------------------------------------+ +| Shell | Code for mask ``0077`` | ++=========================+====================================+ +| sh, bash, ksh, zsh, | ``umask 0077;`` | +| csh, tcsh, fish | | ++-------------------------+------------------------------------+ +| perl | ``umask(0077);`` | ++-------------------------+------------------------------------+ +| python | ``os.umask(0o0077)`` | ++-------------------------+------------------------------------+ +| ruby | ``File.umask(0077)`` | ++-------------------------+------------------------------------+ +| r | ``Sys.umask('0077')`` | ++-------------------------+------------------------------------+ +| lisp | ``(set-default-file-modes #o0700)``| ++-------------------------+------------------------------------+ + +Emacs Lisp expresses the setting as the permissions granted to new files, +which is the complement of the mask. + +No code is produced for *tcl*, *cmake*, *cmd* and *pwsh*: these shells have no +mean to change their own mask. The command is silently ignored for them, as +done for the other modulefile commands a shell cannot honor. + +Limitations +----------- + +* A session that gets the loaded environment of another session without + inheriting its mask (e.g., environment variables forwarded to a new login + session) sees the module loaded whereas its mask is not applied. +* Mask changes made by a shell script are not tracked by :mfcmd:`source-sh` + and :subcmd:`sh-to-mod`. + +.. vim:set tabstop=2 shiftwidth=2 expandtab autoindent: From f85d1f5a504355ffa805c946d6e21f685a31f0f0 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 19 Nov 2026 08:03:40 -0600 Subject: [PATCH 2/4] Add umask modulefile command The file mode creation mask is a property of the shell process that cannot be set through an environment variable. Setting it from a modulefile required to output shell-specific code with puts, which was not undone when the module was unloaded. The umask command takes the mask as an octal number and renders the code that sets it for the sh, csh and fish shells and for the perl, python, ruby, r and lisp languages. Other shells have no mean to change their own mask, so the command is ignored for them. Masks set by loaded modules are stacked in the __MODULES_UMASK environment variable, along with the mask in effect prior the first umask command. On unload, the previous mask is restored only if the mask set by the module is still the current one, to preserve a mask changed in the meantime by another module or by the user. Tcl cannot read the mask of its process. An initStateUmask command is added to the Tcl extension library for that purpose, with a fallback procedure that runs umask through sh when the library is not built. Modules setting a given mask can be searched with the new umask extra specifier. Fixes #649 Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- MIGRATING.rst | 31 +++ NEWS.rst | 8 + doc/source/changes.rst | 6 +- doc/source/design/extra-match-search.rst | 2 + doc/source/design/umask.rst | 3 + doc/source/module.rst | 23 +- doc/source/modulefile.rst | 110 +++++--- doc/source/other-implementations.rst | 4 +- lib/envmodules.c | 37 +++ lib/envmodules.h | 3 + share/emacs/lisp/modulefile-mode.el | 2 +- share/nagelfar/plugin_globalrc.tcl | 6 +- share/nagelfar/syntaxdb_modulefile.tcl | 2 + share/vim/syntax/modulefile.vim | 4 +- tcl/envmngt.tcl.in | 30 ++- tcl/init.tcl.in | 7 + tcl/interp.tcl.in | 3 +- tcl/main.tcl.in | 2 + tcl/mfcmd.tcl | 97 +++++++ tcl/modeval.tcl | 8 +- tcl/modscan.tcl | 4 + tcl/modspec.tcl | 4 +- testsuite/config/base-config.exp | 42 +++ testsuite/example/siteconfig.tcl-1 | 10 + testsuite/install.00-init/010-environ.exp | 3 +- testsuite/modulefiles.4/extra/1.0 | 3 + testsuite/modulefiles.5/umask/1 | 2 + testsuite/modulefiles.5/umask/2 | 2 + testsuite/modulefiles.5/umask/3 | 3 + testsuite/modules.00-init/005-init_ts.exp | 4 +- testsuite/modules.00-init/010-environ.exp | 3 +- .../modules.01-general/120-siteconfig.exp | 7 + testsuite/modules.50-cmds/760-umask.exp | 253 ++++++++++++++++++ .../modules.51-scan/041-extra_spec-env.exp | 10 + testsuite/modules.70-maint/220-config.exp | 2 +- testsuite/modules.70-maint/390-state.exp | 2 +- 36 files changed, 673 insertions(+), 69 deletions(-) create mode 100644 testsuite/modulefiles.5/umask/1 create mode 100644 testsuite/modulefiles.5/umask/2 create mode 100644 testsuite/modulefiles.5/umask/3 create mode 100644 testsuite/modules.50-cmds/760-umask.exp diff --git a/MIGRATING.rst b/MIGRATING.rst index 39aab8e41..acdc7d5f8 100644 --- a/MIGRATING.rst +++ b/MIGRATING.rst @@ -127,6 +127,37 @@ The :mfcmd:`module-hide-regex` and :mfcmd:`module-forbid-regex` commands are also introduced as aliases of :mfcmd:`module-hide` and :mfcmd:`module-forbid` with ``--regexp`` option set. +Setting file mode creation mask +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The :mfcmd:`umask` modulefile command is introduced to set the file mode +creation mask of the shell session. It takes the mask to apply as an octal +number. + +.. parsed-literal:: + + :ps:`$` cat /path/to/modulefiles/project/1.0 + #%Module + umask 0077 + :ps:`$` umask + 0022 + :ps:`$` module load project/1.0 + :ps:`$` umask + 0077 + +The mask in effect prior modulefile load is restored when modulefile is +unloaded, unless the mask of the shell session has been changed in the +meantime. + +.. parsed-literal:: + + :ps:`$` module unload project/1.0 + :ps:`$` umask + 0022 + +:mfcmd:`umask` applies to the shells and scripting languages that are able to +change their own mask. See the :ref:`Shell support` section for details. + v5.7 ---- diff --git a/NEWS.rst b/NEWS.rst index ec289aebc..b3777941d 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -83,6 +83,14 @@ Modules 5.8.0 (not yet released) *conflict unload* and *require via* mechanisms, one module loaded per module name, path entry reordering, *contains* search match and sticky modules kept with a warning on :subcmd:`purge`. +* Add the :mfcmd:`umask` modulefile command that sets the file mode creation + mask of the shell session when modulefile is loaded and restores the + previous mask when it is unloaded. Masks set by loaded modules are recorded + in the :envvar:`__MODULES_UMASK` environment variable. (fix issue #649) +* Add the ``umask`` extra specifier to search the modules that set a given + file mode creation mask. +* Lib: add the ``initStateUmask`` procedure to the Modules Tcl extension + library to fetch the file mode creation mask of the current process. .. _5.7 release notes: diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 5924cb2ca..4d551512e 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -1156,7 +1156,8 @@ The following environment variables appeared on Modules 5. | | :envvar:`MODULES_LIST_JSON_OUTPUT`, | | | :envvar:`MODULES_SPIDER_JSON_OUTPUT`, | | | :envvar:`__MODULES_PRIORITY_\`, | -| | :envvar:`MODULES_FAMILY_\_VERSION` | +| | :envvar:`MODULES_FAMILY_\_VERSION`, | +| | :envvar:`__MODULES_UMASK` | +------------+-----------------------------------------------------------------+ Modules Specific Tcl Commands @@ -1188,7 +1189,8 @@ The following modulefile Tcl commands appeared on Modules 5. +------------+-----------------------------------------------------------------+ | 5.8 | :mfcmd:`module-info modulename`, | | | :mfcmd:`module-info moduleversion`, | -| | :mfcmd:`module-forbid-regex`, :mfcmd:`module-hide-regex` | +| | :mfcmd:`module-forbid-regex`, :mfcmd:`module-hide-regex`, | +| | :mfcmd:`umask` | +------------+-----------------------------------------------------------------+ :mfcmd:`system`, :mfcmd:`is-used` diff --git a/doc/source/design/extra-match-search.rst b/doc/source/design/extra-match-search.rst index 8ef4d54d8..6fae2a58d 100644 --- a/doc/source/design/extra-match-search.rst +++ b/doc/source/design/extra-match-search.rst @@ -205,6 +205,8 @@ specifiers and their associated modulefile command. +--------------------+-----------------------------------------+ | chdir | chdir | +--------------------+-----------------------------------------+ +| umask | umask | ++--------------------+-----------------------------------------+ | family | family, provided-alias | +--------------------+-----------------------------------------+ | provide | provide, provided-alias | diff --git a/doc/source/design/umask.rst b/doc/source/design/umask.rst index 426811909..46bd92daa 100644 --- a/doc/source/design/umask.rst +++ b/doc/source/design/umask.rst @@ -132,6 +132,9 @@ No code is produced for *tcl*, *cmake*, *cmd* and *pwsh*: these shells have no mean to change their own mask. The command is silently ignored for them, as done for the other modulefile commands a shell cannot honor. +On Windows platform, an error is raised when ``umask`` is evaluated in +``load`` mode: there is no file mode creation mask to read or to set there. + Limitations ----------- diff --git a/doc/source/module.rst b/doc/source/module.rst index 46eac0fcd..c1d20f045 100644 --- a/doc/source/module.rst +++ b/doc/source/module.rst @@ -2506,8 +2506,10 @@ Module Sub-Commands In this evaluation mode only the :mfcmd:`complete`, :mfcmd:`set-alias`, :mfcmd:`set-function` and :mfcmd:`puts` modulefile commands will produce environment changes. Other modulefile commands that produce environment - changes (like :mfcmd:`setenv` or :mfcmd:`append-path`) are ignored during a - ``refresh`` evaluation as their changes should already be applied. + changes (like :mfcmd:`setenv`, :mfcmd:`append-path` or :mfcmd:`umask`) are + ignored during a ``refresh`` evaluation as their changes should already be + applied: like environment variables, the file mode creation mask is inherited + by derived shells. Only the loaded modules defining non-persistent environment changes are evaluated in ``refresh`` mode. Such loaded modules are listed in the @@ -3502,7 +3504,7 @@ an environment variable, a shell alias, a module specification, etc. Supported extra specifier *elements* are: * ``variant``, ``complete``, ``uncomplete``, ``set-alias``, ``unset-alias``, - ``set-function``, ``unset-function``, ``chdir``, ``tag`` + ``set-function``, ``unset-function``, ``chdir``, ``umask``, ``tag`` * ``setenv``, ``unsetenv``, ``append-path``, ``prepend-path``, ``remove-path`` and ``pushenv``: these elements related to environment variable handling may also be aliased ``envvar`` @@ -4866,6 +4868,21 @@ ENVIRONMENT than one time in the relative path-like variable +.. envvar:: __MODULES_UMASK + + Stack of file mode creation masks set by loaded modules. A colon-separated + list containing pairs of elements. A pair is formed by a loaded module name + followed by the mask set in this module with :mfcmd:`umask` command. An + ampersand character separates the two parts of the pair. + + First element in list corresponds to the lastly set mask. The mask in effect + prior the first evaluated :mfcmd:`umask` command is associated to an empty + module name to record it as a pair element in :envvar:`__MODULES_UMASK`. + + .. only:: html or latex + + .. versionadded:: 5.8 + .. envvar:: _LMFILES_ A colon separated list of the full pathname for all loaded *modulefiles*. diff --git a/doc/source/modulefile.rst b/doc/source/modulefile.rst index 45f733f51..96f5bdc52 100644 --- a/doc/source/modulefile.rst +++ b/doc/source/modulefile.rst @@ -1834,6 +1834,34 @@ the *modulefile* is being loaded. :file:`modulecmd.tcl` redirects stdout to stderr since stdout would be parsed by the evaluating shell. The exit status of the executed command is returned. +.. mfcmd:: umask value + + Set the file mode creation mask of the shell session to *value*, which is an + octal number made of one to four digits (e.g., ``022`` or ``0077``). An error + is raised if *value* is expressed in another form, like a symbolic mode. + + When a *modulefile* is unloaded, the mask that was in effect prior its load + is restored if the current mask of the shell session is still the one set by + this *modulefile*. If the mask has been changed since then, by another loaded + *modulefile* or by a direct call to the :command:`umask` shell command, it is + left untouched. + + Masks set by loaded modules are recorded in the :envvar:`__MODULES_UMASK` + environment variable. + + The file mode creation mask is inherited by the processes started from the + shell session, sub-shells included. A session that gets the environment of + another one by other means, without inheriting its mask, sees *modulefile* + loaded but does not have its mask applied. + + For some shells, changing the mask is not possible and the command has no + effect (see `Shell support`_ section). An error is raised if the command is + used on Windows platform. + + .. only:: html or latex + + .. versionadded:: 5.8 + .. mfcmd:: uname field Provide lookup of system information. Most *field* information are retrieved @@ -2733,47 +2761,47 @@ Depending on the "shell" kind, not all the environment changes that can be defined in modulefiles are supported. The following table summarizes the changes that are supported by the shells supported by :file:`modulecmd.tcl`. -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| | Environment variables | Shell alias | Shell functions | Command completion | :mfcmd:`chdir` | :mfcmd:`x-resource` | -| | (:mfcmd:`setenv`, | (:mfcmd:`set-alias`, | (:mfcmd:`set-function`, | (:mfcmd:`complete`, | | | -| | :mfcmd:`unsetenv`, | :mfcmd:`unset-alias`)| :mfcmd:`unset-function`)| :mfcmd:`uncomplete`)| | | -| | :mfcmd:`pushenv`, | | | | | | -| | :mfcmd:`append-path`, | | | | | | -| | :mfcmd:`prepend-path`,| | | | | | -| | :mfcmd:`remove-path`) | | | | | | -+========+=======================+======================+=========================+=====================+================+=====================+ -| sh | x | x | x | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| bash | x | x | x | x | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| ksh | x | x | x | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| zsh | x | x | x | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| csh | x | x | | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| tcsh | x | x | | x | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| fish | x | x | x | x | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| cmd | x | x | | | x | | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| pwsh | x | x | x | x | x | | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| python | x | | | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| perl | x | | | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| ruby | x | | | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| tcl | x | | | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| cmake | x | | | | | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| r | x | | | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ -| lisp | x | | | | x | x | -+--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+ ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| | Environment variables | Shell alias | Shell functions | Command completion | :mfcmd:`chdir` | :mfcmd:`x-resource` | :mfcmd:`umask` | +| | (:mfcmd:`setenv`, | (:mfcmd:`set-alias`, | (:mfcmd:`set-function`, | (:mfcmd:`complete`, | | | | +| | :mfcmd:`unsetenv`, | :mfcmd:`unset-alias`)| :mfcmd:`unset-function`)| :mfcmd:`uncomplete`)| | | | +| | :mfcmd:`pushenv`, | | | | | | | +| | :mfcmd:`append-path`, | | | | | | | +| | :mfcmd:`prepend-path`,| | | | | | | +| | :mfcmd:`remove-path`) | | | | | | | ++========+=======================+======================+=========================+=====================+================+=====================+================+ +| sh | x | x | x | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| bash | x | x | x | x | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| ksh | x | x | x | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| zsh | x | x | x | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| csh | x | x | | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| tcsh | x | x | | x | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| fish | x | x | x | x | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| cmd | x | x | | | x | | | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| pwsh | x | x | x | x | x | | | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| python | x | | | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| perl | x | | | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| ruby | x | | | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| tcl | x | | | | x | x | | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| cmake | x | | | | | x | | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| r | x | | | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ +| lisp | x | | | | x | x | x | ++--------+-----------------------+----------------------+-------------------------+---------------------+----------------+---------------------+----------------+ The :mfcmd:`source-sh` command evaluates a shell script and produces the modulefile commands corresponding to the environment changes made by this diff --git a/doc/source/other-implementations.rst b/doc/source/other-implementations.rst index 02f876c2b..033dba64e 100644 --- a/doc/source/other-implementations.rst +++ b/doc/source/other-implementations.rst @@ -119,8 +119,8 @@ the end of this section to map these equivalents. - :mfcmd:`getvariant`, :mfcmd:`is-saved`, :mfcmd:`is-used`, :mfcmd:`lsb-release`, :mfcmd:`module-tag`, :mfcmd:`module-warn`, :mfcmd:`modulepath-ignore`, :mfcmd:`modulepath-label`, - :mfcmd:`provide`, :mfcmd:`reportWarning`, :mfcmd:`uncomplete`, - :mfcmd:`variant`, :mfcmd:`x-resource` + :mfcmd:`provide`, :mfcmd:`reportWarning`, :mfcmd:`umask`, + :mfcmd:`uncomplete`, :mfcmd:`variant`, :mfcmd:`x-resource` See the :ref:`Compatibility with Lmod Tcl modulefile` section for details on how the implementation of the Tcl modulefile commands differ between Lmod and diff --git a/lib/envmodules.c b/lib/envmodules.c index 14b03b3a3..30550264d 100644 --- a/lib/envmodules.c +++ b/lib/envmodules.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -482,6 +483,39 @@ Envmodules_InitStateClockSecondsObjCmd( return TCL_OK; } +/*---------------------------------------------------------------------- + * + * Envmodules_InitStateUmaskObjCmd -- + * + * This function is invoked to return the file mode creation mask of + * current process, as a 4-digit octal number. + * + * Results: + * A standard Tcl result. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------*/ + +int +Envmodules_InitStateUmaskObjCmd( + ClientData dummy, /* Not used. */ + Tcl_Interp *interp, /* Current interpreter. */ + int objc, /* Number of arguments. */ + Tcl_Obj *const objv[]) /* Argument objects. */ +{ + mode_t mask; + + /* Mask can only be read by setting a new one, so restore it right + * after */ + mask = umask(0); + umask(mask); + + Tcl_SetObjResult(interp, Tcl_ObjPrintf("%04o", (unsigned int) mask)); + return TCL_OK; +} + /*---------------------------------------------------------------------- * * Envmodules_ParseDateTimeArgObjCmd -- @@ -603,6 +637,9 @@ Envmodules_Init( Tcl_CreateObjCommand(interp, "initStateClockSeconds", Envmodules_InitStateClockSecondsObjCmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL); + Tcl_CreateObjCommand(interp, "initStateUmask", + Envmodules_InitStateUmaskObjCmd, (ClientData) NULL, + (Tcl_CmdDeleteProc*) NULL); Tcl_CreateObjCommand(interp, "parseDateTimeArg", Envmodules_ParseDateTimeArgObjCmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL); diff --git a/lib/envmodules.h b/lib/envmodules.h index 0857e7d0c..ed5b472d6 100644 --- a/lib/envmodules.h +++ b/lib/envmodules.h @@ -43,6 +43,9 @@ MODULE_SCOPE int Envmodules_InitStateUsergroupsObjCmd (ClientData dummy, MODULE_SCOPE int Envmodules_InitStateClockSecondsObjCmd (ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]); +MODULE_SCOPE int Envmodules_InitStateUmaskObjCmd (ClientData dummy, + Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]); + MODULE_SCOPE int Envmodules_ParseDateTimeArgObjCmd (ClientData dummy, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]); diff --git a/share/emacs/lisp/modulefile-mode.el b/share/emacs/lisp/modulefile-mode.el index 90ecd8115..152acbe27 100644 --- a/share/emacs/lisp/modulefile-mode.el +++ b/share/emacs/lisp/modulefile-mode.el @@ -48,7 +48,7 @@ (define-derived-mode modulefile-mode tcl-mode "Modulefile" "Major mode for editing Modulefile scripts." (font-lock-add-keywords 'modulefile-mode - '(("\\<\\(add-property\\|always-load\\|append-path\\|chdir\\|complete\\|conflict\\|depends-on-any\\|depends-on\\|extensions\\|family\\|getenv\\|getvariant\\|haveDynamicMPATH\\|hide-modulefile\\|hide-version\\|is-avail\\|is-loaded\\|is-saved\\|is-used\\|lsb-release\\|module-alias\\|module-forbid-regex\\|module-forbid\\|module-help\\|module-hide-regex\\|module-hide\\|module-info\\|module-tag\\|module-version\\|module-virtual\\|module-warn\\|module-whatis\\|module\\|modulepath-ignore\\|modulepath-label\\|prepend-path\\|prereq-all\\|prereq-any\\|prereq\\|provide\\|pushenv\\|remove-path\\|reportError\\|reportWarning\\|require-fullname\\|set-alias\\|set-function\\|setenv\\|source-sh\\|system\\|uname\\|uncomplete\\|unset-alias\\|unset-function\\|unsetenv\\|variant\\|versioncmp\\|x-resource\\)\\>" . font-lock-keyword-face)) + '(("\\<\\(add-property\\|always-load\\|append-path\\|chdir\\|complete\\|conflict\\|depends-on-any\\|depends-on\\|extensions\\|family\\|getenv\\|getvariant\\|haveDynamicMPATH\\|hide-modulefile\\|hide-version\\|is-avail\\|is-loaded\\|is-saved\\|is-used\\|lsb-release\\|module-alias\\|module-forbid-regex\\|module-forbid\\|module-help\\|module-hide-regex\\|module-hide\\|module-info\\|module-tag\\|module-version\\|module-virtual\\|module-warn\\|module-whatis\\|module\\|modulepath-ignore\\|modulepath-label\\|prepend-path\\|prereq-all\\|prereq-any\\|prereq\\|provide\\|pushenv\\|remove-path\\|reportError\\|reportWarning\\|require-fullname\\|set-alias\\|set-function\\|setenv\\|source-sh\\|system\\|uname\\|umask\\|uncomplete\\|unset-alias\\|unset-function\\|unsetenv\\|variant\\|versioncmp\\|x-resource\\)\\>" . font-lock-keyword-face)) ) (font-lock-add-keywords 'modulefile-mode '(("\\<\\(ModulesVersion\\|ModulesCurrentModulefile\\|ModuleToolVersion\\|ModuleVariant\\|ModuleTool\\)\\>" . font-lock-preprocessor-face)) diff --git a/share/nagelfar/plugin_globalrc.tcl b/share/nagelfar/plugin_globalrc.tcl index f3f1e7985..65846854a 100644 --- a/share/nagelfar/plugin_globalrc.tcl +++ b/share/nagelfar/plugin_globalrc.tcl @@ -27,9 +27,9 @@ proc statementWords {words info} { module - module-whatis - prepend-path - prereq - prereq-all -\ prereq-any - pushenv - remove-path - reportError - reportWarning -\ require-fullname - set-alias - set-function - setenv - source-sh -\ - uncomplete - unset-alias - unset-function - unsetenv - variant -\ - x-resource - add-property - remove-property - module-log - module-trace\ - - module-user - module-verbosity { + umask - uncomplete - unset-alias - unset-function - unsetenv - variant\ + - x-resource - add-property - remove-property - module-log -\ + module-trace - module-user - module-verbosity { set res [list warning "Command \"[lindex $words 0]\" should not be\ be used in global rc file"] } diff --git a/share/nagelfar/syntaxdb_modulefile.tcl b/share/nagelfar/syntaxdb_modulefile.tcl index d180ef589..e50de429a 100644 --- a/share/nagelfar/syntaxdb_modulefile.tcl +++ b/share/nagelfar/syntaxdb_modulefile.tcl @@ -71,6 +71,7 @@ setenv source-sh system uname +umask uncomplete unset-alias unset-function @@ -207,6 +208,7 @@ set {::syntax(uname domain)} 0 set {::syntax(uname release)} 0 set {::syntax(uname version)} 0 set {::syntax(uname machine)} 0 +set ::syntax(umask) 1 set ::syntax(uncomplete) 1 set ::syntax(unset-alias) 1 set ::syntax(unset-function) 1 diff --git a/share/vim/syntax/modulefile.vim b/share/vim/syntax/modulefile.vim index fcea93dde..88b1ef108 100644 --- a/share/vim/syntax/modulefile.vim +++ b/share/vim/syntax/modulefile.vim @@ -23,8 +23,8 @@ syn keyword modCommand module-info module-tag module-version syn keyword modCommand module-virtual module-warn module-whatis modulepath-ignore modulepath-label syn keyword modCommand prepend-path prereq prereq-all prereq-any provide pushenv syn keyword modCommand remove-path reportError reportWarning require-fullname set-alias -syn keyword modCommand set-function setenv source-sh system uname uncomplete unset-alias -syn keyword modCommand unset-function unsetenv variant versioncmp x-resource +syn keyword modCommand set-function setenv source-sh system umask uname uncomplete +syn keyword modCommand unset-alias unset-function unsetenv variant versioncmp x-resource " Modulefile procedures syn keyword modProc ModulesHelp ModulesDisplay ModulesTest diff --git a/tcl/envmngt.tcl.in b/tcl/envmngt.tcl.in index 164a1796a..40d13e808 100644 --- a/tcl/envmngt.tcl.in +++ b/tcl/envmngt.tcl.in @@ -185,8 +185,8 @@ proc renderSettings {} { g_stateAliases] || [array size g_newXResources] || [array size\ g_stateFunctions] || [array size g_stateCompletes] || [array size\ g_delXResources] || [info exists ::g_changeDir] || [info exists\ - ::g_stdoutPuts] || [info exists ::g_prestdoutPuts] || [info exists\ - ::g_return_text]} { + ::g_changeUmask] || [info exists ::g_stdoutPuts] || [info exists\ + ::g_prestdoutPuts] || [info exists ::g_return_text]} { switch -- [getState shelltype] { python { lappend g_shcode_out {import os} @@ -714,6 +714,32 @@ proc renderSettings {} { # cannot change current directory of cmake "shell" } + if {[info exists ::g_changeUmask]} { + switch -- [getState shelltype] { + sh - csh - fish { + lappend g_shcode_out "umask $::g_changeUmask;" + } + perl { + lappend g_shcode_out "umask($::g_changeUmask);" + } + python { + lappend g_shcode_out "os.umask(0o$::g_changeUmask)" + } + ruby { + lappend g_shcode_out "File.umask($::g_changeUmask)" + } + lisp { + # Emacs expresses it as the permissions granted to new files + lappend g_shcode_out "(set-default-file-modes #o[format %04o\ + [expr {0o777 & ~[scan $::g_changeUmask %o]}]])" + } + r { + lappend g_shcode_out "Sys.umask('$::g_changeUmask')" + } + } + # tcl, cmake, cmd and pwsh have no mean to change their own mask + } + # send content deferred during modulefile interpretation if {[info exists ::g_stdoutPuts]} { foreach {newline msg} $::g_stdoutPuts { diff --git a/tcl/init.tcl.in b/tcl/init.tcl.in index fbcd3cedb..62ec9fb7e 100644 --- a/tcl/init.tcl.in +++ b/tcl/init.tcl.in @@ -63,6 +63,7 @@ array set g_state_defs [list\ tcl_version [list [info patchlevel]]\ term_columns { initStateTermColumns}\ timer {0}\ + umask { initStateUmask}\ usergroups { initStateUsergroups}\ username { initStateUsername}\ ] @@ -649,6 +650,12 @@ proc __initStateClockSeconds {} { return [clock seconds] } +# Get file mode creation mask of modulecmd.tcl process, which is inherited +# from the shell session that runs it +proc __initStateUmask {} { + return [format %04o [scan [runCommand sh -c umask] %o]] +} + proc parseEqualListConfValue {option envvar value {level Warning}} { # overridden value coming from environment if {[isEnvVarDefined $envvar]} { diff --git a/tcl/interp.tcl.in b/tcl/interp.tcl.in index a7ee71aed..accaf08d1 100644 --- a/tcl/interp.tcl.in +++ b/tcl/interp.tcl.in @@ -105,7 +105,7 @@ proc initModfileModeAliases {mode auto aliasesVN aliasesPassArgVN\ # the evaluation mode set ::g_modfileEvalModes {load unload display help test whatis refresh\ scan} - ##nagelfar ignore #52 Too long line + ##nagelfar ignore #53 Too long line array set g_modfilePerModeAliases { add-property {add-property nop reportCmd nop nop nop nop nop} always-load {always-load nop reportCmd nop nop nop nop always-load-sc} @@ -152,6 +152,7 @@ setenv {setenv setenv-un setenv setenv sete source-sh {source-sh source-sh-un source-sh-di nop nop nop source-sh source-sh } system {system system reportCmd nop nop nop nop nop } unique-name-conflict {unique-name-conflict nop nop nop nop nop nop nop } +umask {umask umask-un reportCmd nop nop nop nop umask-sc } uncomplete {uncomplete nop reportCmd nop nop nop nop uncomplete-sc} unset-alias {unset-alias nop reportCmd nop nop nop nop unset-alias-sc} unset-function {unset-function nop reportCmd nop nop nop nop unset-function-sc} diff --git a/tcl/main.tcl.in b/tcl/main.tcl.in index bf4a8ff80..0ec1d93e2 100644 --- a/tcl/main.tcl.in +++ b/tcl/main.tcl.in @@ -649,6 +649,7 @@ if {[catch { rename ::__initStateUsergroups ::initStateUsergroups rename ::__initStateUsername ::initStateUsername rename ::__initStateClockSeconds ::initStateClockSeconds + rename ::__initStateUmask ::initStateUmask rename ::__parseDateTimeArg ::parseDateTimeArg } ##nagelfar syntax readFile x x? x? @@ -656,6 +657,7 @@ if {[catch { ##nagelfar syntax initStateUsergroups ##nagelfar syntax initStateUsername ##nagelfar syntax initStateClockSeconds + ##nagelfar syntax initStateUmask ##nagelfar syntax parseDateTimeArg x x # source site configuration script if any diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index a408ae11c..3d144b929 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -871,6 +871,103 @@ proc chdir {dir} { return {} } +# File mode creation mask in effect at this point of the module command run: +# the one set by an already evaluated modulefile if any, elsewhere the one of +# the shell session +proc getCurrentUmask {} { + if {[info exists ::g_changeUmask]} { + return $::g_changeUmask + } else { + return [getState umask] + } +} + +# Stack of masks set by loaded modules. An environment variable emptied when +# unloading a module stays defined until the end of the module command run: +# it is an empty stack +proc getUmaskStack {} { + if {[isEnvVarDefined __MODULES_UMASK]} { + return [split $::env(__MODULES_UMASK) :] + } else { + return {} + } +} + +# Position in umask value stack of the entry set by currently evaluated module +# or -1 if there is none +proc getUmaskStackIndex {stack_list} { + set prefix [currentState modulename]& + set prefix_len [string length $prefix] + set idx 0 + foreach entry $stack_list { + if {[string equal -length $prefix_len $prefix $entry]} { + return $idx + } + incr idx + } + return -1 +} + +proc umask {value} { + # no shell available on this platform is able to change its mask + if {[getState is_win]} { + knerror {umask not supported on Windows platform} + } + if {![regexp {^[0-7]{1,4}$} $value]} { + knerror "Invalid umask value '$value'" + } + # same mask may be written several ways (22, 022, 0022): use a canonical + # form to compare masks on unload + set value [format %04o [scan $value %o]] + + # stack has same structure than pushenv's one: newest entry first, mask + # prior first umask command saved in an entry without module name + set stack_var __MODULES_UMASK + set stack_list [getUmaskStack] + if {[llength $stack_list]} { + # only keep last mask set when command is called several times by module + set idx [getUmaskStackIndex $stack_list] + if {$idx != -1} { + remove-path --index $stack_var $idx + } + } else { + prepend-path $stack_var &[getCurrentUmask] + } + prepend-path $stack_var [currentState modulename]&$value + + set ::g_changeUmask $value + + return {} +} + +# undo umask in unload mode +proc umask-un {value} { + set stack_var __MODULES_UMASK + set stack_list [getUmaskStack] + set idx [getUmaskStackIndex $stack_list] + if {$idx == -1} { + return {} + } + set mod_mask [lindex [split [lindex $stack_list $idx] &] end] + set stack_list [lreplace $stack_list $idx $idx] + remove-path --index $stack_var $idx + + # previous mask is restored only if mask of unloading module is still in + # effect: a mask set since then by another module or by user is kept + if {$idx == 0 && [llength $stack_list] && [getCurrentUmask] eq\ + $mod_mask} { + set ::g_changeUmask [lindex [split [lindex $stack_list 0] &] end] + } + + # clear the stack if it only holds the mask prior first umask command + if {[llength $stack_list] == 1 && [string index [lindex $stack_list 0]\ + 0] eq {&}} { + remove-path --index $stack_var 0 + } + + return {} +} + # supersede exit command to handle it if called within a modulefile # rather than exiting the whole process proc exitModfileCmd {{code 0}} { diff --git a/tcl/modeval.tcl b/tcl/modeval.tcl index 5a95ef3a7..4b8f91f1b 100644 --- a/tcl/modeval.tcl +++ b/tcl/modeval.tcl @@ -604,8 +604,8 @@ proc pushSettings {} { lappend ::g_SAVE_env [array get ::env] # save non-array variable and indication if it was set - foreach var {g_changeDir g_stdoutPuts g_prestdoutPuts g_return_text\ - g_uReqUnFromDepReList g_depReList} { + foreach var {g_changeDir g_changeUmask g_stdoutPuts g_prestdoutPuts\ + g_return_text g_uReqUnFromDepReList g_depReList} { ##nagelfar ignore #2 Badly formed if statement lappend save_var $var {*}[if {[info exists ::$var]} {list 1 [set\ ::$var]} {list 0 {}}] @@ -657,8 +657,8 @@ proc restoreSettings {} { proc flushEnvSettings {} { foreach var {g_Aliases g_stateEnvVars g_stateAliases g_stateFunctions\ g_Functions g_stateCompletes g_Completes g_newXResources\ - g_delXResources g_changeDir g_stdoutPuts g_prestdoutPuts\ - g_return_text} { + g_delXResources g_changeDir g_changeUmask g_stdoutPuts\ + g_prestdoutPuts g_return_text} { ##nagelfar ignore #2 Suspicious variable name if {[info exists ::$var]} { unset ::$var diff --git a/tcl/modscan.tcl b/tcl/modscan.tcl index da1bc975f..2cf87ffa9 100644 --- a/tcl/modscan.tcl +++ b/tcl/modscan.tcl @@ -133,6 +133,10 @@ proc chdir-sc {dir} { recordScanModuleElt $dir chdir } +proc umask-sc {value} { + recordScanModuleElt $value umask +} + proc family-sc {name} { if {![string length $name] || ![regexp {^[A-Za-z0-9_]*$} $name]} { knerror "Invalid family name '$name'" diff --git a/tcl/modspec.tcl b/tcl/modspec.tcl index a614a449c..2741d759d 100644 --- a/tcl/modspec.tcl +++ b/tcl/modspec.tcl @@ -1365,8 +1365,8 @@ proc parseModuleSpecificationProcAdvVersSpec {mlspec nonamespec xtspec\ depends-on depends-on-any envvar family incompat load load-any\ prepend-path prereq prereq-all prereq-any provide provided-alias\ pushenv remove-path require set-alias set-function setenv switch\ - switch-on switch-off tag try-load uncomplete unload unset-alias\ - unset-function unsetenv use variant] + switch-on switch-off tag try-load umask uncomplete unload\ + unset-alias unset-function unsetenv use variant] set xtelt_modspec_list [list always-load conflict depends-on\ depends-on-any incompat load load-any prereq prereq-all prereq-any\ require switch switch-on switch-off try-load unload] diff --git a/testsuite/config/base-config.exp b/testsuite/config/base-config.exp index 7ccc204e0..09f790dcb 100644 --- a/testsuite/config/base-config.exp +++ b/testsuite/config/base-config.exp @@ -381,6 +381,41 @@ proc shell_unalias {test_shell var {re_mode 0}} { return $answer } +proc shell_umask {test_shell val {re_mode 0}} { + if {$re_mode} { + set esc "\\" + } else { + set esc "" + } + + switch -- $test_shell { + {sh} - {bash} - {ksh} - {zsh} - {csh} - {tcsh} - {fish} { + set answer "umask $val;" + } + {perl} { + set answer "umask${esc}($val${esc});" + } + {python} { + set answer "os.umask${esc}(0o$val${esc})" + } + {ruby} { + set answer "File.umask${esc}($val${esc})" + } + {lisp} { + set modes [format %04o [expr {0o777 & ~[scan $val %o]}]] + set answer "${esc}(set-default-file-modes #o$modes${esc})" + } + {r} { + set answer "Sys.umask${esc}('$val'${esc})" + } + default { + set answer "" + } + } + + return $answer +} + proc shell_chdir {test_shell val {re_mode 0}} { if {$re_mode} { set esc "\\" @@ -633,6 +668,13 @@ proc shell_out {test_shell out_list {re_mode 0} {auto_status 1}} { lappend answer_list $ret } } + {umask} { + set ret [shell_umask $test_shell $var $re_mode] + if {$ret ne ""} { + lappend fixed_list $ret + lappend answer_list $ret + } + } {xres} { set ret [shell_xres $test_shell $var $val $re_mode] if {$ret ne ""} { diff --git a/testsuite/example/siteconfig.tcl-1 b/testsuite/example/siteconfig.tcl-1 index c3763986c..fbfd4af80 100644 --- a/testsuite/example/siteconfig.tcl-1 +++ b/testsuite/example/siteconfig.tcl-1 @@ -765,6 +765,16 @@ if {[info exists env(TESTSUITE_ENABLE_SITECONFIG_ID)]} { report [__initStateUsergroups] } +# test Tcl procedure fetching file mode creation mask, which is renamed when +# tcl ext lib is not loaded +if {[info exists env(TESTSUITE_ENABLE_SITECONFIG_UMASK)]} { + if {[info commands __initStateUmask] eq {}} { + report [initStateUmask] + } else { + report [__initStateUmask] + } +} + # fake id command if following environment variable is set if {[info exists env(TESTSUITE_ENABLE_RUNENVCOMMAND_UNSET)]} { runEnvCommand {UNK_VAR val} echo diff --git a/testsuite/install.00-init/010-environ.exp b/testsuite/install.00-init/010-environ.exp index b43bf9559..2bfe083b2 100644 --- a/testsuite/install.00-init/010-environ.exp +++ b/testsuite/install.00-init/010-environ.exp @@ -43,7 +43,8 @@ unsetenv_var MODULES_QUARANTINE_SUPPORT foreach var [array names env] { switch -glob $var { __MODULES_SHARE_* - __MODULES_PUSHENV_* - __MODULES_QUAR_* - \ - MODULES_FAMILY_* - MODULES_RUNENV_* - __MODULES_LM* { + MODULES_FAMILY_* - MODULES_RUNENV_* - __MODULES_LM* - \ + __MODULES_UMASK { unsetenv_var $var } } diff --git a/testsuite/modulefiles.4/extra/1.0 b/testsuite/modulefiles.4/extra/1.0 index c56d5df0d..99ca9a7e6 100644 --- a/testsuite/modulefiles.4/extra/1.0 +++ b/testsuite/modulefiles.4/extra/1.0 @@ -65,6 +65,9 @@ if {[info exists env(TESTSUITE_EXTRA)]} { chdir1 { chdir /xtfoo } + umask1 { + umask 0027 + } err1 { setenv {} value } diff --git a/testsuite/modulefiles.5/umask/1 b/testsuite/modulefiles.5/umask/1 new file mode 100644 index 000000000..16f37cdc3 --- /dev/null +++ b/testsuite/modulefiles.5/umask/1 @@ -0,0 +1,2 @@ +#%Module +umask 77 diff --git a/testsuite/modulefiles.5/umask/2 b/testsuite/modulefiles.5/umask/2 new file mode 100644 index 000000000..eb9c2a377 --- /dev/null +++ b/testsuite/modulefiles.5/umask/2 @@ -0,0 +1,2 @@ +#%Module +umask 0027 diff --git a/testsuite/modulefiles.5/umask/3 b/testsuite/modulefiles.5/umask/3 new file mode 100644 index 000000000..2dfed1ff5 --- /dev/null +++ b/testsuite/modulefiles.5/umask/3 @@ -0,0 +1,3 @@ +#%Module +umask 0007 +umask $env(TESTSUITE_UMASK) diff --git a/testsuite/modules.00-init/005-init_ts.exp b/testsuite/modules.00-init/005-init_ts.exp index d43b16d17..b77f221c3 100644 --- a/testsuite/modules.00-init/005-init_ts.exp +++ b/testsuite/modules.00-init/005-init_ts.exp @@ -814,8 +814,8 @@ proc err_extraspecifier {xtspec} { depends-on depends-on-any envvar family incompat load load-any\ prepend-path prereq prereq-all prereq-any provide provided-alias\ pushenv remove-path require set-alias set-function setenv switch\ - switch-on switch-off tag try-load uncomplete unload unset-alias\ - unset-function unsetenv use variant" + switch-on switch-off tag try-load umask uncomplete unload\ + unset-alias unset-function unsetenv use variant" } proc err_xtspec_notallowed {} { diff --git a/testsuite/modules.00-init/010-environ.exp b/testsuite/modules.00-init/010-environ.exp index ebbb879fc..a51f9dde9 100644 --- a/testsuite/modules.00-init/010-environ.exp +++ b/testsuite/modules.00-init/010-environ.exp @@ -46,7 +46,8 @@ unsetenv_var MODULES_QUARANTINE_SUPPORT foreach var [array names env] { switch -glob $var { __MODULES_SHARE_* - __MODULES_PUSHENV_* - __MODULES_QUAR_* - \ - MODULES_FAMILY_* - MODULES_RUNENV_* - __MODULES_LM* { + MODULES_FAMILY_* - MODULES_RUNENV_* - __MODULES_LM* - \ + __MODULES_UMASK { unsetenv_var $var } } diff --git a/testsuite/modules.01-general/120-siteconfig.exp b/testsuite/modules.01-general/120-siteconfig.exp index 47892b09c..ff2ac7fbf 100644 --- a/testsuite/modules.01-general/120-siteconfig.exp +++ b/testsuite/modules.01-general/120-siteconfig.exp @@ -867,6 +867,13 @@ lappend ans $vers_reportre testouterr_cmd_re sh -V OK [join $ans \n] unsetenv_var TESTSUITE_ENABLE_SITECONFIG_ID +setenv_var TESTSUITE_ENABLE_SITECONFIG_UMASK 1 +set ans [list] +lappend ans [format %04o [scan [exec sh -c umask] %o]] +lappend ans $vers_reportre +testouterr_cmd_re sh -V OK [join $ans \n] +unsetenv_var TESTSUITE_ENABLE_SITECONFIG_UMASK + setenv_var TESTSUITE_ENABLE_RUNENVCOMMAND_UNSET 1 set ans [list] lappend ans {ok} diff --git a/testsuite/modules.50-cmds/760-umask.exp b/testsuite/modules.50-cmds/760-umask.exp new file mode 100644 index 000000000..1cc08b0ba --- /dev/null +++ b/testsuite/modules.50-cmds/760-umask.exp @@ -0,0 +1,253 @@ +############################################################################## +# Modules Revision 3.0 +# Providing a flexible user environment +# +# File: modules.50-cmds/%M% +# Revision: %I% +# First Edition: 2026/11/19 +# Last Mod.: %U%, %G% +# +# Authors: Xavier Delaruelle, xavier.delaruelle@cea.fr +# +# Description: Testuite testsequence +# Command: load, unload, switch, purge, display, help, test, +# whatis, refresh +# Modulefiles: umask +# Sub-Command: +# +# Comment: %C{ +# Test umask modulefile command +# }C% +# +############################################################################## + +set mp $modpath.5 +set mpre [regsub -all "\(\[.+?\]\)" $mp {\\\1}] +setenv_path_var MODULEPATH $mp + +# ensure auto symbolic versions are not set for these tests +setenv_var MODULES_ADVANCED_VERSION_SPEC 0 + +set mod1 umask/1 +set mod2 umask/2 +set mod3 umask/3 +set mod1p $mp/$mod1 +set mod2p $mp/$mod2 +set mod3p $mp/$mod3 + +# mask of the testsuite process is the one seen by module command as the mask +# of the shell session +set cur_mask [format %04o [scan [exec sh -c umask] %o]] +# a mask that is not the current one, to simulate a mask changed by user after +# module load +set other_mask [lindex {0070 0007} [string equal $cur_mask 0070]] + +setenv_loaded_module {} {} +unsetenv_var __MODULES_UMASK + + +# +# load +# + +set ans [list] +lappend ans [list set _LMFILES_ $mod1p] +lappend ans [list set LOADEDMODULES $mod1] +lappend ans [list set __MODULES_UMASK "$mod1&0077:&$cur_mask"] +lappend ans [list umask 0077] +testouterr_cmd_re ALL "load $mod1" $ans {} + +skip_if_quick_mode + +# last loaded module wins +set ans [list] +lappend ans [list set _LMFILES_ $mod1p:$mod2p] +lappend ans [list set LOADEDMODULES $mod1:$mod2] +lappend ans [list set __MODULES_UMASK "$mod2&0027:$mod1&0077:&$cur_mask"] +lappend ans [list umask 0027] +testouterr_cmd sh "load $mod1 $mod2" $ans {} + +# stack already defined: mask saved prior first umask command is kept +setenv_loaded_module [list $mod1] [list $mod1p] +setenv_var __MODULES_UMASK "$mod1&0077:&0123" +set ans [list] +lappend ans [list set _LMFILES_ $mod1p:$mod2p] +lappend ans [list set LOADEDMODULES $mod1:$mod2] +lappend ans [list set __MODULES_UMASK "$mod2&0027:$mod1&0077:&0123"] +lappend ans [list umask 0027] +testouterr_cmd sh "load $mod2" $ans {} + +# command called several times in modulefile: last mask is retained +setenv_loaded_module {} {} +unsetenv_var __MODULES_UMASK +setenv_var TESTSUITE_UMASK 2 +set ans [list] +lappend ans [list set _LMFILES_ $mod3p] +lappend ans [list set LOADEDMODULES $mod3] +lappend ans [list set __MODULES_UMASK "$mod3&0002:&$cur_mask"] +lappend ans [list umask 0002] +testouterr_cmd sh "load $mod3" $ans {} + +setenv_loaded_module [list $mod1] [list $mod1p] +setenv_var __MODULES_UMASK "$mod1&0077:&0123" +set ans [list] +lappend ans [list set _LMFILES_ $mod1p:$mod3p] +lappend ans [list set LOADEDMODULES $mod1:$mod3] +lappend ans [list set __MODULES_UMASK "$mod3&0002:$mod1&0077:&0123"] +lappend ans [list umask 0002] +testouterr_cmd sh "load $mod3" $ans {} + + +# +# erroneous values +# + +setenv_loaded_module {} {} +unsetenv_var __MODULES_UMASK + +foreach bad_mask [list 088 00077 u=rwx -1 0x1f {}] { + setenv_var TESTSUITE_UMASK $bad_mask + set tserr [msg_load $mod3 [msg_moderr "Invalid umask value '$bad_mask'"\ + {umask $env(TESTSUITE_UMASK)} $mod3p 3]] + # mask set by modulefile prior error is not applied + testouterr_cmd sh "load $mod3" ERR $tserr +} + +# mask set by other modulefile is applied +set ans [list] +lappend ans [list set LOADEDMODULES $mod1] +lappend ans [list set _LMFILES_ $mod1p] +lappend ans [list set __MODULES_UMASK "$mod1&0077:&$cur_mask"] +lappend ans [list umask 0077] +lappend ans [list ERR] +testouterr_cmd sh "load $mod1 $mod3" $ans $tserr + +setenv_var TESTSUITE_UMASK 2 + +# fake Windows platform if expected siteconfig file is installed +if {[siteconfig_isStderrTty]} { + setenv_var TESTSUITE_ENABLE_SITECONFIG_WINDOWS_PLATFORM 1 + set tserr [msg_load $mod1 [msg_moderr {umask not supported on Windows\ + platform} {umask 77} $mod1p 2]] + testouterr_cmd sh "load $mod1" ERR $tserr + unsetenv_var TESTSUITE_ENABLE_SITECONFIG_WINDOWS_PLATFORM +} + + +# +# unload +# + +# mask set by module still in effect: previous mask is restored +setenv_loaded_module [list $mod1] [list $mod1p] +setenv_var __MODULES_UMASK "$mod1&$cur_mask:&0123" +set ans [list] +lappend ans [list unset _LMFILES_] +lappend ans [list unset LOADEDMODULES] +lappend ans [list unset __MODULES_UMASK] +lappend ans [list umask 0123] +testouterr_cmd_re ALL "unload $mod1" $ans {} + +# mask changed since module load: it is kept +setenv_var __MODULES_UMASK "$mod1&$other_mask:&0123" +set ans [list] +lappend ans [list unset _LMFILES_] +lappend ans [list unset LOADEDMODULES] +lappend ans [list unset __MODULES_UMASK] +testouterr_cmd sh "unload $mod1" $ans {} + +# no stack defined or no entry for module in stack +unsetenv_var __MODULES_UMASK +set ans [list] +lappend ans [list unset _LMFILES_] +lappend ans [list unset LOADEDMODULES] +testouterr_cmd sh "unload $mod1" $ans {} + +setenv_var __MODULES_UMASK "foo/1&$cur_mask:&0123" +testouterr_cmd sh "unload $mod1" $ans {} + +# unload of module that has not set the mask in effect +setenv_loaded_module [list $mod1 $mod2] [list $mod1p $mod2p] +setenv_var __MODULES_UMASK "$mod2&$cur_mask:$mod1&0077:&0123" +set ans [list] +lappend ans [list set _LMFILES_ $mod2p] +lappend ans [list set LOADEDMODULES $mod2] +lappend ans [list set __MODULES_UMASK "$mod2&$cur_mask:&0123"] +testouterr_cmd sh "unload $mod1" $ans {} + +# mask of previously loaded module is restored +set ans [list] +lappend ans [list set _LMFILES_ $mod1p] +lappend ans [list set LOADEDMODULES $mod1] +lappend ans [list set __MODULES_UMASK "$mod1&0077:&0123"] +lappend ans [list umask 0077] +testouterr_cmd sh "unload $mod2" $ans {} + +# mask restored by an unload is the one in effect for the following ones +setenv_var __MODULES_UMASK "$mod2&$cur_mask:$mod1&0077:&0123" +set ans [list] +lappend ans [list unset _LMFILES_] +lappend ans [list unset LOADEDMODULES] +lappend ans [list unset __MODULES_UMASK] +lappend ans [list umask 0123] +testouterr_cmd sh purge $ans {} + +# mask changed by user is kept whatever the number of module unloaded +setenv_var __MODULES_UMASK "$mod2&$other_mask:$mod1&0077:&0123" +set ans [list] +lappend ans [list unset _LMFILES_] +lappend ans [list unset LOADEDMODULES] +lappend ans [list unset __MODULES_UMASK] +testouterr_cmd sh purge $ans {} + +# switch: saved mask goes from unloaded to loaded module +setenv_loaded_module [list $mod1] [list $mod1p] +setenv_var __MODULES_UMASK "$mod1&$cur_mask:&0123" +set ans [list] +lappend ans [list set _LMFILES_ $mod2p] +lappend ans [list set LOADEDMODULES $mod2] +lappend ans [list set __MODULES_UMASK "$mod2&0027:&0123"] +lappend ans [list umask 0027] +testouterr_cmd sh "switch $mod1 $mod2" $ans {} + + +# +# other evaluation modes +# + +setenv_loaded_module {} {} +unsetenv_var __MODULES_UMASK + +set tserr "$modlin +$mpre/$mod1: + +umask\t\t77 +$modlin" +testouterr_cmd_re sh "display $mod1" OK $tserr + +set tserr "$modlin +Module Specific Help for $mpre/$mod1: + +$warn_msgs: Unable to find ModulesHelp in $mpre/$mod1. +$modlin" +testouterr_cmd_re sh "help $mod1" OK $tserr + +set tserr "$modlin +Module Specific Test for $mpre/$mod1: + +$warn_msgs: Unable to find ModulesTest in $mpre/$mod1. +$modlin" +testouterr_cmd_re sh "test $mod1" OK $tserr + +testouterr_cmd sh "whatis $mod1" OK {} + +setenv_loaded_module [list $mod1] [list $mod1p] +setenv_var __MODULES_UMASK "$mod1&$other_mask:&0123" +testouterr_cmd sh refresh OK {} + + +# +# Cleanup +# + +reset_test_env diff --git a/testsuite/modules.51-scan/041-extra_spec-env.exp b/testsuite/modules.51-scan/041-extra_spec-env.exp index 91c73932b..1e287856a 100644 --- a/testsuite/modules.51-scan/041-extra_spec-env.exp +++ b/testsuite/modules.51-scan/041-extra_spec-env.exp @@ -172,6 +172,16 @@ set tserr extra/1.0 testouterr_cmd sh {avail -t extra chdir:/xtfoo} OK $tserr +# +# umask +# + +setenv_var TESTSUITE_EXTRA umask1 +set tserr extra/1.0 +testouterr_cmd sh {avail -t extra umask:0027} OK $tserr +testouterr_cmd sh {avail -t extra umask:0077} OK {} + + # # erroneous commands in modulefile # diff --git a/testsuite/modules.70-maint/220-config.exp b/testsuite/modules.70-maint/220-config.exp index 64c1ed1c2..fb05318d6 100644 --- a/testsuite/modules.70-maint/220-config.exp +++ b/testsuite/modules.70-maint/220-config.exp @@ -319,7 +319,7 @@ array set configkind [list\ variant_shortcut l\ ] -set statelist [list always_read_full_file autoinit clock_seconds cmdline commandname domainname error_count extra_siteconfig_loaded false_rendered force hiding_threshold inhibit_errreport inhibit_interp init_error_report is_stderr_tty is_win kernelversion logfd logging lsb_codename lsb_id lsb_release machine nodename os osversion paginate path_separator report_format reportfd return_false shell shelltype siteconfig_loaded sub1_separator sub2_separator subcmd subcmd_args supported_shells tcl_ext_lib_loaded tcl_version term_columns usergroups username] +set statelist [list always_read_full_file autoinit clock_seconds cmdline commandname domainname error_count extra_siteconfig_loaded false_rendered force hiding_threshold inhibit_errreport inhibit_interp init_error_report is_stderr_tty is_win kernelversion logfd logging lsb_codename lsb_id lsb_release machine nodename os osversion paginate path_separator report_format reportfd return_false shell shelltype siteconfig_loaded sub1_separator sub2_separator subcmd subcmd_args supported_shells tcl_ext_lib_loaded tcl_version term_columns umask usergroups username] if {[is_quick_mode]} { array unset configdfl diff --git a/testsuite/modules.70-maint/390-state.exp b/testsuite/modules.70-maint/390-state.exp index 07076dc38..e51d4c8df 100644 --- a/testsuite/modules.70-maint/390-state.exp +++ b/testsuite/modules.70-maint/390-state.exp @@ -23,7 +23,7 @@ set state_header "- State name $modlin.- Value $modlin" set envvar_header "- Env. variable $modlin.- Value $modlin" -set statelist [list always_read_full_file autoinit clock_seconds cmdline commandname domainname error_count extra_siteconfig_loaded false_rendered force hiding_threshold inhibit_errreport inhibit_interp init_error_report is_stderr_tty is_win kernelversion logfd logging lsb_codename lsb_id lsb_release machine modulefile nodename os osversion paginate path_separator report_format reportfd return_false shell shelltype siteconfig_loaded sub1_separator sub2_separator subcmd subcmd_args supported_shells tcl_ext_lib_loaded tcl_version term_columns usergroups username] +set statelist [list always_read_full_file autoinit clock_seconds cmdline commandname domainname error_count extra_siteconfig_loaded false_rendered force hiding_threshold inhibit_errreport inhibit_interp init_error_report is_stderr_tty is_win kernelversion logfd logging lsb_codename lsb_id lsb_release machine modulefile nodename os osversion paginate path_separator report_format reportfd return_false shell shelltype siteconfig_loaded sub1_separator sub2_separator subcmd subcmd_args supported_shells tcl_ext_lib_loaded tcl_version term_columns umask usergroups username] # # global state test From 1312215af1e4ec5d62e269691b60bc0519cf2fdf Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 19 Nov 2026 08:52:14 -0600 Subject: [PATCH 3/4] install: rebuild Tcl extension library when its sources change The rules of the top-level Makefile building the Tcl extension library and the utility libraries of the testsuite had no prerequisite. Once built, these libraries were never rebuilt by "make" or "make test" after a change in their source file, so tests ran against a stale library. Declare the source files as prerequisites of these targets. Also make the object file depend on the header files it includes. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- Makefile | 18 +++++++++--------- lib/Makefile.in | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index b332eaefb..31a1bc0d5 100644 --- a/Makefile +++ b/Makefile @@ -669,31 +669,31 @@ script/modulecmd: script/modulecmd.in chmod +x $@ # Tcl extension library-related rules -lib/libtclenvmodules$(SHLIB_SUFFIX): +lib/libtclenvmodules$(SHLIB_SUFFIX): lib/envmodules.c lib/envmodules.h $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-closedir$(SHLIB_SUFFIX): +lib/libtestutil-closedir$(SHLIB_SUFFIX): lib/testutil-closedir.c $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-getpwuid$(SHLIB_SUFFIX): +lib/libtestutil-getpwuid$(SHLIB_SUFFIX): lib/testutil-getpwuid.c $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-getgroups$(SHLIB_SUFFIX): +lib/libtestutil-getgroups$(SHLIB_SUFFIX): lib/testutil-getgroups.c $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-0getgroups$(SHLIB_SUFFIX): +lib/libtestutil-0getgroups$(SHLIB_SUFFIX): lib/testutil-0getgroups.c $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-dupgetgroups$(SHLIB_SUFFIX): +lib/libtestutil-dupgetgroups$(SHLIB_SUFFIX): lib/testutil-dupgetgroups.c $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-time$(SHLIB_SUFFIX): +lib/libtestutil-time$(SHLIB_SUFFIX): lib/testutil-time.c $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-mktime$(SHLIB_SUFFIX): +lib/libtestutil-mktime$(SHLIB_SUFFIX): lib/testutil-mktime.c $(MAKE) --no-print-directory -C lib $(@F) -lib/libtestutil-tcl_loadfile$(SHLIB_SUFFIX): +lib/libtestutil-tcl_loadfile$(SHLIB_SUFFIX): lib/testutil-tcl_loadfile.c $(MAKE) --no-print-directory -C lib $(@F) # example configs for test rules diff --git a/lib/Makefile.in b/lib/Makefile.in index 4393d61d8..d6e7439fe 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -24,7 +24,7 @@ endif all: libtclenvmodules@TCL_SHLIB_SUFFIX@ -envmodules.o: envmodules.c +envmodules.o: envmodules.c envmodules.h config.h $(ECHO_CC) $(CC) $(CFLAGS) $(CFLAGS_COVERAGE) -c $< -o $@ From 8d2580bff10b7a2935bac5c5a762674b789100c0 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Thu, 19 Nov 2026 08:59:12 -0600 Subject: [PATCH 4/4] Track umask change made by script in source-sh and sh-to-mod A shell script evaluated with the source-sh modulefile command or the sh-to-mod sub-command may change the file mode creation mask. This change was lost as only environment variables, aliases, functions, completions and current working directory were compared prior and after script evaluation. Query the mask with the umask shell command before and after the script is evaluated and produce a umask modulefile command if it has changed. Masks are normalized prior comparison as shells do not report them with the same number of digits. The umask element is accepted by the --ignore option of source-sh to skip this change. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: Xavier Delaruelle --- NEWS.rst | 4 + .../source-shell-script-in-modulefile.rst | 3 +- doc/source/design/umask.rst | 16 ++- doc/source/module.rst | 6 +- doc/source/modulefile.rst | 9 +- tcl/mfcmd.tcl | 98 +++++++++++-------- testsuite/example/umask-sh-to-mod | 2 + testsuite/modulefiles.5/umask/4 | 2 + testsuite/modules.50-cmds/760-umask.exp | 69 +++++++++++++ 9 files changed, 164 insertions(+), 45 deletions(-) create mode 100644 testsuite/example/umask-sh-to-mod create mode 100644 testsuite/modulefiles.5/umask/4 diff --git a/NEWS.rst b/NEWS.rst index b3777941d..a22456e56 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -91,6 +91,10 @@ Modules 5.8.0 (not yet released) file mode creation mask. * Lib: add the ``initStateUmask`` procedure to the Modules Tcl extension library to fetch the file mode creation mask of the current process. +* Track the change of file mode creation mask made by a shell script + evaluated with :mfcmd:`source-sh` modulefile command or :subcmd:`sh-to-mod` + sub-command. ``umask`` element is added to the ``--ignore`` option of + :mfcmd:`source-sh`. .. _5.7 release notes: diff --git a/doc/source/design/source-shell-script-in-modulefile.rst b/doc/source/design/source-shell-script-in-modulefile.rst index a503f63a0..dcea17d68 100644 --- a/doc/source/design/source-shell-script-in-modulefile.rst +++ b/doc/source/design/source-shell-script-in-modulefile.rst @@ -174,7 +174,7 @@ Specification - but it is an external command, so requires extra processing and an additional requirement - moreover it does not return shell functions in general, only exported Bash functions - - Shell builtin commands are used to query existing environment variables, aliases, functions, completions and current working directory + - Shell builtin commands are used to query existing environment variables, aliases, functions, completions, current working directory and file mode creation mask - which provides best processing efficiency - but leads to specific output parsing for each shell @@ -235,6 +235,7 @@ Specification - ``function`` - ``chdir`` - ``complete`` + - ``umask`` (added in version 5.8) - Shell elements changed by sourced script and listed on ``--ignore`` option are ignored (no modulefile command relative to these changes are produced) diff --git a/doc/source/design/umask.rst b/doc/source/design/umask.rst index 46bd92daa..b2556432c 100644 --- a/doc/source/design/umask.rst +++ b/doc/source/design/umask.rst @@ -135,13 +135,25 @@ done for the other modulefile commands a shell cannot honor. On Windows platform, an error is raised when ``umask`` is evaluated in ``load`` mode: there is no file mode creation mask to read or to set there. +Mask changed by shell script +---------------------------- + +:mfcmd:`source-sh` modulefile command and :subcmd:`sh-to-mod` sub-command +track the mask change made by the shell script they evaluate. The ``umask`` +shell command is run prior and after script evaluation. If the two masks +differ, a ``umask`` modulefile command setting the resulting mask is produced. + +Shells do not report the mask with the same number of digits, so both masks +are normalized before being compared. + +``umask`` element can be set on the ``--ignore`` option of ``source-sh`` to +ignore this change. + Limitations ----------- * A session that gets the loaded environment of another session without inheriting its mask (e.g., environment variables forwarded to a new login session) sees the module loaded whereas its mask is not applied. -* Mask changes made by a shell script are not tracked by :mfcmd:`source-sh` - and :subcmd:`sh-to-mod`. .. vim:set tabstop=2 shiftwidth=2 expandtab autoindent: diff --git a/doc/source/module.rst b/doc/source/module.rst index c1d20f045..0dc50f13d 100644 --- a/doc/source/module.rst +++ b/doc/source/module.rst @@ -2797,7 +2797,8 @@ Module Sub-Commands equivalent to the evaluation of shell script. Changes on environment variables, shell aliases, shell functions, shell - completions and current working directory are tracked. + completions, current working directory and file mode creation mask are + tracked. Changes made on environment variable intended for Modules private use (e.g., :envvar:`LOADEDMODULES`, :envvar:`_LMFILES_`, ``__MODULES_*``) are ignored. @@ -2834,6 +2835,9 @@ Module Sub-Commands .. versionchanged:: 5.8 *Shell* is given an empty standard input + .. versionchanged:: 5.8 + Support for tracking file mode creation mask changes added + .. subcmd:: show modulefile... See :subcmd:`display`. diff --git a/doc/source/modulefile.rst b/doc/source/modulefile.rst index 96f5bdc52..6c247c724 100644 --- a/doc/source/modulefile.rst +++ b/doc/source/modulefile.rst @@ -1782,7 +1782,8 @@ the *modulefile* is being loaded. ``load`` time. Changes on environment variables, shell aliases, shell functions, shell - completions and current working directory are tracked. + completions, current working directory and file mode creation mask are + tracked. Changes made on environment variable intended for Modules private use (e.g., :envvar:`LOADEDMODULES`, :envvar:`_LMFILES_`, ``__MODULES_*``) are ignored. @@ -1798,7 +1799,8 @@ the *modulefile* is being loaded. The ``--ignore`` option accepts a list of shell elements whose changes made by *script* should be ignored. *eltlist* corresponds to the concatenation of multiple shell element separated by colon character. Accepted shell elements - are: ``envvar``, ``alias``, ``function``, ``chdir`` and ``complete``. + are: ``envvar``, ``alias``, ``function``, ``chdir``, ``complete`` and + ``umask``. .. note:: *shell* is run in non-interactive mode without loading any shell configuration or profile. As a result, script does not have access to @@ -1827,6 +1829,9 @@ the *modulefile* is being loaded. .. versionchanged:: 5.8 *Shell* is given an empty standard input + .. versionchanged:: 5.8 + Support for tracking file mode creation mask changes added + .. mfcmd:: system string Run *string* command through shell. On Unix, command is passed to the diff --git a/tcl/mfcmd.tcl b/tcl/mfcmd.tcl index 3d144b929..26f752cc7 100644 --- a/tcl/mfcmd.tcl +++ b/tcl/mfcmd.tcl @@ -1553,9 +1553,10 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { # it does not raise error if arguments are set ##nagelfar ignore +3 Found constant set command "export -p; echo $sep; declare -f 2>/dev/null; echo\ - $sep; alias; echo $sep; echo $sep; pwd; echo $sep; . [listTo\ - shell $shdesc] 2>&1; echo $sep; export -p; echo $sep; declare -f\ - 2>/dev/null; echo $sep; alias; echo $sep; echo $sep; pwd" + $sep; alias; echo $sep; echo $sep; pwd; echo $sep; umask; echo\ + $sep; . [listTo shell $shdesc] 2>&1; echo $sep; export -p; echo\ + $sep; declare -f 2>/dev/null; echo $sep; alias; echo $sep; echo\ + $sep; pwd; echo $sep; umask" set varre {export (\S+?)=["']?(.*?)["']?$} set funcre {(\S+?) \(\)\s?\n?{\s?\n(.+?)\n}$} set aliasre {([^='\s]+?)='?(.*?)'$} @@ -1563,11 +1564,12 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { set alvalmap [list {'\''} ' {'"'"'} '] } bash { - ##nagelfar ignore +2 Found constant + ##nagelfar ignore +3 Found constant set command "export -p; echo $sep; declare -f; echo $sep; alias;\ - echo $sep; complete; echo $sep; pwd; echo $sep; . [listTo shell\ - $shdesc] 2>&1; echo $sep; export -p; echo $sep; declare -f; echo\ - $sep; alias; echo $sep; complete; echo $sep; pwd" + echo $sep; complete; echo $sep; pwd; echo $sep; umask; echo $sep;\ + . [listTo shell $shdesc] 2>&1; echo $sep; export -p; echo $sep;\ + declare -f; echo $sep; alias; echo $sep; complete; echo $sep;\ + pwd; echo $sep; umask" set varre {declare -x (\S+?)="(.*?)"$} set funcre {(\S+?) \(\)\s?\n{\s?\n(.+?)\n}$} set aliasre {alias (\S+?)='(.*?)'$} @@ -1580,9 +1582,10 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { bash-eval { ##nagelfar ignore +3 Found constant set command "export -p; echo $sep; declare -f; echo $sep; alias;\ - echo $sep; complete; echo $sep; pwd; echo $sep; eval \"\$([listTo\ - shell $shdesc] 2>/dev/null)\"; echo $sep; export -p; echo $sep;\ - declare -f; echo $sep; alias; echo $sep; complete; echo $sep; pwd" + echo $sep; complete; echo $sep; pwd; echo $sep; umask; echo $sep;\ + eval \"\$([listTo shell $shdesc] 2>/dev/null)\"; echo $sep;\ + export -p; echo $sep; declare -f; echo $sep; alias; echo $sep;\ + complete; echo $sep; pwd; echo $sep; umask" set varre {declare -x (\S+?)="(.*?)"$} set funcre {(\S+?) \(\)\s?\n{\s?\n(.+?)\n}$} set aliasre {alias (\S+?)='(.*?)'$} @@ -1596,10 +1599,10 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { ##nagelfar ignore +3 Found constant set command "typeset -x; echo $sep; typeset +f | while read f; do\ typeset -f \${f%\\(\\)}; echo; done; echo $sep; alias; echo $sep;\ - echo $sep; pwd; echo $sep; . [listTo shell $shdesc] 2>&1; echo\ - $sep; typeset -x; echo $sep; typeset +f | while read f; do\ - typeset -f \${f%\\(\\)}; echo; done; echo $sep; alias; echo $sep;\ - echo $sep; pwd" + echo $sep; pwd; echo $sep; umask; echo $sep; . [listTo shell\ + $shdesc] 2>&1; echo $sep; typeset -x; echo $sep; typeset +f |\ + while read f; do typeset -f \${f%\\(\\)}; echo; done; echo $sep;\ + alias; echo $sep; echo $sep; pwd; echo $sep; umask" set varre {(\S+?)=\$?'?(.*?)'?$} set funcre {(\S+?)\(\) {\n?(.+?)}[;\n]?$} set aliasre {(\S+?)=\$?'?(.*?)'?$} @@ -1607,11 +1610,11 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { set alvalmap [list {\"} \" {\\'} ' {\'} ' {\\\\} {\\}] } zsh { - ##nagelfar ignore +2 Found constant + ##nagelfar ignore +3 Found constant set command "typeset -x; echo $sep; declare -f; echo $sep; alias;\ - echo $sep; echo $sep; pwd; echo $sep; . [listTo shell $shdesc]\ - 2>&1; echo $sep; typeset -x; echo $sep; declare -f; echo $sep;\ - alias; echo $sep; echo $sep; pwd" + echo $sep; echo $sep; pwd; echo $sep; umask; echo $sep; . [listTo\ + shell $shdesc] 2>&1; echo $sep; typeset -x; echo $sep; declare\ + -f; echo $sep; alias; echo $sep; echo $sep; pwd; echo $sep; umask" set varre {(\S+?)=\$?'?(.*?)'?$} set funcre {(\S+?) \(\) {\n(.+?)\n}$} set aliasre {(\S+?)=\$?'?(.*?)'?$} @@ -1621,9 +1624,9 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { csh { ##nagelfar ignore +2 Found constant set command "setenv; echo $sep; echo $sep; alias; echo $sep; echo\ - $sep; pwd; echo $sep; source [listTo shell $shdesc] >&\ - /dev/stdout; echo $sep; setenv; echo $sep; echo $sep; alias; echo\ - $sep; echo $sep; pwd" + $sep; pwd; echo $sep; umask; echo $sep; source [listTo shell\ + $shdesc] >& /dev/stdout; echo $sep; setenv; echo $sep; echo $sep;\ + alias; echo $sep; echo $sep; pwd; echo $sep; umask" set varre {(\S+?)=(.*?)$} set aliasre {(\S+?)\t(.*?)$} set varvalmap [list] @@ -1631,11 +1634,12 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { lappend shellopts -f } tcsh { - ##nagelfar ignore +2 Found constant + ##nagelfar ignore +3 Found constant set command "setenv; echo $sep; echo $sep; alias; echo $sep;\ - complete; echo $sep; pwd; echo $sep; source [listTo shell\ - $shdesc] >& /dev/stdout; echo $sep; setenv; echo $sep; echo $sep;\ - alias; echo $sep; complete; echo $sep; pwd" + complete; echo $sep; pwd; echo $sep; umask; echo $sep; source\ + [listTo shell $shdesc] >& /dev/stdout; echo $sep; setenv; echo\ + $sep; echo $sep; alias; echo $sep; complete; echo $sep; pwd; echo\ + $sep; umask" set varre {(\S+?)=(.*?)$} set aliasre {(\S+?)\t\(?(.*?)\)?$} set compre {(\S+?)\t(.*?)$} @@ -1653,15 +1657,16 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { ##nagelfar ignore +9 Found constant set command "set -xgL; echo '$sep'; status test-feature\ regex-easyesc 2>/dev/null; and set escrepl '\\\\\\\\\$1'; or set\ - escrepl '\\\\\\\\\\\\\\\$1'; set funcfilter \\^\\((string\ - join '|' (string replace -r '(\\\[|\\.)' \$escrepl\ - (builtin -n; functions -a -n | string split ', ' | string match\ - -e -r '^_')))\\|fish\\.\\*\\)\\\$; $getfunc; $getfunc; string\ - split \$funcout; echo '$sep'; string split \$funcout; echo\ - '$sep'; complete; echo '$sep'; pwd; echo '$sep'; source [listTo\ - shell $shdesc] 2>&1; or exit \$status; echo '$sep'; set -xgL;\ - echo '$sep'; $getfunc; string split \$funcout; echo '$sep';\ - string split \$funcout; echo '$sep'; complete; echo '$sep'; pwd" + escrepl '\\\\\\\\\\\\\\\$1'; set funcfilter \\^\\((string join\ + '|' (string replace -r '(\\\[|\\.)' \$escrepl (builtin -n;\ + functions -a -n | string split ', ' | string match -e -r\ + '^_')))\\|fish\\.\\*\\)\\\$; $getfunc; $getfunc; string split\ + \$funcout; echo '$sep'; string split \$funcout; echo '$sep';\ + complete; echo '$sep'; pwd; echo '$sep'; umask; echo '$sep';\ + source [listTo shell $shdesc] 2>&1; or exit \$status; echo\ + '$sep'; set -xgL; echo '$sep'; $getfunc; string split \$funcout;\ + echo '$sep'; string split \$funcout; echo '$sep'; complete; echo\ + '$sep'; pwd; echo '$sep'; umask" set varre {^(\S+?\M) ?['"]?(.*?)['"]?$} # exclude alias from function list set funcre "^function (\\S+?)(?: \[^\\n\]*?--description\ @@ -1705,15 +1710,17 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { # link result variables to calling context upvar cwdbefout cwdbefout cwdaftout cwdaftout + upvar umaskbefout umaskbefout umaskaftout umaskaftout # extract each output sections set idx 0 foreach varout {varbefout funcbefout aliasbefout compbefout cwdbefout\ - scriptout varaftout funcaftout aliasaftout compaftout cwdaftout} { + umaskbefout scriptout varaftout funcaftout aliasaftout compaftout\ + cwdaftout umaskaftout} { ##nagelfar vartype varout varName if {[set sepidx [string first $sep $output $idx]] == -1} { set $varout [string trimright [string range $output $idx end] \n] - if {$varout ne {cwdaftout} && !$sherr} { + if {$varout ne {umaskaftout} && !$sherr} { knerror "Unexpected output when sourcing '$shdesc' in shell\ '$shell'" } @@ -1753,6 +1760,11 @@ proc execShAndGetEnv {elt_ignored_list shell script args} { set cwdaftout $cwdbefout } + # clear file mode creation mask change if ignored + if {{umask} in $elt_ignored_list} { + set umaskaftout $umaskbefout + } + # extract environment variable information if {{envvar} ni $elt_ignored_list} { ##nagelfar ignore Found constant @@ -1818,8 +1830,8 @@ proc sh-to-mod {elt_ignored_list args} { # evaluate script and retrieve environment before and after evaluation # procedure will set result variables in current context ##nagelfar implicitvarcmd {execShAndGetEnv *} ignvarlist cwdbefout\ - cwdaftout varbef varaft funcbef funcaft aliasbef aliasaft compbef\ - compaft + cwdaftout umaskbefout umaskaftout varbef varaft funcbef funcaft\ + aliasbef aliasaft compbef compaft execShAndGetEnv $elt_ignored_list {*}$args # check environment variable change @@ -1953,6 +1965,13 @@ proc sh-to-mod {elt_ignored_list args} { if {$cwdbefout ne $cwdaftout} { lappend modcontent [list chdir $cwdaftout] } + # check file mode creation mask change. shells do not report mask with the + # same number of digits + set umask_bef [format %04o [scan $umaskbefout %o]] + set umask_aft [format %04o [scan $umaskaftout %o]] + if {$umask_bef ne $umask_aft} { + lappend modcontent [list umask $umask_aft] + } # sort result to ensure consistent output whatever the evaluation shell set modcontent [lsort -dictionary $modcontent] @@ -2002,7 +2021,8 @@ proc parseSourceShArgs {args} { if {[info exists ignore_opt_raw]} { set elt_ignored_list [split $ignore_opt_raw :] - set allowed_elt_ignored_list {envvar function alias chdir complete} + set allowed_elt_ignored_list {envvar function alias chdir complete\ + umask} foreach elt_ignored $elt_ignored_list { if {$elt_ignored ni $allowed_elt_ignored_list} { knerror "Invalid ignored element '$elt_ignored'" diff --git a/testsuite/example/umask-sh-to-mod b/testsuite/example/umask-sh-to-mod new file mode 100644 index 000000000..fab621eb5 --- /dev/null +++ b/testsuite/example/umask-sh-to-mod @@ -0,0 +1,2 @@ +# set file mode creation mask, with a syntax valid for sh, csh and fish shells +umask $TESTSUITE_UMASK_VALUE diff --git a/testsuite/modulefiles.5/umask/4 b/testsuite/modulefiles.5/umask/4 new file mode 100644 index 000000000..3173ad5c1 --- /dev/null +++ b/testsuite/modulefiles.5/umask/4 @@ -0,0 +1,2 @@ +#%Module +source-sh {*}$env(TESTSUITE_UMASK_SOURCE_SH) testsuite/example/umask-sh-to-mod diff --git a/testsuite/modules.50-cmds/760-umask.exp b/testsuite/modules.50-cmds/760-umask.exp index 1cc08b0ba..f16f31415 100644 --- a/testsuite/modules.50-cmds/760-umask.exp +++ b/testsuite/modules.50-cmds/760-umask.exp @@ -246,6 +246,75 @@ setenv_var __MODULES_UMASK "$mod1&$other_mask:&0123" testouterr_cmd sh refresh OK {} +# +# mask changed by shell script +# + +setenv_loaded_module {} {} +unsetenv_var __MODULES_UMASK + +set mod4 umask/4 +set mod4p $mp/$mod4 +set script testsuite/example/umask-sh-to-mod + +# script sets a mask different than current one +setenv_var TESTSUITE_UMASK_VALUE $other_mask +foreach sh [list bash ksh zsh sh dash ksh93 fish csh tcsh] { + # mksh flavor of ksh and sh from FreeBSD are not supported + if {[auto_execok $sh] eq {} || ($sh eq {ksh} && $ksh_is_mksh) || ($sh eq\ + {sh} && $os_name eq {freebsd})} { + continue + } + set tserr "#%Module\numask\t\t$other_mask" + testouterr_cmd sh "sh-to-mod $sh $script" OK $tserr +} + +# script sets the mask already in effect +setenv_var TESTSUITE_UMASK_VALUE $cur_mask +testouterr_cmd sh "sh-to-mod bash $script" OK {} + +setenv_var TESTSUITE_UMASK_VALUE $other_mask +setenv_var TESTSUITE_UMASK_SOURCE_SH bash +set ans [list] +lappend ans [list set _LMFILES_ $mod4p] +lappend ans [list set LOADEDMODULES $mod4] +lappend ans [list set __MODULES_LMSOURCESH "$mod4&bash $script|umask $other_mask"] +lappend ans [list set __MODULES_UMASK "$mod4&$other_mask:&$cur_mask"] +lappend ans [list umask $other_mask] +testouterr_cmd sh "load $mod4" $ans {} + +set tserr "$modlin +$mpre/$mod4: + +umask\t\t$other_mask +$modlin" +testouterr_cmd_re sh "display $mod4" OK $tserr + +# mask change recorded at load time is the one undone, whatever the script +# does now +setenv_loaded_module [list $mod4] [list $mod4p] +setenv_var __MODULES_LMSOURCESH "$mod4&bash $script|umask $cur_mask" +setenv_var __MODULES_UMASK "$mod4&$cur_mask:&0123" +set ans [list] +lappend ans [list unset _LMFILES_] +lappend ans [list unset LOADEDMODULES] +lappend ans [list unset __MODULES_LMSOURCESH] +lappend ans [list unset __MODULES_UMASK] +lappend ans [list umask 0123] +testouterr_cmd sh "unload $mod4" $ans {} + +# mask change ignored +setenv_loaded_module {} {} +unsetenv_var __MODULES_LMSOURCESH +unsetenv_var __MODULES_UMASK +setenv_var TESTSUITE_UMASK_SOURCE_SH {--ignore umask bash} +set ans [list] +lappend ans [list set _LMFILES_ $mod4p] +lappend ans [list set LOADEDMODULES $mod4] +lappend ans [list set __MODULES_LMSOURCESH "$mod4&bash $script"] +testouterr_cmd sh "load $mod4" $ans {} + + # # Cleanup #