#include #include #include #include #include int main(int argc, char **argv) { if(argc < 3) { fprintf(stderr, "Usage: %s pidfile command [arguments]\n", argv[0]); return 1; } const char *pidfilename = argv[1]; FILE *pidfile = fopen(pidfilename, "w"); if(pidfile == NULL) { perror("fopen (opening pidfile)"); return 1; } pid_t own_pid = getpid(); if(fprintf(pidfile, "%ji\n", (intmax_t) own_pid) < 0) { perror("fprintf (printing pid)"); fclose(pidfile); return 1; } if(fclose(pidfile) != 0) { perror("fclose (pidfile)"); return 1; } char **daemon_argv = &argv[2]; const char *daemon_command = daemon_argv[0]; execvp(daemon_command, daemon_argv); perror("execvp"); return 1; }