From 2e6b5bc58a6e6bdca77a1770be4995c3c0187ed9 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Tue, 27 May 2008 17:10:49 +0000 Subject: [PATCH] Add the notty utility, a program I wrote long ago which I should have brought into base long ago. This program provides a convenient shortcut to detaching a command from the controlling terminal and running it in the background. --- bin/Makefile | 3 +- bin/notty/Makefile | 8 ++++ bin/notty/notty.1 | 24 ++++++++++ bin/notty/notty.c | 107 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 bin/notty/Makefile create mode 100644 bin/notty/notty.1 create mode 100644 bin/notty/notty.c diff --git a/bin/Makefile b/bin/Makefile index e138d447c6..93efd523e8 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,6 +1,6 @@ # From: @(#)Makefile 8.1 (Berkeley) 5/31/93 # $FreeBSD: src/bin/Makefile,v 1.15.2.2 2000/10/31 09:43:57 dougb Exp $ -# $DragonFly: src/bin/Makefile,v 1.9 2006/07/26 18:26:13 swildner Exp $ +# $DragonFly: src/bin/Makefile,v 1.10 2008/05/27 17:10:48 dillon Exp $ SUBDIR= cat \ chio \ @@ -22,6 +22,7 @@ SUBDIR= cat \ mined \ mkdir \ mv \ + notty \ pax \ ps \ pwd \ diff --git a/bin/notty/Makefile b/bin/notty/Makefile new file mode 100644 index 0000000000..aa4db274a5 --- /dev/null +++ b/bin/notty/Makefile @@ -0,0 +1,8 @@ +# +# $DragonFly: src/bin/notty/Makefile,v 1.1 2008/05/27 17:10:49 dillon Exp $ + +PROG= notty +SRCS= notty.c +MAN= notty.1 + +.include diff --git a/bin/notty/notty.1 b/bin/notty/notty.1 new file mode 100644 index 0000000000..f056841b61 --- /dev/null +++ b/bin/notty/notty.1 @@ -0,0 +1,24 @@ +.\" +.\" $DragonFly: src/bin/notty/notty.1,v 1.1 2008/05/27 17:10:49 dillon Exp $ +.Dd May 27, 2008 +.Dt NOTTY 1 +.Os +.Sh NAME +.Nm notty +.Nd Execute a program in the background, detached from the controlling terminal +.Sh SYNOPSIS +.Nm notty +.Op Fl 012 +.Ar command Ar args... +.Sh DESCRIPTION +The +.Nm +utility will detach the specified command from the controlling terminal, +running it in the background. By default stdin, stdout, and stderr are +set to /dev/null. One or more of stdin, stdout, or stderr may be left +open by specifying the appropriate options. +.Sh HISTORY +The +.Nm +command was originally written in 1994 by Matthew Dillon to provide +a convenient shortcut for detaching programs. diff --git a/bin/notty/notty.c b/bin/notty/notty.c new file mode 100644 index 0000000000..eca6619b6d --- /dev/null +++ b/bin/notty/notty.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2008 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/bin/notty/notty.c,v 1.1 2008/05/27 17:10:49 dillon Exp $ + */ +/* + * NOTTY.C - program to disconnect a program from the tty and close + * stdin, stdout, and stderr (-012 to specify which descriptors + * to leave open). + * + * NOTTY [-012] + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void usage(void); + +int +main(int ac, char **av) +{ + const char *opts = ""; + int ttyfd; + int fd; + + if (ac == 1) + usage(); + + if (av[1]) { + if (av[1][0] == '-') { + opts = av[1]; + ++av; + } + } + + + ttyfd = open("/dev/null", O_RDWR); + + if (strchr(opts, '0') == NULL && ttyfd != 0) + dup2(ttyfd, 0); + if (strchr(opts, '1') == NULL && ttyfd != 1) + dup2(ttyfd, 1); + if (strchr(opts, '2') == NULL && ttyfd != 2) + dup2(ttyfd, 2); + + if (ttyfd > 2) + close(ttyfd); + + fd = open("/dev/tty", O_RDWR); + if (fd >= 0) { + ioctl(fd, TIOCNOTTY, 0); + close(fd); + } + + if (fork() == 0) { + setsid(); + exit(execvp(av[1], av + 1)); + } + exit(0); +} + +static void +usage(void) +{ + fprintf(stderr, "notty [-012] command args...\n"); + exit(1); +} + -- 2.41.0