summaryrefslogtreecommitdiffstats
path: root/how-to-update-process.c
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-05-13 22:18:16 +0200
committerMattias Andrée <m@maandree.se>2026-05-13 22:18:16 +0200
commitdb528a5d90415ec209aec34b1fa8e76850e7a954 (patch)
tree95f372430cfbdfaf0a06411675860cd2cda39538 /how-to-update-process.c
downloadhow-to-update-process-master.tar.gz
how-to-update-process-master.tar.bz2
how-to-update-process-master.tar.xz
First commitHEADmaster
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to 'how-to-update-process.c')
-rw-r--r--how-to-update-process.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/how-to-update-process.c b/how-to-update-process.c
new file mode 100644
index 0000000..6328ecf
--- /dev/null
+++ b/how-to-update-process.c
@@ -0,0 +1,42 @@
+#include <sys/auxv.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+
+static const char *text = "Change this text and recompile while the process is running";
+
+
+int
+main(int argc, char *argv[])
+{
+ uintptr_t exe_addr = (uintptr_t)getauxval(AT_EXECFN);
+ const char *exe;
+
+ if (!exe_addr) {
+ fprintf(stderr, "%s\n", "Sorry, AT_EXECFN not found, would have to use /proc/self/exe, which is out of scope");
+ /* by the way, if you just exec into /proc/self/exe
+ * you restart the process, to the same version as
+ * running even if it has been replaced (define
+ * RESTART to try it out) */
+ return 1;
+ }
+ exe = (void *)exe_addr; /* beware, this might be a relative path */
+
+ if (argc != 2 || strcmp(argv[1], text))
+ printf("%s\n", text);
+
+ unlink(exe);
+
+ for (;;) {
+ sleep(1);
+#ifdef RESTART
+ execlp("/proc/self/exe", argv[0], text, NULL);
+ abort();
+#else
+ execlp(exe, argv[0], text, NULL);
+#endif
+ }
+}