/** * Return newly allocated buffer holding the name of this process's state file. * * (This can't reliably be static because we might fork...) **/ static int dcc_get_state_filename(char **fname) { int ret; char *dir; if ((ret = dcc_get_state_dir(&dir))) return ret; if (asprintf(fname, "%s/%s%ld", dir, dcc_state_prefix, (long) getpid()) == -1) { return EXIT_OUT_OF_MEMORY; } return 0; }
/* * Try to setup dnotify on the state directory. This returns the * descriptor of a pipe in @p dummy_fd. Every time the state changes, * a single byte is written to this pipe. A caller who select()s on * the pipe will therefore be woken every time there is a change. * * If we can do dnotify, create the dummy pipe and turn it on. * * @fixme One problem here is that if the state directory is deleted * and recreated, then we'll never notice and find the new one. I * don't know of any good fix, other than perhaps polling every so * often. So just don't do that. * * @fixme If this function is called repeatedly it will leak FDs. * * @todo Reimplement this on top of kevent for BSD. */ int dcc_mon_setup_notify (int *dummy_fd) { #ifdef F_NOTIFY char *state_dir; int ret; int fd; if (signal (SIGIO, dcc_mon_siginfo_handler) == SIG_ERR) { rs_log_error ("signal(SIGINFO) failed: %s", strerror(errno)); return EXIT_IO_ERROR; } if (pipe ((int *) pipe_fd) == -1) { rs_log_error ("pipe failed: %s", strerror (errno)); return EXIT_IO_ERROR; } *dummy_fd = pipe_fd[0]; /* read end */ dcc_set_nonblocking (pipe_fd[0]); dcc_set_nonblocking (pipe_fd[1]); if ((ret = dcc_get_state_dir (&state_dir))) return ret; if ((fd = open (state_dir, O_RDONLY)) == -1) { rs_log_error ("failed to open %s: %s", state_dir, strerror (errno)); free (state_dir); return EXIT_IO_ERROR; } /* CAUTION! Signals can start arriving immediately. Be ready. */ if (fcntl (fd, F_NOTIFY, DN_RENAME|DN_DELETE|DN_MULTISHOT) == -1) { rs_log_warning ("setting F_NOTIFY failed: %s", strerror (errno)); free (state_dir); return EXIT_IO_ERROR; } return 0; #else /* F_NOTIFY */ return EXIT_IO_ERROR; #endif /* F_NOTIFY */ }