chiark / gitweb /
Great reorganization.
[tripe] / common / util.c
diff --git a/common/util.c b/common/util.c
new file mode 100644 (file)
index 0000000..ceeb0bd
--- /dev/null
@@ -0,0 +1,93 @@
+/* -*-c-*-
+ *
+ * $Id: util.c,v 1.3 2004/04/08 01:36:17 mdw Exp $
+ *
+ * Utilities for the client and the server
+ *
+ * (c) 2001 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Trivial IP Encryption (TrIPE).
+ *
+ * TrIPE is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * TrIPE is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with TrIPE; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "util.h"
+
+#include <sys/ioctl.h>
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @u_detach@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Detaches from the current terminal and ensures it can never
+ *             acquire a new one.  Calls @fork@.
+ */
+
+void u_detach(void)
+{
+#ifdef TIOCNOTTY
+  {
+    int fd;
+    if ((fd = open("/dev/tty", O_RDONLY)) >= 0) {
+      ioctl(fd, TIOCNOTTY);
+      close(fd);
+    }
+  }
+#endif
+  setsid();
+  if (fork() > 0)
+    _exit(0);
+}
+
+/* --- @u_daemon@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    Zero if OK, nonzero on failure.
+ *
+ * Use:                Becomes a daemon.
+ */
+
+int u_daemon(void)
+{
+  pid_t kid;
+
+  if ((kid = fork()) < 0)
+    return (-1);
+  if (kid)
+    _exit(0);
+  u_detach();
+  return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/