forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinit-lib.pl
executable file
·2478 lines (2179 loc) · 61 KB
/
init-lib.pl
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=head1 init-lib.pl
Common functions for SYSV-style boot/shutdown sequences, MacOS, FreeBSD
and Windows. Because each system uses a different format and semantics for
bootup actions, there are separate functions for listing and managing each
type. However, some functions like enable_at_boot and disable_at_boot can
creation actions regardless of the underlying boot system.
Example code :
foreign_require('init', 'init-lib.pl');
$ok = init::action_status('foo');
if ($ok == 0) {
init::enable_at_boot('foo', 'Start or stop the Foo server',
'/etc/foo/start', '/etc/foo/stop');
}
=cut
BEGIN { push(@INC, ".."); };
use WebminCore;
&init_config();
@action_buttons = ( 'start', 'restart', 'condrestart', 'reload', 'status',
'stop' );
%access = &get_module_acl();
=head2 init_mode
This variable is set based on the bootup system in use. Possible values are :
=item osx - MacOSX hostconfig files, for older versions
=item launchd - MacOS Launchd, for newer versions
=item rc - FreeBSD 6+ RC files
=item init - System V init.d files, seen on Linux and Solaris
=item local - A single rc.local file
=item win32 - Windows services
=item upstart - Upstart, seen on Ubuntu 11
=item systemd - SystemD, seen on Fedora 16
=cut
if ($config{'init_mode'}) {
$init_mode = $config{'init_mode'};
}
elsif (&has_command("launchd")) {
$init_mode = "launchd";
}
elsif ($config{'hostconfig'}) {
$init_mode = "osx";
}
elsif ($config{'rc_dir'}) {
$init_mode = "rc";
}
elsif ($config{'init_base'} && -d "/etc/systemd" &&
&has_command("systemctl") &&
&execute_command("systemctl list-units") == 0) {
$init_mode = "systemd";
}
elsif ($config{'init_base'} && -d "/etc/init" && &has_command("initctl")) {
$init_mode = "upstart";
}
elsif ($config{'init_base'}) {
$init_mode = "init";
}
elsif ($config{'local_script'}) {
$init_mode = "local";
}
elsif ($gconfig{'os_type'} eq 'windows') {
$init_mode = "win32";
}
=head2 runlevel_actions(level, S|K)
Return a list of init.d actions started or stopped in some run-level, each of
which is a space-separated string in the format : number name inode
=cut
sub runlevel_actions
{
local($dir, $f, @stbuf, @rv);
$dir = &runlevel_dir($_[0]);
opendir(DIR, $dir);
foreach $f (readdir(DIR)) {
if ($f !~ /^([A-Z])(\d+)(.*)$/ || $1 ne $_[1]) { next; }
if (!(@stbuf = stat("$dir/$f"))) { next; }
push(@rv, "$2 $3 $stbuf[1]");
}
closedir(DIR);
@rv = sort { @a = split(/\s/,$a); @b = split(/\s/,$b); $a[0] <=> $b[0]; } @rv;
return $_[1] eq "S" ? @rv : reverse(@rv);
}
=head2 list_runlevels
Returns a list of known runlevels, such as : 2 3 5.
=cut
sub list_runlevels
{
local(@rv);
opendir(DIR, $config{init_base});
foreach (readdir(DIR)) {
if (/^rc([A-z0-9])\.d$/ || /^(boot)\.d$/) {
#if (!$config{show_opts} && $1 < 1) { next; }
push(@rv, $1);
}
}
closedir(DIR);
return sort(@rv);
}
=head2 list_actions
List boot time action names from init.d, such as httpd and cron.
=cut
sub list_actions
{
local($dir, $f, @stbuf, @rv);
$dir = $config{init_dir};
opendir(DIR, $dir);
foreach $f (sort { lc($a) cmp lc($b) } readdir(DIR)) {
if ($f eq "." || $f eq ".." || $f =~ /\.bak$/ || $f eq "functions" ||
$f eq "core" || $f eq "README" || $f eq "rc" || $f eq "rcS" ||
-d "$dir/$f" || $f =~ /\.swp$/ || $f eq "skeleton" ||
$f =~ /\.lock$/ || $f =~ /\.dpkg-(old|dist)$/ ||
$f =~ /^\.depend\./ || $f eq '.legacy-bootordering' ||
$f =~ /^mandrake/) { next; }
if (@stbuf = stat("$dir/$f")) {
push(@rv, "$f $stbuf[1]");
}
}
closedir(DIR);
foreach $f (split(/\s+/, $config{'extra_init'})) {
if (@stbuf = stat($f)) {
push(@rv, "$f $stbuf[1]");
}
}
return @rv;
}
=head2 action_levels(S|K, action)
Return a list of run levels in which some action (from init.d) is started
or stopped. Each item is a space-separated string in the format : level order name
=cut
sub action_levels
{
local(@stbuf, $rl, $dir, $f, @stbuf2, @rv);
@stbuf = stat(&action_filename($_[1]));
foreach $rl (&list_runlevels()) {
$dir = &runlevel_dir($rl);
opendir(DIR, $dir);
foreach $f (readdir(DIR)) {
if ($f =~ /^([A-Z])(\d+)(.*)$/ && $1 eq $_[0]) {
@stbuf2 = stat("$dir/$f");
if ($stbuf[1] == $stbuf2[1]) {
push(@rv, "$rl $2 $3");
last;
}
}
}
closedir(DIR);
}
return @rv;
}
=head2 action_filename(name)
Returns the path to the file in init.d for some action, such as /etc/init.d/foo.
=cut
sub action_filename
{
return $_[0] =~ /^\// ? $_[0] : "$config{init_dir}/$_[0]";
}
=head2 runlevel_filename(level, S|K, order, name)
Returns the path to the actual script run at boot for some action, such as
/etc/rc3.d/S99foo.
=cut
sub runlevel_filename
{
local $n = $_[3];
$n =~ s/^(.*)\///;
return &runlevel_dir($_[0])."/$_[1]$_[2]$n";
}
=head2 add_rl_action(action, runlevel, S|K, order)
Add some existing action to a runlevel. The parameters are :
=item action - Name of the action, like foo
=item runlevel - A runlevel number, like 3
=item S|K - Either S for an action to run at boot, or K for shutdown
=item order - Numeric boot order, like 99
=cut
sub add_rl_action
{
$file = &runlevel_filename($_[1], $_[2], $_[3], $_[0]);
while(-r $file) {
if ($file =~ /^(.*)_(\d+)$/) { $file = "$1_".($2+1); }
else { $file = $file."_1"; }
}
&lock_file($file);
if ($config{soft_links}) {
&symlink_file(&action_filename($_[0]), $file);
}
else {
&link_file(&action_filename($_[0]), $file);
}
&unlock_file($file);
}
=head2 delete_rl_action(name, runlevel, S|K)
Delete some action from a runlevel. The parameters are :
=item action - Name of the action, like foo.
=item runlevel - A runlevel number, like 3.
=item S|K - Either S for an action to run at boot, or K for shutdown.
=cut
sub delete_rl_action
{
local(@stbuf, $dir, $f, @stbuf2);
@stbuf = stat(&action_filename($_[0]));
$dir = &runlevel_dir($_[1]);
opendir(DIR, $dir);
foreach $f (readdir(DIR)) {
if ($f =~ /^([A-Z])(\d+)(.+)$/ && $1 eq $_[2]) {
@stbuf2 = stat("$dir/$f");
if ($stbuf[1] == $stbuf2[1]) {
# found file to delete.. unlink
&unlink_logged("$dir/$f");
last;
}
}
}
closedir(DIR);
}
=head2 reorder_rl_action(name, runlevel, S|K, new_order)
Change the boot order of some existing runlevel action. The parameters are :
=item action - Name of the action, like foo.
=item runlevel - A runlevel number, like 3.
=item S|K - Either S for an action to run at boot, or K for shutdown.
=item new_order - New numeric boot order to use, like 99.
=cut
sub reorder_rl_action
{
local(@stbuf, $dir, $f, @stbuf2);
@stbuf = stat(&action_filename($_[0]));
$dir = &runlevel_dir($_[1]);
opendir(DIR, $dir);
foreach $f (readdir(DIR)) {
if ($f =~ /^([A-Z])(\d+)(.+)$/ && $1 eq $_[2]) {
@stbuf2 = stat("$dir/$f");
if ($stbuf[1] == $stbuf2[1]) {
# Found file that needs renaming
$file = &runlevel_dir($_[1])."/$1$_[3]$3";
while(-r $file) {
if ($file =~ /^(.*)_(\d+)$/)
{ $file = "$1_".($2+1); }
else { $file = $file."_1"; }
}
&rename_logged("$dir/$f", $file);
last;
}
}
}
closedir(DIR);
}
=head2 rename_action(old, new)
Change the name of an action in init.d, and re-direct all soft links
to it from the runlevel directories. Parameters are :
=item old - Old action name.
=item new - New action name.
=cut
sub rename_action
{
local($file, $idx, $old);
foreach (&action_levels('S', $_[0])) {
/^(\S+)\s+(\S+)\s+(\S+)$/;
$file = &runlevel_dir($1)."/S$2$3";
if (readlink($file)) {
# File is a symbolic link.. change it
&lock_file($file);
&unlink_file($file);
&symlink_file("$config{init_dir}/$_[1]", $file);
&unlock_file($file);
}
if (($idx = index($file, $_[0])) != -1) {
$old = $file;
substr($file, $idx, length($_[0])) = $_[1];
&rename_logged($old, $file);
}
}
foreach (&action_levels('K', $_[0])) {
/^(\S+)\s+(\S+)\s+(\S+)$/;
$file = &runlevel_dir($1)."/K$2$3";
if (readlink($file)) {
# File is a symbolic link.. change it
&lock_file($file);
&unlink_file($file);
&symlink_file("$config{init_dir}/$_[1]", $file);
&unlock_file($file);
}
if (($idx = index($file, $_[0])) != -1) {
$old = $file;
substr($file, $idx, length($_[0])) = $_[1];
&rename_logged($old, $file);
}
}
&rename_logged("$config{init_dir}/$_[0]", "$config{init_dir}/$_[1]");
}
=head2 rename_rl_action(runlevel, S|K, order, old, new)
Change the name of a runlevel file. For internal use only.
=cut
sub rename_rl_action
{
&rename_logged(&runlevel_dir($_[0])."/$_[1]$_[2]$_[3]",
&runlevel_dir($_[0])."/$_[1]$_[2]$_[4]");
}
=head2 get_inittab_runlevel
Returns the runlevels entered at boot time. If more than one is returned,
actions from all of them are used.
=cut
sub get_inittab_runlevel
{
local %iconfig = &foreign_config("inittab");
local @rv;
local $id = $config{'inittab_id'};
if (open(TAB, $iconfig{'inittab_file'})) {
# Read the inittab file
while(<TAB>) {
if (/^$id:(\d+):/ && $1) { @rv = ( $1 ); }
}
close(TAB);
}
if (&has_command("runlevel")) {
# Use runlevel command to get current level
local $out = &backquote_command("runlevel");
if ($out =~ /^(\S+)\s+(\S+)/) {
push(@rv, $2);
}
}
elsif (&has_command("who")) {
# Use who -r command to get runlevel
local $out = &backquote_command("who -r 2>/dev/null");
if (!$? && $out =~ /run-level\s+(\d+)/) {
push(@rv, $1);
}
}
# Last ditch fallback - assume runlevel 3
if (!@rv && !$config{'inittab_extra'}) {
push(@rv, 3);
}
# Add statically configured runlevels
if (@rv && $config{"inittab_rl_$rv[0]"}) {
@rv = split(/,/, $config{"inittab_rl_$rv[0]"});
}
push(@rv, $config{'inittab_extra'});
return &unique(@rv);
}
=head2 init_description(file, [&hasargs])
Given a full path to an init.d file, returns a description from the comments
about what it does. If the hasargs hash ref parameter is given, it is filled
in with supported parameters to the action, like 'start' and 'stop'.
=cut
sub init_description
{
# Read contents of script, extract start/stop commands
open(FILE, $_[0]);
local @lines = <FILE>;
close(FILE);
local $data = join("", @lines);
if ($_[1]) {
foreach (@lines) {
if (/^\s*(['"]?)([a-z]+)\1\)/i) {
$_[1]->{$2}++;
}
}
}
local $desc;
if ($config{'daemons_dir'}) {
# First try the daemons file
local %daemon;
if ($_[0] =~ /\/([^\/]+)$/ &&
&read_env_file("$config{'daemons_dir'}/$1", \%daemon) &&
$daemon{'DESCRIPTIVE'}) {
return $daemon{'DESCRIPTIVE'};
}
}
if ($config{'chkconfig'}) {
# Find the redhat-style description: section
foreach (@lines) {
s/\r|\n//g;
if (/^#+\s*description:(.*?)(\\?$)/) {
$desc = $1;
}
elsif (/^#+\s*(.*?)(\\?$)/ && $desc && $1) {
$desc .= "\n".$1;
}
if ($desc && !$2) {
last;
}
}
}
elsif ($config{'init_info'} || $data =~ /BEGIN INIT INFO/) {
# Find the suse-style Description: line
foreach (@lines) {
s/\r|\n//g;
if (/^#\s*(Description|Short-Description):\s*(.*)/) {
$desc = $2;
}
}
}
else {
# Use the first comments
foreach (@lines) {
s/\r|\n//g;
next if (/^#!\s*\/(bin|sbin|usr)\// || /\$id/i || /^#+\s+@/ ||
/source function library/i || /^#+\s*copyright/i);
if (/^#+\s*(.*)/) {
last if ($desc && !$1);
$desc .= $1."\n" if ($1);
}
elsif (/\S/) { last; }
}
$_[0] =~ /\/([^\/]+)$/;
$desc =~ s/^Tag\s+(\S+)\s*//i;
$desc =~ s/^\s*$1\s+//;
}
return $desc;
}
=head2 chkconfig_info(file)
If a file has a chkconfig: section specifying the runlevels to start in and
the orders to use, return an array containing the levels (as array ref),
start order, stop order and description.
=cut
sub chkconfig_info
{
local @rv;
local $desc;
open(FILE, $_[0]);
while(<FILE>) {
if (/^#\s*chkconfig:\s+(\S+)\s+(\d+)\s+(\d+)/) {
@rv = ( $1 eq '-' ? [ ] : [ split(//, $1) ], $2, $3 );
}
elsif (/^#\s*description:\s*(.*)/) {
$desc = $1;
}
}
close(FILE);
$rv[3] = $desc if ($desc && @rv);
return @rv;
}
=head2 action_status(action)
Returns 0 if some action doesn't exist, 1 if it does but is not enabled,
or 2 if it exists and is enabled. This works for all supported boot systems,
such as init.d, OSX and FreeBSD.
=cut
sub action_status
{
my ($name) = @_;
if ($init_mode eq "upstart") {
# Check upstart service status
local $out = &backquote_command("initctl status ".
quotemeta($name)." 2>&1");
if (!$?) {
my $cfile = "/etc/init/$name.conf";
open(CONF, $cfile);
while(<CONF>) {
if (/^(#*)\s*start/) {
return $1 ? 1 : 2;
}
}
close(CONF);
return 1; # Should never happen
}
}
elsif ($init_mode eq "systemd") {
# Check systemd service status
local $unit = $name;
$unit .= ".service" if ($unit !~ /\.service$/);
local $out = &backquote_command("systemctl show ".
quotemeta($unit)." 2>&1");
if ($out =~ /UnitFileState=(\S+)/ &&
$out !~ /Description=LSB:\s/) {
# Exists .. but is it started at boot?
return lc($1) eq 'enabled' || lc($1) eq 'static' ? 2 : 1;
}
}
if ($init_mode eq "init" || $init_mode eq "upstart" ||
$init_mode eq "systemd") {
# Look for init script
local ($a, $exists, $starting, %daemon);
foreach $a (&list_actions()) {
local @a = split(/\s+/, $a);
if ($a[0] eq $name) {
$exists++;
local @boot = &get_inittab_runlevel();
foreach $s (&action_levels("S", $a[0])) {
local ($l, $p) = split(/\s+/, $s);
$starting++ if (&indexof($l, @boot) >= 0);
}
}
}
if ($starting && $config{'daemons_dir'} &&
&read_env_file("$config{'daemons_dir'}/$name", \%daemon)) {
$starting = lc($daemon{'ONBOOT'}) eq 'yes' ? 1 : 0;
}
return !$exists ? 0 : $starting ? 2 : 1;
}
elsif ($init_mode eq "local") {
# Look for entry in rc.local
local $fn = "$module_config_directory/$name.sh";
local $cmd = "$fn start";
open(LOCAL, $config{'local_script'});
while(<LOCAL>) {
s/\r|\n//g;
$found++ if ($_ eq $cmd);
}
close(LOCAL);
return $found && -r $fn ? 2 : -r $fn ? 1 : 0;
}
elsif ($init_mode eq "win32") {
# Look for a win32 service, enabled at boot
local ($svc) = &list_win32_services($name);
return !$svc ? 0 :
$svc->{'boot'} == 2 ? 2 : 1;
}
elsif ($init_mode eq "rc") {
# Look for an RC script
local @rcs = &list_rc_scripts();
local ($rc) = grep { $_->{'name'} eq $name } @rcs;
return !$rc ? 0 :
$rc->{'enabled'} ? 2 : 1;
}
elsif ($init_mode eq "osx") {
# Look for a hostconfig entry
local $ucname = uc($name);
local %hc;
&read_env_file($config{'hostconfig'}, \%hc);
return $hc{$ucname} eq '-YES-' ? 2 :
$hc{$ucname} eq '-NO-' ? 1 : 0;
}
elsif ($init_mode eq "launchd") {
local @agents = &list_launchd_agents();
local ($agent) = grep { $_->{'name'} eq &launchd_name($name) } @agents;
return !$agent ? 0 :
$agent->{'boot'} ? 2 : 1;
}
}
=head2 enable_at_boot(action, description, startcode, stopcode, statuscode, &opts)
Makes some action start at boot time, creating the script by copying the
specified file if necessary. The parameters are :
=item action - Name of the action to create or enable.
=item description - A human-readable description for the action.
=item startcode - Shell commands to run at boot time.
=item stopcode - Shell commands to run at shutdown time.
=item statuscode - Shell code to output the action's status.
=item opts - Hash ref of additional options, like : fork -> server will fork into background
If this is called for a named action that already exists (even if it isn't
enabled), only the first parameter needs to be given.
=cut
sub enable_at_boot
{
local $st = &action_status($_[0]);
return if ($st == 2); # already exists and is enabled
local ($daemon, %daemon);
local $unit = $_[0];
$unit .= ".service" if ($unit !~ /\.service$/);
if ($init_mode eq "upstart" && (!-r "$config{'init_dir'}/$_[0]" ||
-r "/etc/init/$_[0].conf")) {
# Create upstart action if missing, as long as this isn't an old-style
# init script
my $cfile = "/etc/init/$_[0].conf";
if (-r $cfile) {
# Config file exists, make sure it is enabled
if (&has_command("insserv")) {
&system_logged(
"insserv ".quotemeta($_[0])." >/dev/null 2>&1");
}
my $lref = &read_file_lines($cfile);
my $foundstart;
foreach my $l (@$lref) {
if ($l =~ /^#+start/) {
# Start of start block
$l =~ s/^#+//;
$foundstart = 1;
}
elsif ($l =~ /^#+\s+\S/ && $foundstart) {
# Continuation line for start
$l =~ s/^#+//;
}
elsif ($l =~ /^\S/ && $foundstart) {
# Some other directive after start
last;
}
}
&flush_file_lines($cfile);
}
else {
# Need to create config
$_[2] || &error("Upstart service $_[0] cannot be created ".
"unless a command is given");
&create_upstart_service($_[0], $_[1], $_[2], undef,
$_[5]->{'fork'});
if (&has_command("insserv")) {
&system_logged(
"insserv ".quotemeta($_[0])." >/dev/null 2>&1");
}
}
return;
}
if ($init_mode eq "systemd" && (!-r "$config{'init_dir'}/$_[0]" ||
&is_systemd_service($unit))) {
# Create systemd unit if missing, as long as this isn't an old-style
# init script
my $cfile = &get_systemd_root($_[0])."/".$unit;
if (!-r $cfile) {
# Need to create config
$_[2] || &error("Systemd service $_[0] cannot be created ".
"unless a command is given");
&create_systemd_service($unit, $_[1], $_[2], $_[3], undef,
$_[5]->{'fork'}, $_[5]->{'pidfile'},
$_[5]->{'exit'});
}
&system_logged("systemctl unmask ".
quotemeta($unit)." >/dev/null 2>&1");
&system_logged("systemctl enable ".
quotemeta($unit)." >/dev/null 2>&1");
return;
}
if ($init_mode eq "init" || $init_mode eq "local" || $init_mode eq "upstart" ||
$init_mode eq "systemd") {
# In these modes, we create a script to run
if ($config{'daemons_dir'} &&
&read_env_file("$config{'daemons_dir'}/$_[0]", \%daemon)) {
$daemon++;
}
local $fn;
if ($init_mode eq "init" || $init_mode eq "upstart" ||
$init_mode eq "systemd") {
# Normal init.d system
$fn = &action_filename($_[0]);
}
else {
# Need to create hack init script
$fn = "$module_config_directory/$_[0].sh";
}
local @chk = &chkconfig_info($fn);
local @start = @{$chk[0]} ? @{$chk[0]} : &get_start_runlevels();
local $start_order = $chk[1] || "9" x $config{'order_digits'};
local $stop_order = $chk[2] || "9" x $config{'order_digits'};
local @stop;
if (@chk) {
local %starting = map { $_, 1 } @start;
@stop = grep { !$starting{$_} && /^\d+$/ } &list_runlevels();
}
local $need_links = 0;
if ($st == 1 && $daemon) {
# Just update daemons file
$daemon{'ONBOOT'} = 'yes';
&lock_file("$config{'daemons_dir'}/$_[0]");
&write_env_file("$config{'daemons_dir'}/$_[0]", \%daemon);
&unlock_file("$config{'daemons_dir'}/$_[0]");
}
elsif ($st == 1) {
# Just need to create links (later)
$need_links++;
}
elsif ($_[1]) {
# Need to create the init script
&lock_file($fn);
&open_tempfile(ACTION, ">$fn");
&print_tempfile(ACTION, "#!/bin/sh\n");
if ($config{'chkconfig'}) {
# Redhat-style description: and chkconfig: lines
&print_tempfile(ACTION, "# description: $_[1]\n");
&print_tempfile(ACTION, "# chkconfig: $config{'chkconfig'} ",
"$start_order $stop_order\n");
}
elsif ($config{'init_info'}) {
# Suse-style init info section
&print_tempfile(ACTION, "### BEGIN INIT INFO\n",
"# Provides: $_[0]\n",
"# Required-Start: \$network \$syslog\n",
"# Required-Stop: \$network\n",
"# Default-Start: ",join(" ", @start),"\n",
"# Default-Stop:\n",
"# Description: $_[1]\n",
"### END INIT INFO\n");
}
else {
&print_tempfile(ACTION, "# $_[1]\n");
}
&print_tempfile(ACTION, "\n");
&print_tempfile(ACTION, "case \"\$1\" in\n");
if ($_[2]) {
&print_tempfile(ACTION, "'start')\n");
&print_tempfile(ACTION, &tab_indent($_[2]));
&print_tempfile(ACTION, "\tRETVAL=\$?\n");
if ($config{'subsys'}) {
&print_tempfile(ACTION, "\tif [ \"\$RETVAL\" = \"0\" ]; then\n");
&print_tempfile(ACTION, "\t\ttouch $config{'subsys'}/$_[0]\n");
&print_tempfile(ACTION, "\tfi\n");
}
&print_tempfile(ACTION, "\t;;\n");
}
if ($_[3]) {
&print_tempfile(ACTION, "'stop')\n");
&print_tempfile(ACTION, &tab_indent($_[3]));
&print_tempfile(ACTION, "\tRETVAL=\$?\n");
if ($config{'subsys'}) {
&print_tempfile(ACTION, "\tif [ \"\$RETVAL\" = \"0\" ]; then\n");
&print_tempfile(ACTION, "\t\trm -f $config{'subsys'}/$_[0]\n");
&print_tempfile(ACTION, "\tfi\n");
}
&print_tempfile(ACTION, "\t;;\n");
}
if ($_[4]) {
&print_tempfile(ACTION, "'status')\n");
&print_tempfile(ACTION, &tab_indent($_[4]));
&print_tempfile(ACTION, "\t;;\n");
}
if ($_[2] && $_[3]) {
&print_tempfile(ACTION, "'restart')\n");
&print_tempfile(ACTION, "\t\$0 stop ; \$0 start\n");
&print_tempfile(ACTION, "\tRETVAL=\$?\n");
&print_tempfile(ACTION, "\t;;\n");
}
&print_tempfile(ACTION, "*)\n");
&print_tempfile(ACTION, "\techo \"Usage: \$0 { start | stop }\"\n");
&print_tempfile(ACTION, "\tRETVAL=1\n");
&print_tempfile(ACTION, "\t;;\n");
&print_tempfile(ACTION, "esac\n");
&print_tempfile(ACTION, "exit \$RETVAL\n");
&close_tempfile(ACTION);
chmod(0755, $fn);
&unlock_file($fn);
$need_links++;
}
if ($need_links && ($init_mode eq "init" ||
$init_mode eq "upstart" ||
$init_mode eq "systemd")) {
local $data = &read_file_contents($fn);
my $done = 0;
if (&has_command("chkconfig") && !$config{'no_chkconfig'} &&
(@chk && $chk[3] || $data =~ /Default-Start:/i)) {
# Call the chkconfig command to link up
&system_logged("chkconfig --add ".quotemeta($_[0]));
my $ex = &system_logged(
"chkconfig ".quotemeta($_[0])." on");
if (!$ex) {
$done = 1;
}
}
elsif (&has_command("insserv") && !$config{'no_chkconfig'} &&
$data =~ /Default-Start:/i) {
# Call the insserv command to enable
my $ex = &system_logged("insserv ".quotemeta($_[0]).
" >/dev/null 2>&1");
$done = 1 if (!$ex && &action_status($_[0]) == 2);
}
if (!$done) {
# Just link up the init script
local $s;
foreach $s (@start) {
&add_rl_action($_[0], $s, "S", $start_order);
}
local @klevels = &action_levels("K", $_[0]);
if (!@klevels) {
# Only add K scripts if none exist
foreach $s (@stop) {
&add_rl_action($_[0], $s, "K", $stop_order);
}
}
}
}
elsif ($need_links) {
# Just add rc.local entry
local $lref = &read_file_lines($config{'local_script'});
local $i;
for($i=0; $i<@$lref && $lref->[$i] !~ /^exit\s/; $i++) { }
splice(@$lref, $i, 0, "$fn start");
if ($config{'local_down'}) {
# Also add to shutdown script
$lref = &read_file_lines($config{'local_down'});
for($i=0; $i<@$lref &&
$lref->[$i] !~ /^exit\s/; $i++) { }
splice(@$lref, $i, 0, "$fn stop");
}
&flush_file_lines();
}
}
elsif ($init_mode eq "win32") {
# Enable and/or create a win32 service
if ($st == 1) {
# Just enable
&enable_win32_service($_[0]);
}
else {
# Need to create service, which calls wrapper program
eval "use Win32::Daemon";
# modify the string handed over
# so it does not contain backslashes ...
$_[2] =~ s/\\/\//g;
local $perl_path = &get_perl_path();
local %svc = ( 'name' => $_[0],
'display' => $_[1],
'path' => $perl_path,
'user' => '',
'description' => "OCM Webmin Pro Service",
'pwd' => $module_root_directory,
'parameters' => "\"$module_root_directory/win32.pl\" $_[2]",
);
if (!Win32::Daemon::CreateService(\%svc)) {
print STDERR "Failed to create Win32 service : ",
Win32::FormatMessage(Win32::Daemon::GetLastError()),"\n";
}
}
}
elsif ($init_mode eq "rc") {
# Enable and/or create an RC script
&lock_rc_files();
if ($st == 1) {
# Just enable
&enable_rc_script($_[0]);
}
else {
# Need to create a local rc script, and enable
local @dirs = split(/\s+/, $config{'rc_dir'});
local $file = $dirs[$#dirs]."/".$_[0].".sh";
local $name = $_[0];
$name =~ s/-/_/g;
&open_lock_tempfile(SCRIPT, ">$file");
&print_tempfile(SCRIPT, "#!/bin/sh\n");
&print_tempfile(SCRIPT, "#\n");
&print_tempfile(SCRIPT, "# PROVIDE: $_[0]\n");
&print_tempfile(SCRIPT, "# REQUIRE: LOGIN\n");
&print_tempfile(SCRIPT, "\n");
&print_tempfile(SCRIPT, ". /etc/rc.subr\n");
&print_tempfile(SCRIPT, "\n");
&print_tempfile(SCRIPT, "name=$name\n");
&print_tempfile(SCRIPT, "rcvar=`set_rcvar`\n");
&print_tempfile(SCRIPT, "start_cmd=\"$_[2]\"\n");
if ($_[3]) {
&print_tempfile(SCRIPT, "stop_cmd=\"$_[3]\"\n")
}
if ($_[4] && $_[4] !~ /\n/) {
&print_tempfile(SCRIPT, "status_cmd=\"$_[4]\"\n")
}
&print_tempfile(SCRIPT, "\n");
&print_tempfile(SCRIPT, "load_rc_config \${name}\n");
&print_tempfile(SCRIPT, "run_rc_command \"\$1\"\n");
&close_tempfile(SCRIPT);
&set_ownership_permissions(undef, undef, 0755, $file);
&enable_rc_script($_[0]);
}
&unlock_rc_files();
}
elsif ($init_mode eq "osx") {
# Add hostconfig file entry
local $ucname = uc($_[0]);
local %hc;
&lock_file($config{'hostconfig'});
&read_env_file($config{'hostconfig'}, \%hc);
if (!$hc{$ucname}) {
# Need to create action
local $ucfirst = ucfirst($_[0]);
local $dir = "$config{'darwin_setup'}/$ucfirst";
local $paramlist = "$dir/$config{'plist'}";
local $scriptfile = "$dir/$ucfirst";
# Create dirs if missing
if (!-d $config{'darwin_setup'}) {
&make_dir($config{'darwin_setup'}, 0755);
}
if (!-d $dir) {
&make_dir($dir, 0755);
}
# Make params list file
&open_lock_tempfile(PLIST, ">$paramlist");
&print_tempfile(PLIST, "{\n");
&print_tempfile(PLIST, "\t\tDescription\t\t= \"$_[1]\";\n");
&print_tempfile(PLIST, "\t\tProvides\t\t= (\"$ucfirst\");\n");
&print_tempfile(PLIST, "\t\tRequires\t\t= (\"Resolver\");\n");
&print_tempfile(PLIST, "\t\tOrderPreference\t\t= \"None\";\n");
&print_tempfile(PLIST, "\t\tMessages =\n");
&print_tempfile(PLIST, "\t\t{\n");
&print_tempfile(PLIST, "\t\t\tstart\t= \"Starting $ucfirst\";\n");
&print_tempfile(PLIST, "\t\t\tstop\t= \"Stopping $ucfirst\";\n");
&print_tempfile(PLIST, "\t\t};\n");
&print_tempfile(PLIST, "}\n");
&close_tempfile(PLIST);
# Create Bootup Script
&open_lock_tempfile(STARTUP, ">$scriptfile");
&print_tempfile(STARTUP, "#!/bin/sh\n\n");
&print_tempfile(STARTUP, ". /etc/rc.common\n\n");
&print_tempfile(STARTUP, "if [ \"\${$ucname:=-NO-}\" = \"-YES-\" ]; then\n");
&print_tempfile(STARTUP, "\tConsoleMessage \"Starting $ucfirst\"\n");
&print_tempfile(STARTUP, "\t$_[2]\n");
&print_tempfile(STARTUP, "fi\n");
&close_tempfile(STARTUP);
&set_ownership_permissions(undef, undef, 0750, $scriptfile);
}
# Update hostconfig file
$hc{$ucname} = '-YES-';
&write_env_file($config{'hostconfig'}, \%hc);
&unlock_file($config{'hostconfig'});
}
elsif ($init_mode eq "launchd") {
# Create and if necessary enable a launchd agent
my $name = &launchd_name($_[0]);
my @agents = &list_launchd_agents();
my ($agent) = grep { $_->{'name'} eq $name } @agents;
if (!$agent) {
# Need to create script
&create_launchd_agent($name, $_[1], 1);
}