More work on launchd support for MacOS X

This commit is contained in:
Jamie Cameron
2015-05-02 15:33:36 -07:00
parent 9e4269ebae
commit ac238e37d1
5 changed files with 58 additions and 17 deletions

View File

@@ -29,6 +29,10 @@ if ($in{'new'}) {
# Server command and args
print &ui_table_row($text{'launchd_start'},
&ui_textarea("atstart", undef, 5, 80));
# Start at boot?
print &ui_table_row($text{'upstart_boot'},
&ui_yesno_radio("boot", 1));
}
else {
# Service name (non-editable)

View File

@@ -394,7 +394,7 @@ elsif ($init_mode eq "launchd" && $access{'bootup'}) {
print &ui_columns_row([
&ui_checkbox("d", $u->{'name'}, undef),
&ui_link($l, $u->{'name'}),
$u->{'boot'} == 1 ? $text{'yes'} :
$u->{'boot'} ? $text{'yes'} :
"<font color=#ff0000>$text{'no'}</font>",
$u->{'status'} ? $text{'yes'} :
"<font color=#ff0000>$text{'no'}</font>",

View File

@@ -594,6 +594,12 @@ elsif ($init_mode eq "osx") {
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 $_[0] } @agents;
return !$agent ? 0 :
$agent->{'boot'} ? 2 : 1;
}
}
=head2 enable_at_boot(action, description, startcode, stopcode, statuscode, &opts)
@@ -975,6 +981,10 @@ elsif ($init_mode eq "osx") {
&write_env_file($config{'hostconfig'}, \%hc);
&unlock_file($config{'hostconfig'});
}
elsif ($init_mode eq "launchd") {
# Create and if necessary enable a launchd agent
# XXX
}
}
=head2 disable_at_boot(action)
@@ -1107,6 +1117,10 @@ elsif ($init_mode eq "osx") {
}
&unlock_file($config{'hostconfig'});
}
elsif ($init_mode eq "launchd") {
# Adjust plist file to not run at boot
# XXX
}
}
=head2 delete_at_boot(name)
@@ -2290,13 +2304,16 @@ my @dirs = ("/Library/LaunchAgents",
"/Library/LaunchDaemons",
"/System/Library/LaunchAgents",
"/System/Library/LaunchDaemons");
my %pmap;
my (%pmap, %runatload);
foreach my $dir (@dirs) {
foreach my $file (glob("$dir/*.plist")) {
my $plist = &read_file_contents($file);
if ($plist =~ /<key>Label<\/key>\s*<string>([^<]+)/i) {
$pmap{$1} = $file;
}
if ($plist =~ /<key>RunAtLoad<\/key>\s*<(true|false)\/>/i) {
$runatload{$file} = $1;
}
}
}
@@ -2311,21 +2328,21 @@ foreach my $a (@rv) {
}
}
$a->{'start'} = $attrs{'program'};
$a->{'boot'} = $attrs{'ondemand'} eq 'false';
$a->{'file'} = $pmap{$a->{'name'}};
$a->{'boot'} = $runatload{$a->{'file'}} eq 'true';
}
return @rv;
}
=head2 create_launchd_agent(name, start-script)
=head2 create_launchd_agent(name, start-script, boot-flag)
Creates a new local launchd agent
=cut
sub create_launchd_agent
{
my ($name, $start) = @_;
my ($name, $start, $boot) = @_;
my $file = "/Library/LaunchDaemons/".$name.".plist";
my $plist = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n".
@@ -2339,8 +2356,10 @@ foreach my $a (&split_quoted_string($start)) {
$plist .= "<string>$a</string>\n";
}
$plist .= "</array>\n";
$plist .= "<key>RunAtLoad</key>\n";
$plist .= ($boot ? "<true/>\n" : "<false/>\n");
$plist .= "<key>KeepAlive</key>\n";
$plist .= "<true/>\n";
$plist .= "<false/>\n";
$plist .= "</dict>\n";
$plist .= "</plist>\n";
&open_lock_tempfile(PLIST, ">$file");
@@ -2367,4 +2386,30 @@ if ($a && $a->{'file'} && -f $a->{'file'}) {
}
}
=head2 stop_launchd_agent(name)
Kill the launchd daemon with some name
=cut
sub stop_launchd_agent
{
my ($name) = @_;
my $out = &backquote_logged(
"launchctl stop ".quotemeta($name)." 2>&1 </dev/null");
return (!$?, $out);
}
=head2 start_launchd_agent(name)
Startup the launchd daemon with some name
=cut
sub start_launchd_agent
{
my ($name) = @_;
my $out = &backquote_logged(
"launchctl start ".quotemeta($name)." 2>&1 </dev/null");
return (!$?, $out);
}
1;

View File

@@ -36,15 +36,7 @@ elsif ($in{'new'}) {
$in{'atstart'} =~ /\S/ || &error($text{'launchd_estart'});
# Create the config file
&create_launchd_agent($in{'name'}, $in{'atstart'});
# Enable at boot if selected
if ($in{'boot'} == 0) {
&disable_at_boot($in{'name'});
}
else {
&enable_at_boot($in{'name'});
}
&create_launchd_agent($in{'name'}, $in{'atstart'}, $in{'boot'});
&webmin_log("create", "launchd", $in{'name'});
}

View File

@@ -601,9 +601,9 @@ echo "PERLLIB=$PERLLIB" >>$config_dir/start
echo "export PERLLIB" >>$config_dir/start
uname -a | grep -i 'HP/*UX' >/dev/null
if [ $? = "0" ]; then
echo "exec '$wadir/miniserv.pl' $config_dir/miniserv.conf &" >>$config_dir/start
echo "exec '$wadir/miniserv.pl' \$* $config_dir/miniserv.conf &" >>$config_dir/start
else
echo "exec '$wadir/miniserv.pl' $config_dir/miniserv.conf" >>$config_dir/start
echo "exec '$wadir/miniserv.pl' \$* $config_dir/miniserv.conf" >>$config_dir/start
fi
echo "#!/bin/sh" >>$config_dir/stop