boinc/mac_installer/PostInstall.cpp

112 lines
3.4 KiB
C++
Raw Normal View History

// Berkeley Open Infrastructure for Network Computing
// http://boinc.berkeley.edu
// Copyright (C) 2005 University of California
//
// This is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// either version 2.1 of the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// To view the GNU Lesser General Public License visit
// http://www.gnu.org/copyleft/lesser.html
// or write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
/* PostInstall.c */
#include <Carbon/Carbon.h>
#include <unistd.h> // getlogin
#include <sys/types.h> // getpwname, getpwuid, getuid
#include <pwd.h> // getpwname, getpwuid, getuid
#include <sys/wait.h> // waitpid
#include "LoginItemAPI.h" //please take a look at LoginItemAPI.h for a explanation of the routines available to you.
void Initialize(void); /* function prototypes */
static OSErr QuitAppleEventHandler(const AppleEvent *appleEvt, AppleEvent* reply, UInt32 refcon);
void SetUIDBackToUser (void);
Boolean gQuitFlag = false; /* global */
int main(int argc, char *argv[])
{
char *p, *q;
Boolean Success;
int NumberOfLoginItems, Counter;
Initialize();
Success = false;
// *****************************************************************************************
// Everything after this REQUIRES us to be setuid to the login user's user ID
// *****************************************************************************************
// Installer is running as root. We must setuid back to the logged in user
// in order to add a startup item to the user's login preferences
SetUIDBackToUser ();
NumberOfLoginItems = GetCountOfLoginItems(kCurrentUser);
// Search existing login items in reverse order, deleting any duplicates of ours
for (Counter = NumberOfLoginItems ; Counter > 0 ; Counter--)
{
p = ReturnLoginItemPropertyAtIndex(kCurrentUser, kApplicationNameInfo, Counter-1);
q = p;
while (*q)
{
// It is OK to modify the returned string because we "own" it
*q = toupper(*q); // Make it case-insensitive
q++;
}
if (strcmp(p, "BOINCMANAGER.APP") == 0)
Success = RemoveLoginItemAtIndex(kCurrentUser, Counter-1);
}
Success = AddLoginItemWithPropertiesToUser(kCurrentUser,
"/Applications/BOINCManager.app", kDoNotHideOnLaunch);
return 0;
}
void SetUIDBackToUser (void)
{
char *p;
uid_t login_uid;
passwd *pw;
p = getlogin();
pw = getpwnam(p);
login_uid = pw->pw_uid;
setuid(login_uid);
}
void Initialize() /* Initialize some managers */
{
OSErr err;
InitCursor();
err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP((AEEventHandlerProcPtr)QuitAppleEventHandler), 0, false );
if (err != noErr)
ExitToShell();
}
static OSErr QuitAppleEventHandler( const AppleEvent *appleEvt, AppleEvent* reply, UInt32 refcon )
{
gQuitFlag = true;
return noErr;
}