Repository navigation
Expand file tree
/
Copy pathinit
More file actions
executable file
·522 lines (466 loc) · 11.1 KB
/
Copy pathinit
File metadata and controls
executable file
·522 lines (466 loc) · 11.1 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
#!/bin/sh -e
#
# 2016 Daniel Heule (hda at sfs.biz)
# 2017 Philippe Gregoire (pg at pgregoire.xyz)
# 2020,2023,2025 Dennis Camera (dennis.camera at riiengineering.ch)
#
# This file is part of skonfig-base.
#
# skonfig-base 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.
#
# skonfig-base 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 skonfig-base. If not, see <http://www.gnu.org/licenses/>.
#
# Returns the name of the init system (PID 1)
#
# Expected values:
# Linux:
# Adélie Linux:
# sysvinit+openrc
# Alpine Linux:
# busybox-init+openrc
# ArchLinux:
# systemd, sysvinit
# BeOS:
# (no output, has no init)
# Chimera Linux:
# dinit
# CRUX:
# sysvinit
# Debian:
# systemd, upstart, sysvinit, openrc, ???
# Devuan:
# sysvinit, sysvinit+openrc
# DSM:
# busybox-init+syno, upstart-syno, systemd
# Gentoo:
# sysvinit+openrc, openrc-init, systemd
# Haiku:
# launch_daemon
# OpenBMC:
# systemd
# OpenWrt:
# procd, init???
# RedHat (RHEL, CentOS, Fedora, RedHat Linux, ...):
# systemd, upstart, upstart-legacy, sysvinit
# Slackware:
# sysvinit
# SuSE:
# systemd, sysvinit
# Ubuntu:
# systemd, upstart, upstart-legacy, sysvinit
# VoidLinux:
# runit
#
# GNU:
# Debian:
# sysvinit, hurd-init
#
# BSD:
# {Free,Open,Net}BSD:
# init
#
# Mac OS X:
# launchd, init+SystemStarter
#
# Solaris/Illumos:
# smf, init???
#
# NOTE: init systems can be stacked. This is popular to run OpenRC on top of
# sysvinit (Gentoo) or busybox-init (Alpine), but can also be used to run runit
# as a systemd service. This makes init system detection very complicated
# (which result is expected?) This script tries to untangle some combinations,
# OpenRC on top of sysv or busybox (X+openrc), but will ignore others (runit as
# a systemd service)
# NOTE: When we have no idea, nothing will be printed!
# NOTE:
# When trying to gather information about the init system make sure to do so
# without calling the binary! On some systems this triggers a reinitialisation
# of the system which we don't want (e.g. embedded systems).
set -e
KERNEL_NAME=$(uname -s)
KNOWN_INIT_SYSTEMS="\
systemd
sysvinit
upstart
runit
dinit
procd
smf
launchd
init
hurd_init
systemstarter"
common_candidates_by_kernel() {
case ${KERNEL_NAME}
in
(FreeBSD|NetBSD|OpenBSD)
echo init
;;
(Linux)
echo systemd
echo sysvinit
echo upstart
echo openrc
;;
(GNU)
echo sysvinit
echo hurd-init
;;
(Darwin)
echo launchd
echo systemstarter
;;
(SunOS)
echo smf
;;
esac
}
## Helpers
trim() {
sed -e 's/^[[:blank:]]*//' -e 's/[[:blank:]]*$//' -e '/^[[:blank:]]*$/d'
}
unique() {
# Delete duplicate lines (keeping input order)
# NOTE: Solaris AWK breaks without if/print construct.
awk '{ if (!x[$0]++) print }'
}
## Check functions
# These functions are used to verify if a guess is correct by checking some
# common property of a running system (presence of a directory in /run etc.)
check_busybox_init() (
busybox_path=${1:-/bin/busybox}
test -x "${busybox_path}" || return 1
grep -q 'BusyBox v[0-9]' "${busybox_path}" || return 1
# It is quite common to use BusyBox’s init to stack other service managers
# (like OpenRC) on top of it. So we check for that, too.
if stacked=$(check_openrc)
then
echo "busybox-init+${stacked}"
elif stacked=$(check_synoinit)
then
# Synology DSM < 5.0 uses BusyBox + synoservice(8)
echo "busybox-init+${stacked}"
else
echo busybox-init
fi
)
check_dinit() {
init_exe=${1:-/sbin/dinit}
test -x "${init_exe}" || return 1
test -S /run/dinitctl || test -S /var/run/dinitctl || return 1
echo dinit
}
check_hurd_init() (
init_exe=${1:-/hurd/init}
test -x "${init_exe}" || return 1
grep -qF 'GNU Hurd' "${init_exe}" || return 1
echo hurd-init
)
check_init() {
# Checks for various BSD inits...
test -x /sbin/init || return 1
case ${KERNEL_NAME}
in
(*BSD)
if grep -qF -e "${KERNEL_NAME}" /sbin/init
then
echo init
return 0
fi
;;
esac
}
check_launchd() {
command -v launchctl >/dev/null 2>&1 || return 1
launchctl getenv PATH >/dev/null || return 1
echo launchd
}
check_launch_daemon() {
command -v launch_roster >/dev/null 2>&1 || return 1
echo launch_daemon
}
check_openrc() {
test -f /run/openrc/softlevel || return 1
echo openrc
}
check_procd() (
procd_path=${1:-/sbin/procd}
test -x "${procd_path}" || return 1
grep -qF 'procd' "${procd_path}" || return 1
echo procd
)
check_runit() {
test -d /run/runit || return 1
echo runit
}
check_smf() {
# XXX: Is this the correct way??
test -f /etc/svc/volatile/svc_nonpersist.db || return 1
echo smf
}
check_synoinit() {
# synoservice(8)
test -x /usr/syno/sbin/synoservice || return 1
echo syno
}
check_systemd() {
# NOTE: sd_booted(3)
test -d /run/systemd/system/ || return 1
# systemctl --version | sed -e '/^systemd/!d;s/^systemd //'
echo systemd
}
check_systemstarter() {
test -d /System/Library/StartupItems/ || return 1
test -f /System/Library/StartupItems/LoginWindow/StartupParameters.plist || return 1
echo init+SystemStarter
}
check_sysvinit() (
init_path=${1:-/sbin/init}
test -x "${init_path}" || return 1
grep -q 'INIT_VERSION=sysvinit-[0-9.]*' "${init_path}" || return 1
# It is quite common to use SysVinit to stack other init systemd
# (like OpenRC) on top of it. So we check for that, too.
if stacked=$(check_openrc)
then
echo "sysvinit+${stacked}"
else
echo sysvinit
fi
unset -v stacked
)
check_upstart() {
test -x "$(command -v initctl 2>/dev/null)" || return 1
case $(initctl version)
in
(*'(upstart '*')')
if test -x /usr/syno/sbin/synoservice \
&& test -x /usr/syno/sbin/synoservicectl
then
# upstart-based Synology init system
echo upstart-syno
elif test -d /etc/init
then
# modern (DBus-based?) upstart >= 0.5
echo upstart
elif test -d /etc/event.d
then
# ancient upstart
echo upstart-legacy
else
# whatever...
echo upstart
fi
;;
(*)
return 1
;;
esac
}
find_init_procfs() (
# First, check if the required file in procfs exists...
test -h /proc/1/exe || return 1
# Find init executable
init_exe=$(ls -l /proc/1/exe 2>/dev/null) || return 1
init_exe=${init_exe#* -> }
if ! test -x "${init_exe}"
then
# On some rare occasions it can happen that the
# running init's binary has been replaced. In this
# case Linux adjusts the symlink to "X (deleted)"
# [root@fedora-12 ~]# readlink /proc/1/exe
# /sbin/init (deleted)
# [root@fedora-12 ~]# ls -l /proc/1/exe
# lrwxrwxrwx. 1 root root 0 2020-01-30 23:00 /proc/1/exe -> /sbin/init (deleted)
init_exe=${init_exe% (deleted)}
fi
test -x "${init_exe}" || return 1
echo "${init_exe}"
)
guess_by_path() {
case $1
in
(/bin/busybox)
check_busybox_init "$1" && return
;;
(/boot/system/servers/launch_daemon)
check_launch_daemon "$1" && return
;;
(/lib/systemd/systemd)
check_systemd "$1" && return
;;
(/hurd/init)
check_hurd_init "$1" && return
;;
(/sbin/launchd)
check_launchd "$1" && return
;;
(/usr/bin/runit|/sbin/runit)
check_runit "$1" && return
;;
(/sbin/openrc-init)
if check_openrc "$1" >/dev/null
then
echo openrc-init
return
fi
;;
(/sbin/procd)
check_procd "$1" && return
;;
(/sbin/dinit|/usr/bin/dinit)
check_dinit "$1" && return
;;
(/sbin/init|*/init)
# init: it could be anything -> (explicit) no match
return 1
;;
esac
# No match
return 1
}
guess_by_comm_name() {
case $1
in
(busybox)
check_busybox_init && return
;;
(openrc-init)
if check_openrc >/dev/null
then
echo openrc-init
return 0
fi
;;
(init)
# init could be anything -> no match
return 1
;;
(*)
# Run check function by comm name if available.
# Fall back to comm name if either it does not exist or
# returns non-zero.
if type "check_$1" >/dev/null
then
"check_$1" && return
else
echo "$1"
return 0
fi
;;
esac
return 1
}
check_list() (
# List must be a multi-line input on stdin (one name per line)
while read -r init
do
if type "check_${init}" >/dev/null
then
"check_${init}" || continue
return 0
fi
done
return 1
)
# BusyBox’s versions of ps and pgrep do not support some options
# depending on which compile-time options have been used.
find_init_pgrep() {
command -v pgrep >/dev/null 2>&1 || return 1
pgrep -P0 -fl 2>/dev/null | awk -F '[[:blank:]]' '$1 == 1 { print $2 }'
}
find_init_ps() {
case ${KERNEL_NAME}
in
(Darwin)
ps -o command -p 1 2>/dev/null | tail -n +2
;;
(FreeBSD)
ps -o args= -p 1 2>/dev/null | cut -d ' ' -f 1
;;
(Linux)
ps -o comm= -p 1 2>/dev/null
;;
(NetBSD)
ps -o comm= -p 1 2>/dev/null
;;
(OpenBSD)
ps -o args -p 1 2>/dev/null | tail -n +2 | cut -d ' ' -f 1
;;
(*)
ps -o args= -p 1 2>/dev/null
;;
esac \
| trim # trim trailing whitespace (some ps like Darwin add it)
}
find_init() {
case ${KERNEL_NAME}
in
(Linux|GNU|NetBSD)
find_init_procfs || find_init_pgrep || find_init_ps
;;
(FreeBSD)
find_init_procfs || find_init_ps
;;
(BeOS)
# BeOS does not have an init program like other UNIX systems.
# Being a desktop OS it does not have services or such things known
# from other Unices.
# It has some shell scripts (/boot/beos/system/boot/Bootscript & co)
# to start and stop some programs, that’s all.
exit 0
;;
(Haiku)
# NOTE: Haiku is special in that it does not have an init
# program like other UNIX systems.
# Beginning with Haiku R1/beta1, the OS has some
# sort of service manager, but it is not the parent
# of all other processes, so the common detection
# strategies don't work.
if test -x /boot/system/servers/launch_daemon
then
echo /boot/system/servers/launch_daemon
else
# Older versions of Haiku boot using Bootscript (like BeOS, but
# located in /boot/system/boot/Bootscript) and don't have
# services or such things.
exit 0
fi
;;
(OpenBSD)
find_init_pgrep || find_init_ps
;;
(Darwin|SunOS)
find_init_ps
;;
(*)
echo "Don't know how to determine init." >&2
echo 'Please send a patch.' >&2
exit 1
;;
esac
}
# -----
init=$(find_init)
# If we got a path, guess by the path first (fall back to file name if no match)
# else guess by file name directly.
# shellcheck disable=SC2015
{
test -x "${init}" \
&& guess_by_path "${init}" \
|| guess_by_comm_name "$(basename "${init}")"
} && exit 0 || :
# Guessing based on the file path and name didn’t lead to a definitive result.
#
# We go through all of the checks until we find a match. To speed up the
# process, common cases will be checked first based on the underlying kernel.
{ common_candidates_by_kernel; echo "${KNOWN_INIT_SYSTEMS}"; } \
| unique | check_list