[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-11
7
Medium Priority
?
1,188 Views
Last Modified: 2012-05-05
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
Comment
Question by:jimbojj
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:heteronymous
ID: 22968299
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
 

Author Comment

by:jimbojj
ID: 22988959
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
 
LVL 9

Accepted Solution

by:
heteronymous earned 1000 total points
ID: 22989570
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:jimbojj
ID: 22989652
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
 

Author Closing Comment

by:jimbojj
ID: 31515683
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
 
LVL 9

Expert Comment

by:heteronymous
ID: 22990243
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
 
LVL 9

Expert Comment

by:heteronymous
ID: 22990257
typo: "have the launch app" should read: have that launch the app
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A common question or need, when setting-up a new Mac for someone would be to make all of the applications, installed, available from the dock. Many people often do not realize an application is installed unless it is in the dock. Creating a custo…
In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
Users will learn how resize a batch of photos from a single command in Photoshop via Photoshop's Image Processor. Open up an Image you'd like to resize in Adobe Photoshop: Adjust the image size according to your preferences. Image > Adjustments > …
Users will learn how to set proper sequence settings, scale images, paste attributes, add transitions, fades, and music. Open up Final Cut Pro 7 and Create a new Project: Set the Sequence Settings. a) Click File > Easy Setup > Format > Apple ProRe…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question