+
+int start_ustd_daemon()
+{
+ int result;
+ int fd[2];
+ pid_t child_pid;
+
+ result = pipe(fd);
+
+ result = child_pid = fork();
+ if(result == -1) {
+ PERROR("fork");
+ return -1;
+ }
+ else if(result == 0) {
+ return start_ustd(fd[1]);
+ }
+ else {
+ char buf;
+ FILE *pidfp;
+
+ /* It's important to write the file *before*
+ * the parent ends, because the file may be
+ * read as soon as the parent ends.
+ */
+ if(pidfile) {
+ pidfp = fopen(pidfile, "w+");
+ if(!pidfp) {
+ PERROR("fopen (%s)", pidfile);
+ WARN("killing child process");
+ result = kill(child_pid, SIGTERM);
+ if(result == -1) {
+ PERROR("kill");
+ }
+ return -1;
+ }
+
+ fprintf(pidfp, "%d\n", child_pid);
+ fclose(pidfp);
+ }
+
+ result = read(fd[0], &buf, 1);
+ if(result == -1) {
+ PERROR("read");
+ return -1;
+ }
+ if(result != 1) {
+ ERR("did not receive valid confirmation that the daemon is started");
+ return -1;
+ }
+
+ result = close(fd[0]);
+ if(result == -1) {
+ PERROR("close");
+ }
+
+ DBG("The daemon is now successfully started");
+ }
+
+ /* Wait for confirmation that the server is ready. */
+
+
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int result;
+
+ result = parse_args(argc, argv);
+ if(result == -1) {
+ exit(1);
+ }
+
+ if(daemon_mode) {
+ result = start_ustd_daemon();
+ }
+ else {
+ result = start_ustd(-1);
+ }
+
+ return result;
+}