LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2011, 05:58 PM   #1
jeffbarish
LQ Newbie
 
Registered: Oct 2004
Distribution: Ubuntu
Posts: 27

Rep: Reputation: 15
Writing a daemon that can update itself


I have written a daemon server. I would like to add the ability for the daemon to update itself. That is, it should be able to download the latest version of the code (when it receives a signal from a client) and then restart itself. I am stuck on getting the daemon to restart itself. The problem is killing the old daemon without killing the process that is starting the new daemon. In my latest attempt, the daemon that is updating itself runs a program in a subshell (using system) that starts a restarter daemon that runs the original daemon (again using system) with the restart command option. With the restart option, the new version of the daemon sends a terminate signal to the old version. Obviously, when I run the original daemon manually with the restart option, it restarts reliably. I figured that issuing the same restart command from a daemon would adequately isolate the process doing the restarting from the program being restarted. Using ps, I can see that the daemon does actually restart. However, the client locks. In fact, a few times, the OS locked and I had to reset. Debugging is very difficult because I cannot print from a daemon. Am I doing something obviously wrong? Is there a better way to solve this problem?
 
Old 04-27-2011, 06:49 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
It sounds a bit convoluted, I usually try to go with the simplest solution - maybe a separate check_update script/binary run from cron?
 
Old 04-27-2011, 08:25 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Isn't it the package manager's job to update stuff?
 
Old 04-28-2011, 12:44 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Or you could write two scripts:

a) the first checks for an available update, and somehow notifies your program (for example, the script might print "yes" or "no" to stdout).

b) the second script performs the actual update (and starts a new instance of your newly updated daemon).

For example:
Code:
  char buf[80];
  FILE *fp = popen ("chk_for_updates.sh", "r");
  fgets (buf, sizeof (buf), fp);
  pclose (fp);
  if (strstr ("yes", buf)) {
    system ("do_update.sh &");
    exit (0);
  }
  ...
Admittedly a stretch...
 
Old 04-28-2011, 09:15 AM   #5
jeffbarish
LQ Newbie
 
Registered: Oct 2004
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
I may not have been clear that I intend for the update to be triggered by the user from the client. The client would check for updates of its own code and send a message to the servers to check for updates of their code. The main problem with using a cron job is that the client might update itself in a manner that requires the updated server code, but the servers would remain unupdated until the cron job runs. The only way to keep the client and server code synchronized would be to trigger updates to the client code from cron as well -- and to synchronize cron on multiple platforms. It's not out of the question, but there would be a period when updates could easily get out of sync. The same problems occur with the package manager. But I completely disagree that what I have implemented now is "a bit convoluted". It's absurdly convoluted. It's way beyond "a bit".

I thought about having a separate update program. The problem with simple scripts is that users do not operate on the platforms that run the servers, so getting the scripts to run would be problematic. The update program could be a daemon also that communicates with the same client. It would be responsible for downloading the new code and restarting the other daemons. The problem with this version of the idea is that I don't know what to do if the update daemon itself needs to be updated, though perhaps it would be simple enough that the need for an update would be unlikely.

The thought that I had last night when I should have been sleeping is that the servers will check for new code, download it if they find an update -- as they do now -- but then they will issue a reboot command. The servers are started by init.d, so the new code will run when the system reboots. This solution is inelegant, but I think it will work.
 
Old 04-28-2011, 09:31 AM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"cron" dude

The "client" sets a flag (for example, writes a file that says "Hey! Update!"). The cron script looks for the flag, and takes the appropriate action(s). Including rebooting the server (why not, if it's the most direct way to insure everything is set to a "known good state"?)
 
Old 04-28-2011, 09:22 PM   #7
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I don't think your logic is ideal, a client/server architecture indicates that there may be more than one client so what would happen in this case ? How about having the server update itself via cron and include a version check in the client to trigger an update if it's version doesn't match the server version ?
 
Old 04-29-2011, 10:00 AM   #8
jeffbarish
LQ Newbie
 
Registered: Oct 2004
Distribution: Ubuntu
Posts: 27

Original Poster
Rep: Reputation: 15
I like your suggestion. I'm not sure that I want the updates to be fully automatic (some users prefer to stick with obsolete code that works rather than update and risk failure), but I will consider this approach. In the meantime, my reboot idea is working. Thanks for all the suggestions.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not able to start bind:SELinux is preventing the named daemon from writing to the zon abhijit_mohanta Fedora 5 09-01-2009 05:03 PM
fedora bind start problem: SELinux is preventing the named daemon from writing to the abhijit_mohanta Linux - Networking 1 08-31-2009 08:03 AM
writing multihoming daemon without bind blackzone Linux - Networking 0 08-26-2004 09:40 PM
writing a daemon markus1982 Programming 1 11-13-2003 06:35 PM
which daemon or program watches writing files on floppy disk? norman68 Linux - Software 2 04-04-2003 07:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration