Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Start and stop a privileged service programmatically



Hi,
I want to manage a privileged service (namely, openvpn) programmatically. I am using Mac OS X 10.4.7. From the shell, I can do either


[to start]  sudo openvpn2 --daemon --config openvpn.conf
[to stop]  sudo kill -s SIGINT <pid>

or

[to start] sudo launchctl load net.openvpn.plist
[to stop] sudo launchctl unload net.openvpn.plist

(having previously created the .conf and .plist files). Both methods work flawlessly (well, launchctl gives the mysterious "Workaround Bonjour: Unknown error: 0" message, but it works). I would like to perform the above operations from inside my code. I have read "Performing Privileged Operations With Authorization Services" in Apple's documentation, and I have browsed through AuthSample and MoreAuthSample's code. As a test, I have taken AuthSample code and changed the performOperation() function in authtool.c by adding the following code:

/* XXX perform the actual operation here */
pid_t pid;
switch(pid = fork()) {
case 0: /* Child */ {
execl("/opt/local/sbin/openvpn2", "openvpn2", "--daemon", "--config", "/Users/me/Library/openvpn/openvpn.conf", 0);
}
case -1: /* an error occured */
return kMyAuthorizedCommandInternalError;
default: /* Parent */
break;
}


I have also tried to run launchctl:

execl("/bin/launchctl", "launchctl", "load", "/Library/LaunchDaemons/ net.openvpn.plist", 0);

The latter method simply does not work. In the console, I get several messages like this:

net.openvpn: exited abnormally: Broken pipe
net.openvpn: respawning too quickly! throttling

Using the former method, the process starts (as root, correctly), but it does not work. Browsing the logs, it seems that openvpn initialization chokes somewhere, and the routing tables are left incomplete (I get one tap0 entry, while I get three when I run openvpn from the shell).

My questions:

1) why do the commands from the shell and from the program behave differently? Is there any (possibly trivial) oversight in my code?
2) Is launchd to be preferred over other methods in this case? And is it the way to go in general?


Thanks in advance for your patience
Nicola
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.