Docsity

User Defined Command Context in Linux, Schemes and Mind Maps of Computer science

Toggle sidebar
bg1
Shell Context
ā— Process context
In Linux, all user-space processes are spawned from another process, (with
the exception of the very first processes created by the kernel at boot time.)
All child processes inherit a context automatically from their parent processes.
This includes:
Category
Inherited Items
Identity & Security
Real UID, Effective UID, Saved UID, Real GID, Effective GID,
Saved GID, Supplementary groups, Capabilities, Security
context (SELinux/AppArmor)
Execution
Environment
Environment variables, Locale settings, Umask
Filesystem Context
Current working directory (CWD), Root directory (chroot),
Open file descriptors, File descriptor flags (e.g.
FD_CLOEXEC), Memory-mapped files (inheritable)
Signal State
Signal dispositions (handlers / ignored / default), Signal
mask
Resource &
Scheduling
Resource limits (rlimit), Nice value, Scheduling policy /
priority
Process
Relationships
Parent PID (PPID), Process group ID (PGID), Session ID
(SID), Controlling terminal
IPC & Shared State
Pipes, Sockets, Message queues, Shared memory
mappings
pf3
pf4
pf5
pf8

Partial preview of the text

Download User Defined Command Context in Linux and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Shell Context

ā— Process context

In Linux, all user-space processes are spawned from another process, (with the exception of the very first processes created by the kernel at boot time.)

All child processes inherit a context automatically from their parent processes. This includes:

Category Inherited Items

Identity & Security Real UID, Effective UID, Saved UID, Real GID, Effective GID, Saved GID, Supplementary groups, Capabilities, Security context (SELinux/AppArmor)

Execution Environment

Environment variables, Locale settings, Umask

Filesystem Context Current working directory (CWD), Root directory (chroot), Open file descriptors, File descriptor flags (e.g. FD_CLOEXEC), Memory-mapped files (inheritable) Signal State Signal dispositions (handlers / ignored / default), Signal mask Resource & Scheduling

Resource limits (rlimit), Nice value, Scheduling policy / priority Process Relationships

Parent PID (PPID), Process group ID (PGID), Session ID (SID), Controlling terminal

IPC & Shared State Pipes, Sockets, Message queues, Shared memory mappings

Runtime / Kernel State

Address space (copy-on-write), Thread state (if multithreaded)

The shell is essentially a process, and all that spawns from it whether programs or instances of the shell itself, are its child processes. Like all other processes the shell too passes down the process context discussed above, to its child processes.

Along with the process context, in the case of shells, there is another category of objects: the ā€œshell configurationā€ including aliases, functions and variables.

This category is not automatically inherited by child processes, unlike process context. It is obtained by sourcing files where these objects are defined like .bashrc. Informally, all of this is bundled together and imprecisely termed: the shell environment. To be precise, we will distinguish between the two:

Shell Context

Process context

Environment variables, CWD, file descriptors, UID, GID …. and much more

Shell configuration

Aliases, functions, shell variables (those not exported).

User-Defined Command Context

There are five kinds of objects that constitute the user-defined command context in Linux.

  1. Aliases

  2. Variables (shell variables, environment variables)

  3. Shell functions

  4. Scripts (ā€œshell scriptsā€, if the interpreter is the shell itself)

  5. Programs (those that are compiled before execution.)

  6. Aliases :

Alises function as macro text replacement. Their primary usage is to replace a long string of commands (or a single long command) with a short string.

Aliases can’t take arguments.

Inclusion method Availability

Defining aliases in a file, then sourcing the file.

Available in whichever shell sources the file.

  1. Variables : Variables are labels you can store data under. These can be used to change the environment, or pass data from one process to another.

Inclusion method Availability

Defining variables in a file, then sourcing the file.

Available in the shell which sources the file.

defining and exporting variables in a file, then sourcing the file.

Available in the shell which sources the file and all its child processes.

  1. Shell functions :

A bunch of commands strung together, the procedure given a name. The entire sequence of commands can be invoked using the name. This can take arguments, unlike aliases.

Inclusion method Availability

Defining functions in a file, then sourcing the file.

Available in the shell which sources the file.

defining and exporting functions in a file, then sourcing the file. (fragile, NOT recommended)

Available in the shell which sources the file and all its child processes.

  1. Scripts Scripts are just a sequence of commands put in a file. Scripts, as opposed to programs, are run line-by-line by an interpreter, without being compiled first. If the interpreter is the shell itself (say, bash) then these are called ā€œshell scriptsā€. These could also be Python or Perl scripts.

Note : A script run as ./script.sh is a child process. However, commands run in parentheses (command) are subshells. They are clones of the current shell (including non-exported variables) but still cannot modify the parent.

  1. Programs A Program is a sequence of commands which needs to be compiled first before it can be executed. The only way to run a program is by entering its path (relative or full) in the file tree. (or just its name if its directory is in PATH). And so it will always execute as a chile process. You can’t source a program like you can a shell script.

NOTE : aliases, variables, shell functions and scripts could also be just typed in the command line interactively. This is what sourcing results in anyway. But that is not a sane practice. Usually you just put the most used objects in a file and add a command to source them (or source the files containing them) in your config files like .bashrc.

Type Aliases Shell functions Scripts Programs description Basic text replacement. Used to replace long sequence of commands with a short string. Can’t

Sequence of commands which can take arguments; defined in a file

Sequence of commands which can take arguments; put in a file.

Sequence of commands which can take arguments; need to be compiled before

take arguments. execution. Method of inclusion/ execution

Source file which contains definition

Source file which contains definition

Always available via path. Either execute directly or source.

Always available via path, executed directly, can’t be sourced Shell session

Current shell Current shell Child process if executed directly, current shell if sourced.

As a separate process always

Misc ā— the exec command allows a program to replace the current shell process (keeping the same PID) instead of spawning a child. This is the only way a program runs without being a "child."