From ac238e37d199c024b3f071f1b12fa77ce18f4de4 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sat, 2 May 2015 15:33:36 -0700 Subject: [PATCH] More work on launchd support for MacOS X --- init/edit_launchd.cgi | 4 ++++ init/index.cgi | 2 +- init/init-lib.pl | 55 +++++++++++++++++++++++++++++++++++++++---- init/save_launchd.cgi | 10 +------- setup.sh | 4 ++-- 5 files changed, 58 insertions(+), 17 deletions(-) diff --git a/init/edit_launchd.cgi b/init/edit_launchd.cgi index cc063bc1d..eaf3e0f96 100755 --- a/init/edit_launchd.cgi +++ b/init/edit_launchd.cgi @@ -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) diff --git a/init/index.cgi b/init/index.cgi index 1c9e183ea..5d1656b4c 100755 --- a/init/index.cgi +++ b/init/index.cgi @@ -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'} : "$text{'no'}", $u->{'status'} ? $text{'yes'} : "$text{'no'}", diff --git a/init/init-lib.pl b/init/init-lib.pl index b1e7189d0..af97b9223 100755 --- a/init/init-lib.pl +++ b/init/init-lib.pl @@ -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 =~ /Label<\/key>\s*([^<]+)/i) { $pmap{$1} = $file; } + if ($plist =~ /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 = "\n". "\n". @@ -2339,8 +2356,10 @@ foreach my $a (&split_quoted_string($start)) { $plist .= "$a\n"; } $plist .= "\n"; +$plist .= "RunAtLoad\n"; +$plist .= ($boot ? "\n" : "\n"); $plist .= "KeepAlive\n"; -$plist .= "\n"; +$plist .= "\n"; $plist .= "\n"; $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 &1 >$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