-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibshed.sh
More file actions
executable file
·726 lines (680 loc) · 21.3 KB
/
libshed.sh
File metadata and controls
executable file
·726 lines (680 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
##########################################################################
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
# version @VERSION@
prog_v="@VERSION@"
# empty definition so that the lsp won't complain, this SHOULD be defined by the
# program that loads libshed AFTER loading libshed
prog=""
# unix command line compatible booleans
# Type: int
# value: 0
_true=0
# Type: int
# value: 1
_false=1
# the SHED_SESSION_PID, this is hopefully the pid of the process that keeps the
# session alive, aka the session leader, if shed was ran with exec as part of
# the session startup process then it will be the PID of the running shed
# instance, unless a GUI_SESSION_PID exists, meaning another process is the
# session leader
SHED_SESSION_PID="$SHED_SESSION_PID"
# check if GUI_SESSION_PID is set
if [ -n "$GUI_SESSION_PID" ]; then
# since it exists that will be the SHED_SESSION_PID
# this way we can reload from an older version of shed
# otherwise shed will be the one setting this env var
SHED_SESSION_PID="$GUI_SESSION_PID"
fi
# dir for the pid files
# ${XDG_RUNTIME_DIR}/shed/${SHED_SESSION_PID}
ShedSessionDir=${XDG_RUNTIME_DIR}/shed/${SHED_SESSION_PID}
# ShedSessionDir definition used on shed versions prior to this commit
OldShedSessionDir=${XDG_RUNTIME_DIR}/GUISession${SHED_SESSION_PID}
UsingOldShedDir="$_false"
# the OldShedSessionDir will only exist if an older version of shed was the one
# that started the session and was later reloaded onto a newer one, meaning the
# new dir is not present so we have to use the old one.
if [ -d "$OldShedSessionDir" ]; then
ShedSessionDir="$OldShedSessionDir"
UsingOldShedDir="$_true"
fi
# Return type: int bool
# Usage: is_dir_empty directory
# --------------------------------------------------
# Check if directory is empty (no files matching any glob)
# Returns $_true if empty or doesn't exist, $_false if has files
is_dir_empty() {
dir="$1"
# Check if directory exists and has files
if [ -d "$dir" ]; then
# Use a simple glob test - if the glob doesn't expand, it returns the pattern
for _ in "$dir"/* ; do
# If we get here, at least one file exists
return "$_false"
done
# No files found
return "$_true"
else
# Directory doesn't exist
return "$_true"
fi
}
# directory where we are loading the user services to start from
# ${XDG_CONFIG_HOME:-${HOME}/.config}/shed/services
ServicesDir="${XDG_CONFIG_HOME:-${HOME}/.config}/shed/services"
# directory where we fallback to loading the user services to start from
# /etc/shed/services
FallbackServicesDir="/etc/shed/services"
# directory where we are loading the session components to start from
# ${XDG_CONFIG_HOME:-${HOME}/.config}/shed/components
ComponentsDir="${XDG_CONFIG_HOME:-${HOME}/.config}/shed/components"
# directory where we fallback to loading the session components to start from
# /etc/shed/components
FallbackComponentsDir="/etc/shed/components"
# path of the transient executable script, the transient program will have the
# responsability to run shed as it's child
# ${XDG_CONFIG_HOME:-${HOME}/.config}/shed/transient
UseTransient="${XDG_CONFIG_HOME:-${HOME}/.config}/shed/transient"
# fallback transient executable script, the transient program will have the
# responsability to run shed as it's child
# /etc/shed/transient
FallbackTransient="/etc/shed/transient"
# directory for loadable shallow .env files
# ${XDG_CONFIG_HOME:-${HOME}/.config}/shed/shallow.d
ShallowEnvDir="${XDG_CONFIG_HOME:-${HOME}/.config}/shed/shallow.d"
# directory for loadable shallow .env files
# /etc/shed/shallow.d
FallbackShallowEnvDir="/etc/shed/shallow.d"
# directory for loadable session .env files
# ${XDG_CONFIG_HOME:-${HOME}/.config}/shed/env.d
EnvDir="${XDG_CONFIG_HOME:-${HOME}/.config}/shed/env.d"
# directory for loadable session .env files
# /etc/shed/env.d
FallbackEnvDir="/etc/shed/env.d"
# old shed start file, contains the pid of the shed process, for compatibility
# as versions after this ought to use the new lockfile name
# ${ShedSessionDir}/shed.started
oldlockfile="${ShedSessionDir}/shed.started"
# shed lock file, contains the pid of the shed process
# ${ShedSessionDir}/shed.lock
lockfile="${ShedSessionDir}/shed.lock"
# recognize the oldlockfile as the lockfile if it exists
if [ -f "$oldlockfile" ]; then
lockfile="$oldlockfile"
fi
# contains version and start date
# ${ShedSessionDir}/shed.info
shed_info="${ShedSessionDir}/shed.info"
# shed's logs dir, service logs may be redirected to their own file in here
# ${ShedSessionDir}/logs
shed_logs_dir="${ShedSessionDir}/logs"
# logs for shed
# ${shed_logs_dir}/shed.logs
shed_log_file="${shed_logs_dir}/shed.log"
# dir for service pid files
# ${ShedSessionDir}/services
shed_service_pid_dir="${ShedSessionDir}/services"
# dir for component pid files
# ${ShedSessionDir}/components
shed_component_pid_dir="${ShedSessionDir}/components"
# defined as: ${XDG_RUNTIME_DIR}/shed/${SHED_SESSION_PID}/socket
msg_socket="${ShedSessionDir}/socket"
# defined as: ${XDG_RUNTIME_DIR}/shed/${SHED_SESSION_PID}/reply
msg_reply="${ShedSessionDir}/reply"
# Return type: void
# Usage: msg_log "level" "message"
# log level can be:
# info
# error
# debug
msg_log() {
loglevel="$1"
shift
message="$*"
case "$loglevel" in
info)
loglevel="inf"
;;
err)
loglevel="err"
;;
debug)
loglevel="dbg"
;;
esac
printf '[%s] %s: %s\n' \
"$(date '+%Y-%m-%d-%H:%M:%S')" \
"$loglevel" "$message" >> "$shed_log_file"
}
# Return type: void or string
# Usage: msg_send "message"
# --------------------------------------------------
# This function will print to stdout if called from shedc and redirect the
# message to the $msg_reply file if called from shed
msg_send() {
msg="$(date '+%Y-%m-%d-%H:%M:%S') $*"
case "$prog" in
shedc*)
printf '%s\n' "$msg"
;;
shed*)
printf '%s\n' "$msg" >> "$msg_reply"
;;
esac
}
# Return type: void
# Usage: serv_start pid_dir service_file nosock nodelay
# nosock: default $_false, if passed $_true will set
# no sock mode and no message is sent to the
# reply socket
# nodelay: default $_false, if passed $_true will set
# no delay mode and no delay will be applied
# to starting of the service
# --------------------------------------------------
# this function is not expected to have a return value as
# all messages are sent to the reply socket unless specified
serv_start() {
# No Sock, default $_false
NSck="$_false"
# No Delay, default $_false
NDlay="$_false"
NAME=""
EXEC=""
E_ARGS=""
DELAY=""
LOGFILE=""
TYPE=""
exit_status=""
logfile_path=""
p_dir="$1"
s_file="$2"
if [ "${3}" -eq "$_true" ]; then
NSck="${3}"
fi
if [ "${4}" -eq "$_true" ]; then
NDlay="${4}"
fi
# source the file to get the variables: EXEC E_ARGS from the service
. "$s_file"
NAME="${s_file##*/}"
if [ -z "$LOGFILE" ]; then
LOGFILE="${shed_logs_dir}/${NAME}.log"
fi
# check if service is already running
if [ -f "${p_dir}/${NAME}.pid" ]; then
s_pid="$(cat "${p_dir}/${NAME}.pid")"
if kill -0 "$s_pid" 2>/dev/null; then
msg_send "$NAME running"
return
elif [ -f "${p_dir}/${NAME}.est" ]; then
msg_send "$NAME oneshot already ran"
return
fi
fi
logfile_path="${LOGFILE%/*}"
if [ ! -d "$logfile_path" ]; then
mkdir -p "$logfile_path" || msg_log "error" \
"could not create logfile dir for $NAME"
fi
if [ ! -d "$logfile_path" ]; then
msg_log "info" "service $NAME not started"
return
fi
case "$TYPE" in
# guard against possible stupid values
oneshot|one|ONESHOT|ONE)
TYPE="oneshot"
;;
*)
# you're a daemon
TYPE="daemon"
;;
esac
# needed for services that got $HOME/path/service in their EXEC def
EXEC=$(printf '%s\n' "$EXEC" | sed "s@\$HOME@$HOME@")
# get the full path of the binary
EXEC=$(command -v "$EXEC")
if [ -n "$DELAY" ] && [ "$NDlay" -eq "$_false" ]; then
msg_log "info" "$NAME start delayed by $DELAY seconds"
sleep "$DELAY"
fi
msg_log "info" "starting $TYPE $NAME"
# run the service command with the arguments
s_run="exec $EXEC $E_ARGS >> $LOGFILE 2>&1"
eval "$s_run" &
# catch the pid of the process
proc_pid=$!
# write the pid of the process to the pid file
printf '%s\n' "$proc_pid" > "${p_dir}/${NAME}.pid"
[ "$NSck" -eq "$_false" ] && msg_send "$NAME started"
case "$TYPE" in
oneshot)
wait "$proc_pid"
exit_status=$?
printf '%s\n' "$exit_status" > "${p_dir}/${NAME}.est"
;;
esac
}
# Return type: void
# Usage: start_from_dir def_dir fal_dir pid_dir name_to_start
# def_dir: directory with service definitions
# fal_dir: fallback directory with service definitions
# pid_dir: directory to store pid files
# name_to_start: can be a service name or one of the macros all and firstrun
# --------------------------------------------------
# Generic function to start services from definitions located in a directory
start_from_dir() {
def_dir="$1"
fal_dir="$2"
pid_dir="$3"
start_s="$4"
use_dir="$def_dir"
nodelay="$_true"
nosock="$_false"
specific_name=""
case "$start_s" in
firstrun)
nosock="$_true" # do not write to msg_reply sock
nodelay="$_false" # have start delays
;;
all) : ;; # do nothing
*) specific_name="$start_s" ;;
esac
if is_dir_empty "$def_dir"; then
errmsg="no definitions found in '$def_dir'"
msg_send "$errmsg"
msg_log "error" "$errmsg"
use_dir="$fal_dir"
fi
if is_dir_empty "$use_dir"; then
errmsg="no definitions found in '$fal_dir'"
msg_send "$errmsg"
msg_log "error" "$errmsg"
return
fi
if [ -n "$specific_name" ]; then
s_file="${use_dir}/${specific_name}"
if [ -r "$s_file" ]; then
serv_start "$pid_dir" "$s_file" "$nosock" "$nodelay" &
else
msg_send "definition for $start_s not found in $use_dir"
fi
return
fi
for i in "${use_dir}"/* ; do
serv_start "$pid_dir" "$i" "$nosock" "$nodelay" &
done
}
# Return type: void
# Usage: start_services all | firstrun | <service name>
# all: start all services
# firstrun: start all services on first run mode
# <service name>: the service name to start
# --------------------------------------------------
# only one argument is acknowledged.
# this function is not expected to have a return value.
start_services() {
case "$prog" in
shedc*) : ;; # do nothing
shed*) : > "$msg_reply" ;; # blank msg_reply
esac
msg_send "starting services"
start_from_dir \
"${ServicesDir}" \
"${FallbackServicesDir}" \
"${shed_service_pid_dir}" \
"$1"
}
# Return type: void
# Usage: start_components all | firstrun | <component name>
# all: start all services
# firstrun: start all services on first run mode
# <component name>: the component name to start
# --------------------------------------------------
# only one argument is acknowledged.
# components are only started once when shed first runs
start_components() {
case "$prog" in
shedc*) : ;; # do nothing
shed*) : > "$msg_reply" ;; # blank msg_reply
esac
msg_send "starting components"
start_from_dir \
"${ComponentsDir}" \
"${FallbackComponentsDir}" \
"${shed_component_pid_dir}" \
"$1"
}
# Return type: string
# Usage: readkeyvalprop "PROPERTY" file
# property: key name
# Return: string containing the value of the
# PROPERTY key from the passed file.
# --------------------------------------------------
# the specific property keys for service definition
# files are:
# EXEC
# E_ARGS
# DELAY
# NOHUP
# LOGFILE
# TYPE
readkeyvalprop(){
# Setting 'IFS' tells 'read' where to split the string.
while IFS='=' read -r key val; do
# Skip over lines containing comments.
# (Lines starting with '#').
[ "${key##\#*}" ] || continue
# '$key' stores the key.
# '$val' stores the value.
if [ "$key" = "$1" ]; then
printf '%s\n' "$val"
fi
done < "$2"
}
# Return type: string
# Usage: read_file file [prefix]
# [prefix]: if provided the characters here will be used a prefix when
# printing the contents of the file.
# Return: '[prefix]line'
read_file() {
if [ $# -gt 1 ]; then
prefix=$2
else
prefix=""
fi
while read -r FileLine
do
printf '%s%s\n' "$prefix" "$FileLine"
done < "$1"
}
# Return type: int bool ($_true or $_false)
# Usage: check_hup_allowed service_file
# ----------------------------------
# Check if service allows HUP signal, if service can be hupped the return will
# be $_true, else it will be $_false
check_hup_allowed() {
canhup="$_true"
# Read NOHUP property from service file
s_nohup=$(readkeyvalprop "NOHUP" "$1")
if [ -n "$s_nohup" ]; then
case "$s_nohup" in
true|TRUE|1|yes|YES|y|Y)
canhup="$_false"
;;
esac
fi
return "$canhup"
}
# Return type: int bool ($_true or $_false)
# Usage: is_oneshot service_file
# ----------------------------------
# Check if service is a oneshot or a daemon, a oneshot will return $_true
is_oneshot() {
isoneshot="$_false"
# Read TYPE property from service file
s_type=$(readkeyvalprop "TYPE" "$1")
if [ -n "$s_type" ]; then
case "$s_type" in
oneshot|one|ONESHOT|ONE)
isoneshot="$_true"
;;
esac
fi
return "$isoneshot"
}
# Return type: void
# Usage: sig_proc <pids dir> <service dir> <f dir> <service name> <signal>
# <service name>: service to send signal
# signal: term kill hup usr1 usr2
# --------------------------------------------------
# this function is not expected to have a return value as
# all messages are sent to the reply socket
sig_proc() {
p_dir="$1"
shift
s_dir="$1"
shift
f_dir="$1"
shift
s_name="$1"
shift
signal="$1"
shift
s_file="${s_dir}/$s_name"
if [ ! -f "$s_file" ]; then
s_file="${f_dir}/$s_name"
msg_send "no service $s_name found in $s_dir, falling back to $f_dir"
fi
if [ ! -f "$s_file" ]; then
msg_send "no service $s_name found in $f_dir"
return
fi
sig_use=$(printf '%s' "$signal" | tr '[:lower:]' '[:upper:]')
sig_str=$(printf '%s' "$signal" | tr '[:upper:]' '[:lower:]')
if [ -f "${p_dir}/${s_name}.pid" ]; then
s_pid=$(read_file "${p_dir}/${s_name}.pid")
if kill -0 "$s_pid" 2>/dev/null && ! is_oneshot "$s_file"; then
if [ "hup" = "$sig_str" ] && ! check_hup_allowed "$s_file"; then
msg_send "cannot hup daemon $s_name"
else
msg_send "sending $sig_str to $s_pid $s_name"
if [ -z "$dry_run" ]; then
kill "-${sig_use}" "$s_pid"
fi
fi
else
case "$sig_str" in
term|kill)
msg_send "removing oneshot $s_name pid and exit status files"
;;
*)
msg_send "cannot send signals to oneshots"
;;
esac
fi
# remove the pid file even if process is not alive, this needs to be here
# so that the pid file for term/kill is always removed so long it exists
if [ -z "$dry_run" ]; then
case "$sig_str" in
term|kill)
rm -f "${p_dir}/${s_name}.pid"
if [ -f "${p_dir}/${s_name}.est" ]; then
rm -f "${p_dir}/${s_name}.est"
fi
;;
esac
fi
else
msg_send "service $s_name not running"
fi
}
# Return type: void
# Usage: sig_all <pids dir> <service dir> <fallback dir> <signal>
# signal: term kill hup usr1 usr2
# --------------------------------------------------
# this function calls sig_proc for every pid file in
# the $shed_service_pid_dir
sig_all() {
p_dir="$1"
shift
f_dir="$1"
shift
s_dir="$1"
shift
sig="$1"
shift
if is_dir_empty "$p_dir"; then
errmsg="no pid files found in $p_dir"
msg_send "$errmsg"
msg_log "error" "$errmsg"
return
fi
for i in "${p_dir}"/*.pid ; do
s_name="${i##*/}"
s_name="${s_name%.pid}"
sig_proc "${p_dir}" "${s_dir}" "${f_dir}" "$s_name" "$sig"
done
}
# Return type: void
# Usage: hupprocs all | <service name>
# all: hup all services
# <service name>: service to hup
# --------------------------------------------------
# this function is not expected to have a return value as
# all messages are sent to the reply socket
hupprocs() {
if [ -z "$1" ] || [ "all" = "$1" ]; then
sig_all \
"${shed_service_pid_dir}" \
"${ServicesDir}" \
"${FallbackServicesDir}" \
"hup"
else
sig_proc \
"${shed_service_pid_dir}" \
"${ServicesDir}" \
"${FallbackServicesDir}" \
"$1" \
"hup"
fi
}
# Return type: void or string
# Usage: killprocs all | <service name>
# all: kill all services
# <service name>: service to kill
# --------------------------------------------------
# when ran from shedc messages will be output to stdout
# when ran from shed messages will be redirected to $msg_reply
killprocs() {
if [ -z "$1" ] || [ "all" = "$1" ]; then
sig_all \
"${shed_service_pid_dir}" \
"${ServicesDir}" \
"${FallbackServicesDir}" \
"term"
else
sig_proc \
"${shed_service_pid_dir}" \
"${ServicesDir}" \
"${FallbackServicesDir}" \
"$1" \
"term"
fi
}
# Return type: void or string
# Usage: killcomps all | <component name>
# all: kill all services
# <component name>: component to kill
# --------------------------------------------------
# when ran from shedc messages will be output to stdout
# when ran from shed messages will be redirected to $msg_reply
# components are only killed on logout
killcomps() {
if [ -z "$1" ] || [ "all" = "$1" ]; then
sig_all \
"${shed_component_pid_dir}" \
"${ComponentsDir}" \
"${FallbackComponentsDir}" \
"term"
else
sig_proc \
"${shed_component_pid_dir}" \
"${ComponentsDir}" \
"${FallbackComponentsDir}" \
"$1" \
"term"
fi
}
# Return type: void or string
# Usage: old_kill_all_procs
# --------------------------------------------------
# special function to handle versions of shed from before
# service pidfiles were stored in $shed_service_pid_dir,
# it sends term to every process with a pidfile in $ShedSessionDir
old_kill_all_procs() {
for i in "${ShedSessionDir}"/*.pid ; do
s_pid=$(read_file "$i")
s_name="${i##*/}"
msg_send "sending term to $s_pid $s_name"
if kill -0 "$s_pid" 2>/dev/null; then
[ -z "$dry_run" ] && kill "$s_pid"
fi
[ -z "$dry_run" ] && rm -f "$i"
done
}
# Return type: int
# Usage: get_perms <file>
# --------------------------------------------------
# will return the octal permissions of the file ie: 755
get_perms() {
case "$(uname -s)" in
Linux) stat -c '%a' "$1" ;;
*BSD*|Darwin) stat -f '%Lp' "$1" ;;
esac
}
# Return type: int
# Usage: get_ownerid <file>
# --------------------------------------------------
# will return the UID of the file owner, say for a normal
# user 1000 but for the root user 0
get_ownerid() {
case "$(uname -s)" in
Linux) stat -c '%u' "$1" ;;
*BSD*|Darwin) stat -f '%u' "$1" ;;
esac
}
# Return type: int bool
# Usage: are_exec_perms_correct <e_path> <uid>
# e_path: executable file path
# uid: expected owner user id
# --------------------------------------------------
# check if the executable permissions are correct, which is:
# - the executable file exists and is in fact executable
# - the actual owner id is the expected owner id
# - the file is writeable only by the owner
are_exec_perms_correct() {
executable_path="$1"
shift
expected_owner_uid="$1"
shift
execut_realpath="$(realpath "$executable_path")"
[ ! -x "$execut_realpath" ] && return "$_false"
owner_id="$(get_ownerid "$execut_realpath")"
[ "$expected_owner_uid" -ne "$owner_id" ] && return "$_false"
exec_perms="$(get_perms "$execut_realpath")"
others_bit=$((exec_perms % 10))
[ $((others_bit & 2)) -ne 0 ] && return "$_false"
return "$_true"
}
# Return type: int bool
# Usage: is_str_valid <string>
# --------------------------------------------------
# valid strings are those that only contain
# 'alphanums', dots '.' and dashes '_-'
is_str_valid() {
case "$1" in
# the class of strings we consider false, this should mean:
# 'empty' OR those which contain strings that are NOT 'alphanums', '._-'
""|*[!A-Za-z0-9._-]*)
return "$_false"
;;
*)
return "$_true"
;;
esac
}