Debian Codegraph @

sources/core/tools/oarsh-legacy/pam_oar_adopt

raw symbols SCIP
1 #!/bin/bash
2 #
3 # pam_oar_adopt is a PAM module that adopts processes launched under ssh
4 # connections made by users. The processes will be moved inside the correct
5 # job cgroup, if the user owns all cores of a node in one OAR job.
6 # If user has multiple jobs on node or one job with only a part of available
7 # cores, an error is thrown. In that case, `oarsh` must be used.
8 #
9 set -e
10
11 CGROUP_MOUNT_POINT="/dev/oar_cgroups_links"
12 OAR_CPUSETS_BASE="$CGROUP_MOUNT_POINT/cpuset/oar"
13 USER_UID_MIN=1000
14
15
16 get_oar_cpusets_of_user() {
17 readarray OAR_CPUSETS < <( cd "$OAR_CPUSETS_BASE" && ls -d "$1"_* 2>/dev/null )
18 OAR_CPUSET="${OAR_CPUSETS[0]%[[:space:]]}"
19 }
20
21 pam_account() {
22 if [ -z "$PAM_USER" ]; then
23 echo "Please launch this module via PAM"
24 exit 1
25 fi
26
27 # We exit if the pam service is su, we don't want to have the error
28 # message when using su.
29 if [ "$PAM_SERVICE" = "su-l" ]; then
30 exit 0
31 fi
32
33 # Exit if the user id is inferior than 1000 (system user), indeed there is
34 # no need to do OAR cgroups machinery in that case.
35 if [ "$(getent passwd "$PAM_USER" | cut -d: -f3)" -lt "$USER_UID_MIN" ]; then
36 exit 0
37 fi
38
39 get_vars "$PAM_USER"
40 test_pam_activation
41
42 # Four cases:
43 # - the connecting user is oar or root, we fail silently (since we are in 'sufficient' mode)
44 # - the user has no cgroups (= no jobs) on node
45 # - the user has more than one cgroup or one but without all cores
46 # - the user has one cgroup with all cores
47 if [ "$PAM_USER" = "oar" ] || [ "$PAM_USER" = "root" ]; then
48 exit 1
49 elif [ -z "$OAR_CPUSET" ]; then
50 echo "No running job for user $PAM_USER on this node." >&2
51 exit 1
52 elif [ ${#OAR_CPUSETS[*]} -ne 1 ]; then
53 cat << EOF >&2
54 Cannot connect to node using 'ssh' because you appear to have more than one job on the node.
55 Make sure to only have one job on the node, or use 'oarsh' to connect to a specific job.
56 EOF
57 exit 1
58 elif [ "$(< "$OAR_CPUSETS_BASE/$OAR_CPUSET"/cpuset.cpus)" != "$ALL_CPUSET_CPUS" ]; then
59 cat << EOF >&2
60 Cannot connect to node using 'ssh' because not all its compute resources (e.g. CPU cores or threads) are assigned to the job which reserves it.
61 Reserve the whole node, or use 'oarsh' instead.
62 EOF
63 exit 1
64 else
65 exit 0
66 fi
67 }
68
69 pam_session() {
70 if [ -z "$PAM_TYPE" ]; then
71 echo "Please launch this module via PAM"
72 exit 1
73 fi
74
75 # Exit if not a login
76 if [ "$PAM_TYPE" != "open_session" ]; then
77 exit 0
78 fi
79
80 get_vars "${PAM_RUSER:-$PAM_USER}"
81
82 # We could not find a running OAR job for this user on this node. It probably means that
83 # the user connecting is either root or oar (for example because of oarsh).
84 # We do nothing in that case.
85 if [ -z "$OAR_CPUSET" ]; then
86 exit 0
87 fi
88
89 # To have job's environment variables, we create a symkink to the already
90 # created (by oarsh) environment file. pam_env while then load it.
91 ln -fs "/var/lib/oar/$OAR_CPUSET.env" /var/lib/oar/pam.env
92
93 PIDS="$(ps -o ppid= $$)"
94 for pid in $PIDS; do
95 for cg in "$CGROUP_MOUNT_POINT"/*; do
96 echo "$pid" > "$cg/oar/$OAR_CPUSET/tasks"
97 done
98 done
99 }
100
101 test_pam_activation() {
102 # We test if the pam module should perform verification. This file is created
103 # by g5k-postinstall when required (i.e. node is running a non user deployed
104 # std env).
105 if [ ! -f "/etc/oar/pam_activated" ]; then
106 exit 0
107 fi
108 }
109
110 get_vars() {
111 get_oar_cpusets_of_user "$1"
112 ALL_CPUSET_CPUS=$(< ${OAR_CPUSETS_BASE}/cpuset.cpus)
113 }
114
115 [ $# -eq 0 ] && echo "Please provide PAM mode" && exit 1
116
117 while getopts ":as" opt; do
118 case $opt in
119 "s")
120 pam_session
121 ;;
122 "a")
123 pam_account
124 ;;
125 *)
126 echo "Unknown mode"
127 exit 1
128 ;;
129 esac
130 done