Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .hunspell.en.dic
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ indepth
inhibitErrorReport
init
initStateClockSeconds
initStateUmask
initStateUsergroups
initadd
initclear
Expand Down Expand Up @@ -1674,3 +1675,5 @@ TMOD
getPathElementWeight
withdrawPriorityDemands
getPathWeightList
0o0077
o0700
31 changes: 31 additions & 0 deletions MIGRATING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Expand Down
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ 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.
* 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:
Expand Down
6 changes: 4 additions & 2 deletions doc/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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_\<VAR\>`, |
| | :envvar:`MODULES_FAMILY_\<NAME\>_VERSION` |
| | :envvar:`MODULES_FAMILY_\<NAME\>_VERSION`, |
| | :envvar:`__MODULES_UMASK` |
+------------+-----------------------------------------------------------------+

Modules Specific Tcl Commands
Expand Down Expand Up @@ -1188,7 +1189,8 @@ The following modulefile Tcl commands appeared on Modules 5.
+------------+-----------------------------------------------------------------+
| 5.8 | :mfcmd:`module-info modulename<module-info>`, |
| | :mfcmd:`module-info moduleversion<module-info>`, |
| | :mfcmd:`module-forbid-regex`, :mfcmd:`module-hide-regex` |
| | :mfcmd:`module-forbid-regex`, :mfcmd:`module-hide-regex`, |
| | :mfcmd:`umask` |
+------------+-----------------------------------------------------------------+

:mfcmd:`system`, :mfcmd:`is-used`
Expand Down
2 changes: 2 additions & 0 deletions doc/source/design/extra-match-search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ specifiers and their associated modulefile command.
+--------------------+-----------------------------------------+
| chdir | chdir |
+--------------------+-----------------------------------------+
| umask | umask |
+--------------------+-----------------------------------------+
| family | family, provided-alias |
+--------------------+-----------------------------------------+
| provide | provide, provided-alias |
Expand Down
3 changes: 2 additions & 1 deletion doc/source/design/source-shell-script-in-modulefile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
159 changes: 159 additions & 0 deletions doc/source/design/umask.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
.. _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_\<VAR\>` 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.

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.

.. vim:set tabstop=2 shiftwidth=2 expandtab autoindent:
29 changes: 25 additions & 4 deletions doc/source/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2795,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.
Expand Down Expand Up @@ -2832,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`.
Expand Down Expand Up @@ -3502,7 +3508,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``
Expand Down Expand Up @@ -4866,6 +4872,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*.
Expand Down
Loading
Loading