Restarting the program when updating

I checked everywhere, so hopefully I won’t repeat the question.

I want to add a portable update function to some C code I'm writing. The program may not be in any particular place, and I would prefer to save it in one binary file (loading of the dynamic library was not)

Then, after the update is complete, I want the program to be able to restart (not a cycle, actually reboot from the hard drive)

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

+6
source share
2 answers

If you know where the program is stored on disk, you can exec() execute 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 located on disk, either use execvp() to search for it on $ PATH, or find out. On Linux, use the /proc - and /proc/self/exe ; this is a symbolic link to the executable, so you will need to use readlink() to get the value. Beware: readlink() not null, terminates the line it is reading.

If you want, you can arrange for the transfer of an argument that tells the new process that it restarts after the update; the list of available minimum arguments that I have provided can be as complicated as you need (a list of files currently open for editing, perhaps, or any other relevant information and options).

Also, be sure to clear before re-executing - for example, close all open files. Remember that open file descriptors are inherited by the executable process (if you did not mark them for closing on exec using FD_CLOEXEC or O_CLOEXEC ), but the new process will not know what they are for, unless you say so (in the argument list), therefore, he cannot use them. They will simply clutter up the process without helping at all.

+10
source

Yes, you need to call the correct exec() function. There may be some complications, it may be difficult to find the absolute path name. You need:

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

Since the call to exec() replaces the current process, this should be all you need to do to restart yourself. You may also need to close all open files, as they could be β€œinherited” for yourself, which you rarely need.

+2
source

All Articles