• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1191
  • Last Modified:

Mac application installer needs to start user agent but not as root

I've built an install using Apple's PackageMaker utility.  It installs a daemon and a user agent.  At the end of the install I would like to start the user agent using launchctl.  However, since I had to elevate privileges during the install in order to install my daemon, I end up starting the user agent as root.  If I use sudo -u <user> launchctl, I just get an error: "launch_msg(): Socket is not connected".

Is there any way to un-elevate privileges during an install?
postflight:
 
# this just errors out with launch_msg(): Socket is not connected".
# sudo -u current_user /bin/launchctl load ""/Library/LaunchAgents/com.myuseragent.plist"
 
#  this works but starts the user agent as root.  I want it to run as the current user
/bin/launchctl load "/Library/LaunchAgents/com.myuseragent.plist"

Open in new window

0
jimbojj
Asked:
jimbojj
  • 4
  • 3
1 Solution
 
heteronymousCommented:
How are you determining the current user ?

You have "current_user" but that's not a shell/bash internal so that's not going to work.

From Nigel Kersten (see http://explanatorygap.net , afp548.comApple's Server mailing list, MacEnterprise list)

to get the current logged in user
/usr/bin/who | /usr/bin/grep console | /usr/bin/cut -d " " -f 1

So you would use:

current_user=$(/usr/bin/who | /usr/bin/grep console | /usr/bin/cut -d " " -f 1)

You might also try specifying the user in your launchd plist via the provided
UserName  key.

See
http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

You could also get the user in Python via
import os
os.getenv('USER')
0
 
jimbojjAuthor Commented:
I came up with an applescript to solve my problem.  Since it will be run in the context of the logged-on user, my app starts correctly.

So far I haven't seen any negative side affects from this.


postflight:
 
osascript -e "tell application \"System Events\"" -e "do shell script \"launchctl load -S Aqua /Library/LaunchAgents/com.myproduct.plist\"" -e "end tell"

Open in new window

0
 
heteronymousCommented:
Ok, but you hardly specified you wanted or needed an AppleScript-based solution, but yours is another way to go.
What I replied with already does work.

If you're running as root, you can run another command via:

su <username> -c /path/to/binary

Or
 
sudo -u [username] [command]
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
jimbojjAuthor Commented:
I was looking for any solution that would work--I didn't have any specific technology in mind.

Just launching the app won't solve the problem--when the current user logs out and another logs in, since nothing has told launchd to run it, it won't automatically start for the new user.

As far as your previous suggestion, I need my launchd agent to run as whoever is logged in--which can be anybody.  I can't put a specific user in the .plist file.   Was this your solution?
0
 
jimbojjAuthor Commented:
OK, you were right.  Just starting the app as the user works because when they log out launchd does refresh its list of what should be run and will start it anyway.  I like your solution better than mine because I HATE applescript. Thanks for your help!!  
0
 
heteronymousCommented:
Actually, what a good way to go would be to have your plist launch your script: that way, via the script,
you can get the current user, and have the launch app as/for them.

If you look at (don't modify of course !) some of the Apple plists in /System/Library/LaunchDaemons
eg: cat /System/Library/LaunchDaemons/ssh.plist

you can see that it does just that - invokes a script ( /usr/libexec/sshd-keygen-wrapper )that invokes the desired binary with specific options.


0
 
heteronymousCommented:
typo: "have the launch app" should read: have that launch the app
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now