File: chkconfig

package info (click to toggle)
chkconfig 11.4-54.60.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 140 kB
  • sloc: perl: 603; sh: 269; makefile: 7
file content (745 lines) | stat: -rwxr-xr-x 17,124 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use strict;
use Getopt::Long;
use File::Temp 'tempfile';

my $initdir = '/etc/init.d';
my $inetddir = '/etc/inetd.d';
my $xinetddir = '/etc/xinetd.d';

my %to_d = (
  '0' => 'rc0.d', '1' => 'rc1.d', '2' => 'rc2.d', '3' => 'rc3.d',
  '4' => 'rc4.d', '5' => 'rc5.d', 'S' => 'rcS.d', 'B' => 'boot.d'
);

# which files to skip in $initdir
my %skips_rc = map {$_ => 1} qw {rc rx skeleton powerfail boot halt reboot single boot.local halt.local};

# which services are known
my %known_rc = ();
my %known_inetd = ();
my %known_xinetd = ();
my %known_all = ();

#
# get the contents of a directory
#
sub ls {
  my $dir = shift;

  local *D;
  return () unless opendir(D, $dir);
  my @ret = grep {$_ ne '.' && $_ ne '..'} readdir(D);
  closedir D;
  return @ret;
}

#
# unify an array
#
sub unify {
  my %h = map {$_ => 1} @_;
  return grep {delete $h{$_}} @_;
}


##################################################################
#                         runlevel part
##################################################################

# which services are currently on? this is a cache to speed things up
# initialized by initlinks_rc(), used in getreal_rc()
my %links = ();
my %links_unknown = ();

#
#
# calculate the default runlevels of a service by reading the
# insserv header. regexes taken from insserv.c
#
my %getdef_rc_cache = ();

sub getdef_rc {
  my $s = shift;

  return $getdef_rc_cache{$s} if exists $getdef_rc_cache{$s};
  my $file = "$initdir/$s";
  local *F;
  if (!open(F, "<$file")) {
    print STDERR "$file: $!\n";
    $getdef_rc_cache{$s} = undef;
    return undef;
  }
  while (<F>) {
    chomp;
    if (/^#[[:blank:]]*default[-_]?start:[[:blank:]]*([[:print:][:blank:]]*)/i) {
      my $ret = $1;
      close F;
      $ret =~ s/[[:blank:]]+//g;
      my @ret = split('', $ret);
      $ret = '';
      for (sort @ret) {
	$_ = uc($_);
	$ret .= $_ if /[0123456SB]/;
      }
      $getdef_rc_cache{$s} = $ret;
      return $ret;
    }
  }
  $getdef_rc_cache{$s} = '35';
  return '35';
}

#
# calculate the required services by reading the insserv header.
# regexes taken from insserv.c
#
sub getdeps_rc {
  my $s = shift;

  my $file = "$initdir/$s";
  local *F;
  open(F, "<$file") || return undef;
  while (<F>) {
    chomp;
    if (/^#[[:blank:]]*required[-_]?start:[[:blank:]]*([[:print:][:blank:]]*)/i) {
      my $ret = $1;
      close F;
      $ret =~ s/\s+$//;
      return $ret;
    }
  }
  return '';
}

#
# calculate the active runlevels of a service. Uses global %links
# hash.
#
sub getreal_rc {
  my $s = shift;

  my $start = '';
  my $l;
  initlinks_rc() if $links_unknown{$s};
  for $l (sort keys %links) {
    $start .= $l if $links{$l}->{$s};
  }
  return $start;
}

#
# initializes global %links hash by scanning the link directories
# for each runlevel.
#
sub initlinks_rc {
  my $l;
  for $l (keys %to_d) {
    my @links = grep {s/^S\d\d//} ls("$initdir/$to_d{$l}");
    $links{$l} = { map {$_ => 1} @links };
  }
  %links_unknown = ();
}

my $force;
my $allservices;

#
# run insserv
#
sub insserv {
  my @i = ("/sbin/insserv");
  push @i, "-f" if $force;
  my $r = system(@i, @_);
  if ($r == -1) {
    printf STDERR "/sbin/insserv: $!\n";
    return undef;
  } elsif ($r) {
    printf STDERR "/sbin/insserv failed, exit code %d\n", $? >> 8;
    return undef;
  }
  return 1;
}


##################################################################
#                         xinetd part
##################################################################

#
# get the state of a xinetd service
#
sub getreal_xinetd {
  my $s = shift;

  my $file = "$xinetddir/$s";
  local *F;
  open(F, "<$file") || return undef;
  my $dis = 1;
  while (<F>) {
    if (/^\s*service\s*\S/) {
      if (!$dis) {
	close F;
	return 'X';
      }
      $dis = 0;
    }
    if (/^\s*disable\s*=\s*yes/) {
      $dis = 1;
      next;
    }
  }
  close F;
  return $dis ? '' : 'X';
}

#
# change the state of a xinetd service
#
sub set_xinetd {
  my $s = shift;
  my $state = shift;

  if (!$known_xinetd{$s}) {
    print STDERR "$s: not a xinetd service\n";
    return;
  }
  local *F;
  local *N;
  my $file = "$xinetddir/$s";
  if (!open(F, "<$file")) {
    print STDERR "$file: $!\n";
    return;
  }
  if (!open(N, ">$file.chkconfig~")) {
    print STDERR "$file.chkconfig~: $!\n";
    return;
  }
  while (<F>) {
    if (/^\s*service\s*\S/) {
      if (!/{/) {	#}
        print N $_;
	$_ = <F>;
      }
      print N $_;
      print N "\tdisable     = yes\n" unless $state;
      next;
    }
    print N $_ unless /^\s*disable\s*=\s*yes/;
  }
  close F;
  if (!close N) {
    print STDERR "$file.chkconfig~: $!\n";
    unlink("$file.chkconfig~");
    return;
  }
  if (!rename("$file.chkconfig~", "$file")) {
    print STDERR "rename $file.chkconfig~ $file: $!\n";
    unlink("$file.chkconfig~");
    return;
  }
  return 1;
}


##################################################################
#                         inetd part
##################################################################

#
# get the state of a inetd service
#
sub getreal_inetd {
  my $s = shift;

  my $file = "$inetddir/$s";
  local *F;
  open(F, "<$file") || return undef;
  while (<F>) {
    chomp;
    next if /^\s*#/;
    next if /^\s*$/;
    close F;
    return 'T';
  }
  close F;
  return '';
}

#
# does the line look like a inetd service?
#
sub looks_ok_inetd {
  return 1 if $_[0] =~ /^![\|<]/;
  my @x = split(' ', $_[0]);
  my %oktype = map {$_ => 1} qw{stream dgram raw rdm seqpacket};
  return 0 unless $oktype{$x[1]};
  return 0 unless $x[3] =~ /^(no)?wait/;
  return 1;
}

#
# change the state of a inetd service
#
sub set_inetd {
  my $s = shift;
  my $state = shift;

  if (!$known_inetd{$s}) {
    print STDERR "$s: not an inetd service\n";
    return;
  }
  local *F;
  local *N;
  my $file = "$inetddir/$s";
  if (!open(F, "<$file")) {
    print STDERR "$file: $!\n";
    return;
  }
  if (!open(N, ">$file.chkconfig~")) {
    print STDERR "$file.chkconfig~: $!\n";
    return;
  }
  while (<F>) {
    chomp;
    if (/^#\s*(.*)/) {
      my $l = $1;
      if (looks_ok_inetd($l)) {
        print N $state ? "$l\n" : "## $l\n";
        next;
      }
    }
    if (!$state && looks_ok_inetd($_)) {
      print N "# $_\n";
      next;
    }
    print N "$_\n";
  }
  if (!close N) {
    print STDERR "$file.chkconfig~: $!\n";
    unlink("$file.chkconfig~");
    return;
  }
  if (!rename("$file.chkconfig~", "$file")) {
    print STDERR "rename $file.chkconfig~ $file: $!\n";
    unlink("$file.chkconfig~");
    return;
  }
  return 1;
}


##################################################################
#                     common functions
##################################################################

#
# calculate current status
#
sub getcurrent {
  my $s = shift;

  if (!$known_all{$s}) {
    print STDERR "$s: unknown service\n";
    return undef;
  }
  my $start = '';
  $start .= getreal_rc($s) if $known_rc{$s};
  $start .= getreal_inetd($s) if $known_inetd{$s};
  $start .= getreal_xinetd($s) if $known_xinetd{$s};
  return $start;
}


#
# return all services we know about by scanning $initdir for init
# scripts.
#
sub findknown {
  for (ls($initdir)) {
    next unless -f "$initdir/$_";
    next if /^README/ || /^core/;
    next if /~$/ || /^[\d\$\.#_\-\\\*]/ || /\.(rpm|ba|old|new|save|swp|core)/;
    $known_rc{$_} = 1;
    $known_all{$_} = 1;
  }
  for (ls($xinetddir)) {
    next unless -f "$xinetddir/$_";
    next if /~$/ || /\./;
    $known_xinetd{$_} = 1;
    $known_all{$_} = 1;
  }
  return unless -d $inetddir;
  return unless -f "/etc/inetd.conf";
  local *F;
  my $gotinetd = 0;
  if (!open(F, "</etc/inetd.conf")) {
    print STDERR "/etc/inetd.conf: $!\n";
    return;
  }
  while (<F>) {
    chomp;
    if (/^!\|\s*\/usr\/lib\/inetd\/includedir\s+\Q$inetddir\E\s*$/) {
      $gotinetd = 1;
      last;
    }
  }
  close F;
  return unless $gotinetd;
  for (ls($inetddir)) {
    next unless -f "$inetddir/$_";
    next if /~$/ || /\./;
    $known_inetd{$_} = 1;
    $known_all{$_} = 1;
  }
}

#
# normalize runlevel
#
my $level;	# overwrite on with $level

sub normalize {
  my $s = shift;
  my $rl = shift;

  $rl = lc($rl);
  return '' if $rl eq 'off' || $rl eq '';
  my $def = '35';
  $def = 'inetd' if $known_inetd{$s};
  $def = 'xinetd' if $known_xinetd{$s};
  $def = getdef_rc($s) if $known_rc{$s};
  return undef unless defined $def;
  $rl = ",$rl,";
  $rl =~ s/,on,/,$level,/g if defined $level;
  $rl =~ s/,on,/,$def,/g;
  $rl =~ s/,xinetd,/,X,/g;
  $rl =~ s/,inetd,/,T,/g;
  $rl =~ s/s/S/g;
  $rl =~ s/b/B/g;
  $rl =~ s/,//g;
  $rl = join('', sort unify(split('', $rl)));
  if ($rl =~ /([^0123456SBTX])/) {
    print STDERR "illegal runlevel specified for $s: $1\n";
    return undef;
  }
  return $rl;
}

#
# convert runlevels into a nice human readable form
#
sub readable {
  my $s = shift;
  my $rl = shift;

  return 'off' if $rl eq '';
  my $def = '';
  $def = getdef_rc($s) if $known_rc{$s};
  return undef unless defined $def;
  $rl = ",$rl,";
  $rl =~ s/T/,inetd,/g;
  $rl =~ s/X/,xinetd,/g;
  $rl =~ s/,\Q$def\E,/,on,/ if $def ne '';
  $rl =~ s/,,+/,/g;
  $rl =~ s/^,//;
  $rl =~ s/,$//;
  return $rl;
}


##################################################################
#                     main program
##################################################################

my $mode = '';
my $printdeps;
my $root = '/';

sub addmode {
  die("Please specify only one mode.\n") if $mode;
  $mode = substr($_[0], 0, 1);
}

sub usage {
  print <<EOF;
usage:
        chkconfig -A|--allservices              (together with -l: show all services)
        chkconfig -t|--terse [names]            (shows the links)
        chkconfig -e|--edit  [names]            (configure services)
        chkconfig -s|--set   [name state]...    (configure services)
        chkconfig -l|--list [--deps] [names]    (shows the links)
        chkconfig -c|--check name [state]       (check state)
        chkconfig -a|--add   [names]            (runs insserv)
        chkconfig -d|--del   [names]            (runs insserv -r)
        chkconfig -h|--help                     (print usage)
        chkconfig -f|--force ...                (call insserv with -f)

        chkconfig [name]             same as chkconfig -t
        chkconfig name state...      same as chkconfig -s name state
        chkconfig --root=<root> ...  use <root> as the root file system
EOF
}

Getopt::Long::Configure('no_ignore_case');

if (!GetOptions('list|l'   => \&addmode,
                'terse|t'  => \&addmode,
                'add|a'    => \&addmode,
                'del|d'    => \&addmode,
                'edit|e'   => \&addmode,
                'help|h'   => \&addmode,
                'set|s'    => \&addmode,
                'check|c'  => \&addmode,
                'level=s'  => \$level,
                'force|f'  => \$force,
                'allservices|A'  => \$allservices,
                'deps'     => \$printdeps,
                'root=s'   => \$root

   )) {
  usage();
  exit 1;
}
if ($mode eq 'h') {
  usage();
  exit 0;
}
my (@services, $s);
my (@remove, $s);
my (@enable, $s);

$initdir = "$root/etc/init.d";
$inetddir = "$root/etc/inetd.d";
$xinetddir = "$root/etc/xinetd.d";

findknown();

if (@ARGV) {
  @services = @ARGV;
  $mode = @services == 1 ? 't' : 's' if $mode eq '';
} else {
  die("Please specify a service\n") if $mode eq 'c' || $mode eq 'a' || $mode eq 'd';
  @services = sort grep {!$skips_rc{$_}} keys %known_all if $mode ne 's';
}
$mode = 't' if $mode eq '';

initlinks_rc() if $mode eq 'e' || $mode eq 't' || $mode eq 's' || $mode eq 'c' || $mode eq 'l';

if (!@ARGV && !$allservices) {
  my $l;
  my %ison;
  for $l (0, 1, 2, 3, 4, 5, 6) {
    $ison{$_} = 1 for keys %{$links{$l}};
  }
  @services = grep {!/^boot\./ || $ison{$_}} @services;
}

my %current = ();

if ($mode eq 'c') {
  die("Please specify only one service to check\n") if @services > 2;
  $s = $services[0];
  my $want;
  if (@services == 1) {
    $want = `/sbin/runlevel`;
    chomp($want);
    die("Can't determine current runlevel\n") unless $want =~ s/^. (.)$/$1/;
  } else {
    $want = $services[1];
  }
  $want = normalize($s, $want);
  exit 1 unless defined $want;
  exit 0 if $want eq '';
  my $l;
  for $l (split('', $want)) {
    if ($l eq 'T') {
      exit 1 unless getreal_inetd($s) ne '';
      next;
    }
    if ($l eq 'X') {
      exit 1 unless getreal_xinetd($s) ne '';
      next;
    }
    exit 1 unless $links{$l}->{$s};
  }
  exit 0;
}

if ($mode eq 'e' || $mode eq 't') {
  my ($fh, $tmpname);
  my $maxlen = 0;
  $maxlen >= length($_) or $maxlen = length($_) for @services;
  if ($mode eq 'e') {
    ($fh, $tmpname) = tempfile("chkconfig.XXXXX", DIR => '/tmp', UNLINK => 1);
    die("Could not create temporary file\n") unless $tmpname ne '';
  } else {
    $fh = *STDOUT;
  }
  for $s (@services) {
    $current{$s} = getcurrent($s);
    next unless defined $current{$s};
    my $r = readable($s, $current{$s});
    next unless defined $r;
    printf $fh "%-*s  %s\n", $maxlen, $s, $r;
  }
  exit 0 unless $mode eq 'e';
  close $fh;
  system("\${VISUAL:-vi} $tmpname");
  open(STDIN, "<$tmpname") or die("Could not open temporary file\n");
  $mode = 's';
  @services = ();
}

if ($mode eq 's') {
  my $status = 0;
  my $usestdin = !@services;
  my $ln = 0;
  do {
    if ($usestdin) {
      while (<STDIN>) {
	$ln++;
	chomp;
	next if /^\s*#/;
	next if /^\s*$/;
	my @line = split(' ', $_);
	if (@line != 2) {
	  print STDERR "parse error line $ln: $_\n";
	  $status = 1;
	  next;
	}
	@services = @line;
	last;
      }
      exit 1 unless @services;
    }
    if (@services & 1) {
      printf("Usage: chkconfig -s service on|off|runlevels\n");
      exit 1;
    }
    @remove = ();
    @enable = ();
    while (@services) {
      $s = shift @services;
      my $want = shift @services;
      $want = normalize($s, $want);
      $status = 1, next unless defined $want;
      $current{$s} = getcurrent($s) unless defined $current{$s};
      $status = 1, next unless defined $current{$s};
      my $current = $current{$s};
      next if $want eq $current;
      delete $current{$s};
      if (($want =~ /T/) && ($current !~ /T/)) {
        $status = 1 unless set_inetd($s, 1);
      } elsif (($want !~ /T/) && ($current =~ /T/)) {
        $status = 1 unless set_inetd($s, 0);
      }
      if (($want =~ /X/) && ($current !~ /X/)) {
        $status = 1 unless set_xinetd($s, 1);
      } elsif (($want !~ /X/) && ($current =~ /X/)) {
        $status = 1 unless set_xinetd($s, 0);
      }
      $want =~ s/[TX]//g;
      $current =~ s/[TX]//g;
      next if $want eq $current;

      if (!$known_rc{$s}) {
        print STDERR "$s: not a runlevel service\n";
	$status = 1;
        next;
      }
      if ($want eq '') {
        push @remove, $s;
      } elsif ($want eq getdef_rc($s)) {
        push @enable, $s;
      } else {
        push @remove, $s;
        push @enable, "$s,start=".join(',', split('', $want));
      }
      $links_unknown{$s} = 1;	# check again for this service
    }
    if (scalar(@remove)) {
      $status = 1 unless insserv('-r', '-d', '-p', "$initdir", @remove);
    }
    if (scalar(@enable)) {
      $status = 1 unless insserv('-p', "$initdir", @enable);
    }
  } while ($usestdin);
  exit $status;
}

#
# compatibility section
#
my $status = 0;
if ($mode eq 'a' || $mode eq 'd') {
  for $s (splice @services) {
    if (!$known_all{$s}) {
      print STDERR "$s: unknown service\n";
      $status = 1;
      next;
    }
    if (!$known_rc{$s}) {
      print STDERR "$s: not a runlevel service\n";
      $status = 1;
      next;
    }
    push @services, $s;
  }
  if (!$status) {
    if ($mode eq 'a') {
      insserv('-p', "$initdir", @services) or $status = 1;
    } else {
      insserv('-p', "$initdir", '-r', @services) or $status = 1;
    }
  }
  $mode = 'l';
  initlinks_rc();
}
if ($mode eq 'l') {
  my $usecolor = -t STDOUT;
  for $s (@services) {
    if (!$known_rc{$s}) {
      print STDERR "$s: unknown service\n" unless $known_all{$s};
      next;
    }
    printf "%-24s", $s;
    my $l;
    for $l (0, 1, 2, 3, 4, 5, 6, 'B', 'S') {
      next if ($l eq 'B' || $l eq 'S') && !$links{$l}->{$s};
      if ($usecolor) {
	print $links{$l}->{$s} ? "  \e[0;1;32m$l:on\e[m " : "  $l:off";
      } else {
	print $links{$l}->{$s} ? "  $l:on " : "  $l:off";
      }
    }
    print getdeps_rc($s) if $printdeps;
    print "\n";
  }
  my @inetd_services = grep {$known_inetd{$_}} @services;
  if (@inetd_services) {
    print "inetd based services:\n";
    for $s (@inetd_services) {
      printf "        %-19s ", "$s:";
      if (getreal_inetd($s) ne '') {
	print $usecolor ? "\e[0;1;32mon\e[m\n" : "on\n";
      } else {
	print "off\n";
      }
    }
  }
  my @xinetd_services = grep {$known_xinetd{$_}} @services;
  if (@xinetd_services) {
    print "xinetd based services:\n";
    for $s (@xinetd_services) {
      printf "        %-19s ", "$s:";
      if (getreal_xinetd($s) ne '') {
	print $usecolor ? "\e[0;1;32mon\e[m\n" : "on\n";
      } else {
	print "off\n";
      }
    }
  }
  exit($status);
}