Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program restart self on update

Tags:

c

linux

restart

I checked everywhere so I am hopefully not repeating a question.

I want to add a portable update feature to some C code I am writing. The program may not be in any specific location, and I would prefer to keep it to a single binary (No dynamic library loading)

Then after the update is complete, I want the program to be able to restart (not a loop, actually reload from the HDD)

Is there any way to do this in C on Linux?

like image 696
Drumm Avatar asked Dec 18 '12 15:12

Drumm


2 Answers

If you know where the program is saved on disk, then you can exec() the program:

char args[] = { "/opt/somewhere/bin/program", 0 };

execv(args[0], args);
fprintf(stderr, "Failed to reexecute %s\n", args[0]);
exit(1);

If you don't know where the program is on disk, either use execvp() to search for it on $PATH, or find out. On Linux, use the /proc file system — and /proc/self/exe specifically; it is a symlink to the executable, so you would need to use readlink() to get the value. Beware: readlink() does not null terminate the string it reads.

If you want, you can arrange to pass an argument which indicates to the new process that it is being restarted after update; the bare minimum argument list I provided can be as complex as you need (a list of the files currently open for edit, perhaps, or any other appropriate information and options).

Also, don't forget to clean up before reexecuting — cleanly close any open files, for example. Remember, open file descriptors are inherited by the executed process (unless you mark them for closure on exec with FD_CLOEXEC or O_CLOEXEC), but the new process won't know what they're for unless you tell it (in the argument list) so it won't be able to use them. They'll just be cluttering up the process without helping in the least.

like image 188
Jonathan Leffler Avatar answered Nov 03 '22 12:11

Jonathan Leffler


Yes, you need to call the proper exec() function. There might be some complications, it can be troublesome to find the absolute path name. You need to:

  • Store the current directory in main().
  • Store the argc and (all) argv[] values from main().

Since calling exec() replaces the current process, that should be all you need to do in order to restart yourself. You might also need to take care to close any opened files, since they might otherwise be "inherited" back to yourself, which is seldom what you want.

like image 24
unwind Avatar answered Nov 03 '22 13:11

unwind