~ubuntu-branches/ubuntu/karmic/system-tools-backends/karmic-proposed

« back to all changes in this revision

Viewing changes to Init/Services.pm

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2009-10-08 11:42:26 UTC
  • mfrom: (1.1.28 upstream) (1.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20091008114226-zp9fxn7h4ky0qai0
Tags: 2.8.2-1
* New upstream release.
  - debian/patches/01_debian_4.0.patch,
  - debian/patches/04_correct_perl_command.patch:
    + Removed, fixed upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#
7
7
# Authors: Carlos Garnacho Parro <garparr@teleline.es>,
8
8
#          Hans Petter Jansson <hpj@ximian.com>,
9
 
#          Arturo Espinosa <arturo@ximian.com>
 
9
#          Arturo Espinosa <arturo@ximian.com>,
 
10
#          Milan Bouchet-Valat <nalimilan@club.fr>
10
11
#
11
12
# This program is free software; you can redistribute it and/or modify
12
13
# it under the terms of the GNU Library General Public License as published
45
46
     "redhat-8.0"       => "redhat-6.2",
46
47
     "mandrake-9.0"     => "redhat-6.2",
47
48
     "conectiva-9"      => "redhat-6.2",
48
 
     "debian-3.0"       => "redhat-6.2",
49
 
     "ubuntu-7.04"      => "redhat-6.2",
 
49
     "debian"           => "redhat-6.2",
50
50
     "suse-9.0"         => "redhat-6.2",
51
51
     "pld-1.0"          => "redhat-6.2",
52
52
     "vine-3.0"         => "redhat-6.2",
88
88
  return &get_sysv_default_runlevel ();
89
89
}
90
90
 
 
91
# Upstart support
 
92
# TODO: Handle Upstart jobs, and not only traditional SystemV scripts inside an upstart system
 
93
sub get_upstart_paths
 
94
{
 
95
  my %dist_map =
 
96
    (
 
97
     # gst_dist => [rc.X dirs location, init.d scripts location, relative path, upstart init jobs location]
 
98
     "debian"   => ["$gst_prefix/etc",   "$gst_prefix/etc/init.d",   "../init.d",   "$gst_prefix/etc/init"],
 
99
     );
 
100
  my $res;
 
101
 
 
102
  $res = $dist_map{$Utils::Backend::tool{"platform"}};
 
103
  &Utils::Report::do_report ("service_upstart_unsupported", $Utils::Backend::tool{"platform"}) if ($res eq undef);
 
104
  return @$res;
 
105
}
 
106
 
 
107
# we are going to extract the name of the script
 
108
sub get_upstart_service_name
 
109
{
 
110
        my ($service) = @_;
 
111
 
 
112
        $service =~ s/$initd_path\///;
 
113
 
 
114
        return $service;
 
115
}
 
116
 
 
117
# This function gets the state of the service along the runlevels,
 
118
# it also returns the average priority
 
119
sub get_upstart_runlevels_status
 
120
{
 
121
        my ($service) = @_;
 
122
        my ($link);
 
123
        my ($runlevel, $action, $priority);
 
124
        my (@arr, @ret);
 
125
 
 
126
        foreach $link (<$rcd_path/rc[0-6].d/[SK][0-9][0-9]$service>)
 
127
        {
 
128
                $link =~ s/$rcd_path\///;
 
129
                $link =~ /rc([0-6])\.d\/([SK])([0-9][0-9]).*/;
 
130
                ($runlevel,$action,$priority)=($1,$2,$3);
 
131
 
 
132
                if ($action eq "S")
 
133
                {
 
134
                        push @arr, [ $runlevel, $SERVICE_START, $priority ];
 
135
                }
 
136
                elsif ($action eq "K")
 
137
                {
 
138
                        push @arr, [ $runlevel, $SERVICE_STOP, $priority ];
 
139
                }
 
140
        }
 
141
 
 
142
        return \@arr;
 
143
}
 
144
 
 
145
# We are going to extract the information of the service
 
146
sub get_upstart_service_info
 
147
{
 
148
        my ($service) = @_;
 
149
        my ($script, @actions, @runlevels, $role);
 
150
 
 
151
        # Return if it's a directory
 
152
        return if (-d $service);
 
153
 
 
154
        # We have to check if the service is executable
 
155
        return unless (-x $service);
 
156
 
 
157
        $script = &get_upstart_service_name ($service);
 
158
        $runlevels = &get_upstart_runlevels_status($script);
 
159
 
 
160
  return ($script, $runlevels);
 
161
}
 
162
 
 
163
# This function gets an ordered array of the available services from a upstart system
 
164
sub get_upstart_services
 
165
{
 
166
        my ($service);
 
167
        my (@arr);
 
168
 
 
169
        ($rcd_path, $initd_path, $relative_path, $init_path) = &get_upstart_paths ();
 
170
        return undef unless ($rcd_path && $initd_path && $init_path);
 
171
 
 
172
        foreach $service (<$initd_path/*>)
 
173
        {
 
174
                my (@info, $script);
 
175
 
 
176
                @info = &get_upstart_service_info ($service);
 
177
                # Only manage traditional init.d scripts, ignore services with jobs installed
 
178
                $script = $info[0];
 
179
                if (!-e "$init_path/$script.conf")
 
180
                {
 
181
                        push @arr, \@info  if (scalar (@info));
 
182
                }
 
183
        }
 
184
 
 
185
        return \@arr;
 
186
}
 
187
 
 
188
# These are the functions for storing the service settings in upstart
 
189
sub remove_upstart_link
 
190
{
 
191
  my ($rcd_path, $runlevel, $script) = @_;
 
192
 
 
193
  foreach $link (<$rcd_path/rc$runlevel.d/[SK][0-9][0-9]$script>)
 
194
  {
 
195
    &Utils::Report::enter ();
 
196
    &Utils::Report::do_report ("service_upstart_remove_link", "$link");
 
197
    unlink ($link);
 
198
    &Utils::Report::leave ();
 
199
  }
 
200
}
 
201
 
 
202
sub add_upstart_link
 
203
{
 
204
  my ($rcd_path, $relative_path, $runlevel, $action, $priority, $service) = @_;
 
205
  my ($prio) = sprintf ("%0.2d",$priority);
 
206
 
 
207
  symlink ("$relative_path/$service", "$rcd_path/rc$runlevel.d/$action$prio$service");
 
208
 
 
209
  &Utils::Report::enter ();
 
210
  &Utils::Report::do_report ("service_upstart_add_link", "$rcd_path/rc$runlevel.d/$action$prio$service");
 
211
  &Utils::Report::leave ();
 
212
}
 
213
 
 
214
sub run_upstart_initd_script
 
215
{
 
216
  my ($service, $arg) = @_;
 
217
  my ($rc_path, $initd_path);
 
218
  my $str;
 
219
 
 
220
  &Utils::Report::enter ();
 
221
 
 
222
  if (&Utils::File::run ("service", $service, $arg) == 0)
 
223
  {
 
224
      &Utils::Report::do_report ("service_upstart_op_success", $service, $arg);
 
225
      &Utils::Report::leave ();
 
226
      return 0;
 
227
  }
 
228
 
 
229
  &Utils::Report::do_report ("service_upstart_op_failed", $service, $arg);
 
230
  &Utils::Report::leave ();
 
231
  return -1;
 
232
}
 
233
 
 
234
sub set_upstart_service
 
235
{
 
236
  my ($service) = @_;
 
237
  my ($script, $priority, $runlevels, $default_runlevel);
 
238
  my ($runlevel, $action, %configured_runlevels);
 
239
 
 
240
  ($rcd_path, $initd_path, $relative_path) = &get_upstart_paths ();
 
241
  return unless ($rcd_path && $initd_path && $relative_path);
 
242
 
 
243
  $script = $$service[0];
 
244
  $runlevels = $$service[1];
 
245
  $default_runlevel = &get_default_runlevel ();
 
246
 
 
247
  foreach $r (@$runlevels)
 
248
  {
 
249
    $runlevel = $$r[0];
 
250
    $action   = ($$r[1] == $SERVICE_START) ? "S" : "K";
 
251
    $priority = sprintf ("%0.2d", $$r[2]);
 
252
    $priority = "50" if ($$r[2] <= 0);
 
253
 
 
254
    $configured_runlevels{$runlevel} = 1;
 
255
 
 
256
    if (!-f "$rcd_path/rc$runlevel.d/$action$priority$script")
 
257
    {
 
258
      &remove_upstart_link ($rcd_path, $runlevel, $script);
 
259
      &add_upstart_link ($rcd_path, $relative_path, $runlevel, $action, $priority, $script);
 
260
 
 
261
      if ($runlevel eq $default_runlevel)
 
262
      {
 
263
        &run_upstart_initd_script ($script, ($$r[1] == $SERVICE_START) ? "start" : "stop");
 
264
      }
 
265
    }
 
266
  }
 
267
 
 
268
  # remove unneeded links
 
269
  foreach $link (<$rcd_path/rc[0-6].d/[SK][0-9][0-9]$script>)
 
270
        {
 
271
    $link =~ /rc([0-6])\.d/;
 
272
    $runlevel = $1;
 
273
 
 
274
    if (!exists $configured_runlevels{$runlevel})
 
275
    {
 
276
      &remove_upstart_link ($rcd_path, $runlevel, $script);
 
277
 
 
278
      if ($runlevel eq $default_runlevel)
 
279
      {
 
280
        &run_upstart_initd_script ($script, "stop");
 
281
      }
 
282
    }
 
283
  }
 
284
}
 
285
 
 
286
sub set_upstart_services
 
287
{
 
288
        my ($services) = @_;
 
289
 
 
290
        foreach $i (@$services)
 
291
        {
 
292
                &set_upstart_service($i);
 
293
        }
 
294
}
 
295
 
 
296
# This function gets the runlevel that is in use
 
297
sub get_sysv_default_runlevel
 
298
{
 
299
        my (@arr);
 
300
        @arr = split / /, `/sbin/runlevel` ;
 
301
  chomp $arr[1];
 
302
 
 
303
        return $arr[1];
 
304
}
 
305
 
 
306
sub get_default_runlevel
 
307
{
 
308
  my $type = &get_init_type ();
 
309
 
 
310
  return "default" if ($type eq "gentoo" || $type eq "rcng" || $type eq "bsd" || $type eq "smf");
 
311
  return &get_sysv_default_runlevel ();
 
312
}
 
313
 
91
314
sub get_sysv_paths
92
315
{
93
316
  my %dist_map =
102
325
     "mandrake-9.0"   => ["$gst_prefix/etc/rc.d", "$gst_prefix/etc/rc.d/init.d", "../init.d"],
103
326
     "yoper-2.2"      => ["$gst_prefix/etc/rc.d", "$gst_prefix/etc/rc.d/init.d", "../init.d"],
104
327
     "conectiva-9"    => ["$gst_prefix/etc/rc.d", "$gst_prefix/etc/rc.d/init.d", "../init.d"],
105
 
     "debian-3.0"     => ["$gst_prefix/etc",      "$gst_prefix/etc/init.d",      "../init.d"],
106
 
     "ubuntu-7.04"    => ["$gst_prefix/etc",      "$gst_prefix/etc/init.d",      "../init.d"],
 
328
     "debian"         => ["$gst_prefix/etc",      "$gst_prefix/etc/init.d",      "../init.d"],
107
329
     "suse-9.0"       => ["$gst_prefix/etc/init.d", "$gst_prefix/etc/init.d",    "../"],
108
330
     "pld-1.0"        => ["$gst_prefix/etc/rc.d", "$gst_prefix/etc/rc.d/init.d", "../init.d"],
109
331
     "vine-3.0"       => ["$gst_prefix/etc/rc.d", "$gst_prefix/etc/rc.d/init.d", "../init.d"],
232
454
 
233
455
  if (-f "$initd_path/$service")
234
456
  {
235
 
    if (&Utils::File::run ("$initd_path/$service $arg") == 0)
 
457
    if (&Utils::File::run ("$initd_path/$service", $arg) == 0)
236
458
    {
237
459
      &Utils::Report::do_report ("service_sysv_op_success", $service, $arg);
238
460
      &Utils::Report::leave ();
263
485
    $runlevel = $$r[0];
264
486
    $action   = ($$r[1] == $SERVICE_START) ? "S" : "K";
265
487
    $priority = sprintf ("%0.2d", $$r[2]);
 
488
    $priority = "50" if ($$r[2] <= 0);
266
489
 
267
490
    $configured_runlevels{$runlevel} = 1;
268
491
 
589
812
  if (! -x $service)
590
813
  {
591
814
    $chmod = 1;
592
 
    &Utils::File::run ("chmod ugo+x $service");
 
815
    &Utils::File::run ("chmod", "ugo+x", $service);
593
816
  }
594
817
 
595
 
  &Utils::File::run ("$service $arg", 1);
 
818
  &Utils::File::run_bg ($service, $arg);
596
819
 
597
820
  # return it to it's normal state
598
821
  if ($chmod)
599
822
  {
600
 
    &Utils::File::run ("chmod ugo-x $service");
 
823
    &Utils::File::run ("chmod", "ugo-x", $service);
601
824
  }
602
825
}
603
826
 
625
848
 
626
849
    if ($status == $SERVICE_START)
627
850
    {
628
 
      &Utils::File::run ("chmod ugo+x $script");
 
851
      &Utils::File::run ("chmod", "ugo+x", $script);
629
852
      &run_bsd_script ($script, "start");
630
853
    }
631
854
    else
632
855
    {
633
856
      &run_bsd_script ($script, "stop");
634
 
      &Utils::File::run ("chmod ugo-x $script");
 
857
      &Utils::File::run ("chmod", "ugo-x", $script);
635
858
    }
636
859
  }
637
860
}
778
1001
 
779
1002
  if (&gentoo_service_exists ($service))
780
1003
  {
781
 
    if (!&Utils::File::run ("/etc/init.d/$service $arg"))
 
1004
    if (!&Utils::File::run ("/etc/init.d/$service", $arg))
782
1005
    {
783
1006
      &Utils::Report::do_report ("service_sysv_op_success", $service, $arg);
784
1007
      &Utils::Report::leave ();
804
1027
 
805
1028
  if ($status == $SERVICE_START)
806
1029
  {
807
 
    &Utils::File::run ("rc-update add $script $rl");
 
1030
    &Utils::File::run ("rc-update", "add", $script, $rl);
808
1031
    &run_gentoo_script ($script, "start");
809
1032
  }
810
1033
  else
811
1034
  {
812
1035
    &run_gentoo_script ($script, "stop");
813
 
    &Utils::File::run ("rc-update del $script $rl");
 
1036
    &Utils::File::run ("rc-update", "del", $script, $rl);
814
1037
  }
815
1038
}
816
1039
 
909
1132
 
910
1133
  &Utils::Report::enter ();
911
1134
 
912
 
  if (!&Utils::File::run ("/etc/rc.d/$service $arg"))
 
1135
  if (!&Utils::File::run ("/etc/rc.d/$service", $arg))
913
1136
  {
914
1137
    &Utils::Report::do_report ("service_sysv_op_success", $service, $arg);
915
1138
    &Utils::Report::leave ();
1092
1315
    $rllist = "";
1093
1316
    %configured_runlevels = {};
1094
1317
 
1095
 
    &Utils::File::run ("insserv -r $script");
 
1318
    &Utils::File::run ("insserv", "-r", $script);
1096
1319
 
1097
1320
    foreach $rl (@$runlevels)
1098
1321
    {
1110
1333
    {
1111
1334
      $rllist =~ s/,$//;
1112
1335
 
1113
 
      &Utils::File::run ("insserv $script,start=$rllist");
 
1336
      &Utils::File::run ("insserv", $script, "start=$rllist");
1114
1337
    }
1115
1338
 
1116
1339
    if (!$configured_runlevels{$default_runlevel})
1146
1369
 
1147
1370
  if (&smf_service_exists ($service))
1148
1371
  {
1149
 
    if (!&Utils::File::run ("svcadm $op{$arg} $service"))
 
1372
    if (!&Utils::File::run ("svcadm", $op{$arg}, $service))
1150
1373
    {
1151
1374
      &Utils::Report::do_report ("service_sysv_op_success", $service, $arg);
1152
1375
      &Utils::Report::leave ();
1245
1468
 
1246
1469
  if ($active == $SERVICE_START)
1247
1470
  {
1248
 
    &Utils::File::run ("svcadm enable -s $service");
 
1471
    &Utils::File::run ("svcadm", "enable", "-s", $service);
1249
1472
  }
1250
1473
  else
1251
1474
  {
1252
 
    &Utils::File::run ("svcadm disable -s $service");
 
1475
    &Utils::File::run ("svcadm", "disable", "-s", $service);
1253
1476
  }
1254
1477
}
1255
1478
 
1279
1502
 
1280
1503
  $gst_dist = $Utils::Backend::tool{"platform"};
1281
1504
 
1282
 
  if (($gst_dist =~ /debian/) && (Utils::File::exists ("/etc/runlevel.conf")))
 
1505
  if (($gst_dist =~ /debian/))
1283
1506
  {
1284
 
    return "file-rc";
 
1507
    return "upstart";
1285
1508
  }
1286
1509
  elsif ($gst_dist =~ /slackware/)
1287
1510
  {
1315
1538
  my ($proc, $type);
1316
1539
  my %map =
1317
1540
    (
 
1541
     "upstart" => \&run_upstart_initd_script,
1318
1542
     "sysv"    => \&run_sysv_initd_script,
1319
1543
     "file-rc" => \&run_sysv_initd_script,
1320
1544
     "bsd"     => \&run_bsd_script,
1333
1557
{
1334
1558
  $type = &get_init_type ();
1335
1559
 
1336
 
  return &get_sysv_services ()   if ($type eq "sysv");
1337
 
  return &get_filerc_services () if ($type eq "file-rc");
1338
 
  return &get_bsd_services ()    if ($type eq "bsd");
1339
 
  return &get_gentoo_services () if ($type eq "gentoo");
1340
 
  return &get_rcng_services ()   if ($type eq "rcng");
1341
 
  return &get_suse_services ()   if ($type eq "suse");
1342
 
  return &get_smf_services ()    if ($type eq "smf");
 
1560
  return &get_upstart_services () if ($type eq "upstart");
 
1561
  return &get_sysv_services ()    if ($type eq "sysv");
 
1562
  return &get_filerc_services ()  if ($type eq "file-rc");
 
1563
  return &get_bsd_services ()     if ($type eq "bsd");
 
1564
  return &get_gentoo_services ()  if ($type eq "gentoo");
 
1565
  return &get_rcng_services ()    if ($type eq "rcng");
 
1566
  return &get_suse_services ()    if ($type eq "suse");
 
1567
  return &get_smf_services ()     if ($type eq "smf");
1343
1568
 
1344
1569
  return undef;
1345
1570
}
1350
1575
 
1351
1576
  $type = &get_init_type ();
1352
1577
 
1353
 
  &set_sysv_services   ($services) if ($type eq "sysv");
1354
 
  &set_filerc_services ($services) if ($type eq "file-rc");
1355
 
  &set_bsd_services    ($services) if ($type eq "bsd");
1356
 
  &set_gentoo_services ($services) if ($type eq "gentoo");
1357
 
  &set_rcng_services   ($services) if ($type eq "rcng");
1358
 
  &set_suse_services   ($services) if ($type eq "suse");
1359
 
  &set_smf_services    ($services) if ($type eq "smf");
 
1578
  &set_upstart_services ($services) if ($type eq "upstart");
 
1579
  &set_sysv_services    ($services) if ($type eq "sysv");
 
1580
  &set_filerc_services  ($services) if ($type eq "file-rc");
 
1581
  &set_bsd_services     ($services) if ($type eq "bsd");
 
1582
  &set_gentoo_services  ($services) if ($type eq "gentoo");
 
1583
  &set_rcng_services    ($services) if ($type eq "rcng");
 
1584
  &set_suse_services    ($services) if ($type eq "suse");
 
1585
  &set_smf_services     ($services) if ($type eq "smf");
1360
1586
}
1361
1587
 
1362
1588
1;