06-19-2009, 10:28 AM
I have an application (in C/C++, Carbon) which occasionally downloads itself from an FTP server to the Applications folder (the original application is renamed to .bak). Now I want to relaunch this new version. How can I do that? I have tried following:
1. if the application wants to restart itself, then right before the end of main(), I do execlp("sh", "sh", "-c", "open -a /Applications/MyApp.app", 0);
This doesn't work as soon as I use a socket() call in the application, which I necessarily have to use to download the new version. It doesn't work on both OS X Tiger and Leopard. If I don't use the socket(), it works fine.
2. I tried to fork() and let the child do all the work (GUI and download). The parent waits for the child and if the child exits with return code 42, it does the execlp() to restart. This doesn't work because after the fork() the child actually can't use core-foundation on Leopard.
3. the application does all the work (GUI and download). If it wants to restart, I do fork() and in the child I do execlp(). This works on Tiger but not on Leopard. The same with vfork().
4. so I though fork() after all the work is done is too late. The application first does the fork(). The parent does all the work. The child just waits (read from stdin (0)). When the app wants to restart, the parent kills the child with SIGUSR1 otherwise it kills the child with SIGTERM. The child handles SIGUSR1 and in the handler it execlp() itself. This works on Tiger but not on Leopard.
5. actually should be point 0. I tried the launch services to launch the app, but it doesn't work, because the OS finds that the application is already running, and thus it doesn't re-launch it.
Currently I just show a message telling "new version downloaded, pls restart". Is there a way to re-launch "itself"?