88
88
return &get_sysv_default_runlevel ();
92
# TODO: Handle Upstart jobs, and not only traditional SystemV scripts inside an upstart system
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"],
102
$res = $dist_map{$Utils::Backend::tool{"platform"}};
103
&Utils::Report::do_report ("service_upstart_unsupported", $Utils::Backend::tool{"platform"}) if ($res eq undef);
107
# we are going to extract the name of the script
108
sub get_upstart_service_name
112
$service =~ s/$initd_path\///;
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
123
my ($runlevel, $action, $priority);
126
foreach $link (<$rcd_path/rc[0-6].d/[SK][0-9][0-9]$service>)
128
$link =~ s/$rcd_path\///;
129
$link =~ /rc([0-6])\.d\/([SK])([0-9][0-9]).*/;
130
($runlevel,$action,$priority)=($1,$2,$3);
134
push @arr, [ $runlevel, $SERVICE_START, $priority ];
136
elsif ($action eq "K")
138
push @arr, [ $runlevel, $SERVICE_STOP, $priority ];
145
# We are going to extract the information of the service
146
sub get_upstart_service_info
149
my ($script, @actions, @runlevels, $role);
151
# Return if it's a directory
152
return if (-d $service);
154
# We have to check if the service is executable
155
return unless (-x $service);
157
$script = &get_upstart_service_name ($service);
158
$runlevels = &get_upstart_runlevels_status($script);
160
return ($script, $runlevels);
163
# This function gets an ordered array of the available services from a upstart system
164
sub get_upstart_services
169
($rcd_path, $initd_path, $relative_path, $init_path) = &get_upstart_paths ();
170
return undef unless ($rcd_path && $initd_path && $init_path);
172
foreach $service (<$initd_path/*>)
176
@info = &get_upstart_service_info ($service);
177
# Only manage traditional init.d scripts, ignore services with jobs installed
179
if (!-e "$init_path/$script.conf")
181
push @arr, \@info if (scalar (@info));
188
# These are the functions for storing the service settings in upstart
189
sub remove_upstart_link
191
my ($rcd_path, $runlevel, $script) = @_;
193
foreach $link (<$rcd_path/rc$runlevel.d/[SK][0-9][0-9]$script>)
195
&Utils::Report::enter ();
196
&Utils::Report::do_report ("service_upstart_remove_link", "$link");
198
&Utils::Report::leave ();
204
my ($rcd_path, $relative_path, $runlevel, $action, $priority, $service) = @_;
205
my ($prio) = sprintf ("%0.2d",$priority);
207
symlink ("$relative_path/$service", "$rcd_path/rc$runlevel.d/$action$prio$service");
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 ();
214
sub run_upstart_initd_script
216
my ($service, $arg) = @_;
217
my ($rc_path, $initd_path);
220
&Utils::Report::enter ();
222
if (&Utils::File::run ("service", $service, $arg) == 0)
224
&Utils::Report::do_report ("service_upstart_op_success", $service, $arg);
225
&Utils::Report::leave ();
229
&Utils::Report::do_report ("service_upstart_op_failed", $service, $arg);
230
&Utils::Report::leave ();
234
sub set_upstart_service
237
my ($script, $priority, $runlevels, $default_runlevel);
238
my ($runlevel, $action, %configured_runlevels);
240
($rcd_path, $initd_path, $relative_path) = &get_upstart_paths ();
241
return unless ($rcd_path && $initd_path && $relative_path);
243
$script = $$service[0];
244
$runlevels = $$service[1];
245
$default_runlevel = &get_default_runlevel ();
247
foreach $r (@$runlevels)
250
$action = ($$r[1] == $SERVICE_START) ? "S" : "K";
251
$priority = sprintf ("%0.2d", $$r[2]);
252
$priority = "50" if ($$r[2] <= 0);
254
$configured_runlevels{$runlevel} = 1;
256
if (!-f "$rcd_path/rc$runlevel.d/$action$priority$script")
258
&remove_upstart_link ($rcd_path, $runlevel, $script);
259
&add_upstart_link ($rcd_path, $relative_path, $runlevel, $action, $priority, $script);
261
if ($runlevel eq $default_runlevel)
263
&run_upstart_initd_script ($script, ($$r[1] == $SERVICE_START) ? "start" : "stop");
268
# remove unneeded links
269
foreach $link (<$rcd_path/rc[0-6].d/[SK][0-9][0-9]$script>)
271
$link =~ /rc([0-6])\.d/;
274
if (!exists $configured_runlevels{$runlevel})
276
&remove_upstart_link ($rcd_path, $runlevel, $script);
278
if ($runlevel eq $default_runlevel)
280
&run_upstart_initd_script ($script, "stop");
286
sub set_upstart_services
290
foreach $i (@$services)
292
&set_upstart_service($i);
296
# This function gets the runlevel that is in use
297
sub get_sysv_default_runlevel
300
@arr = split / /, `/sbin/runlevel` ;
306
sub get_default_runlevel
308
my $type = &get_init_type ();
310
return "default" if ($type eq "gentoo" || $type eq "rcng" || $type eq "bsd" || $type eq "smf");
311
return &get_sysv_default_runlevel ();
91
314
sub get_sysv_paths
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"],