Welcome to little lamb

Code » anopa » commit 5476f50

Add aa-ctty

author Olivier Brunel
2015-04-21 19:23:18 UTC
committer Olivier Brunel
2015-04-21 19:23:18 UTC
parent ca5c8ccae005d6961b6bcfebe196f8724f0c093d

Add aa-ctty

doc/aa-ctty.pod +39 -0
package/modes +1 -0
package/targets.mak +2 -0
src/utils/aa-ctty.c +95 -0
src/utils/deps-exe/aa-ctty +2 -0

diff --git a/doc/aa-ctty.pod b/doc/aa-ctty.pod
new file mode 100644
index 0000000..65a756c
--- /dev/null
+++ b/doc/aa-ctty.pod
@@ -0,0 +1,39 @@
+=head1 NAME
+
+aa-ctty - Helper for execline script to set controlling terminal
+
+=head1 SYNOPSIS
+
+B<aa-ctty> [B<-f> I<FD>] [B<-s>] I<PROG...>
+
+=head1 OPTIONS
+
+=over
+
+=item B<-f, --fd> I<FD>
+
+Use file descriptor I<FD> as terminal; Defaults to stdin (0).
+
+=item B<-h, --help>
+
+Show help screen and exit.
+
+=item B<-s, --steal>
+
+Steal controlling terminal if already controlling terminal of a different
+session.
+
+=item B<-V, --version>
+
+Show version information and exit.
+
+=back
+
+=head1 DESCRIPTION
+
+B<aa-ctty>(1) will set the controlling terminal (via ioctl(TIOCSCTTY)) to that
+of the device open as file descriptor 0, or specified with B<--fd>. In typical
+B<execline> fashion, it then executes into the rest of its command line.
+
+If the ioctl call fails, a warning is printed and it still executes into the
+rest of its command line. If it fails to do so, it returns 111.
diff --git a/package/modes b/package/modes
index fba6848..5837c9c 100644
--- a/package/modes
+++ b/package/modes
@@ -1,4 +1,5 @@
 aa-chroot               0755
+aa-ctty                 0755
 aa-echo                 0755
 aa-enable               0755
 aa-incmdline            0755
diff --git a/package/targets.mak b/package/targets.mak
index bee3147..134d1ea 100644
--- a/package/targets.mak
+++ b/package/targets.mak
@@ -1,5 +1,6 @@
 BIN_TARGETS := \
 aa-chroot \
+aa-ctty \
 aa-echo \
 aa-enable \
 aa-incmdline \
@@ -33,6 +34,7 @@ aa-stage4
 DOC_TARGETS := \
 anopa.1 \
 aa-chroot.1 \
+aa-ctty.1 \
 aa-echo.1 \
 aa-enable.1 \
 aa-incmdline.1 \
diff --git a/src/utils/aa-ctty.c b/src/utils/aa-ctty.c
new file mode 100644
index 0000000..3a4c607
--- /dev/null
+++ b/src/utils/aa-ctty.c
@@ -0,0 +1,95 @@
+/*
+ * anopa - Copyright (C) 2015 Olivier Brunel
+ *
+ * aa-ctty.c
+ * Copyright (C) 2015 Olivier Brunel <jjk@jjacky.com>
+ *
+ * This file is part of anopa.
+ *
+ * anopa 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 3 of the License, or (at your option) any later
+ * version.
+ *
+ * anopa 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
+ * anopa. If not, see http://www.gnu.org/licenses/
+ */
+
+#include <getopt.h>
+#include <termios.h>
+#include <sys/ioctl.h>
+#include <skalibs/uint.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/strerr2.h>
+#include <anopa/common.h>
+
+static void
+dieusage (int rc)
+{
+    aa_die_usage (rc, "[OPTION...] PROG...",
+            " -f, --fd=FD                   Use FD as terminal (Default: 0)\n"
+            " -s, --steal                   Steal terminal from other session if needed\n"
+            " -h, --help                    Show this help screen and exit\n"
+            " -V, --version                 Show version information and exit\n"
+            );
+}
+
+int
+main (int argc, char * const argv[], char const * const *envp)
+{
+    PROG = "aa-ctty";
+    int fd = 0;
+    int steal = 0;
+
+    for (;;)
+    {
+        struct option longopts[] = {
+            { "fd",                 required_argument,  NULL,   'f' },
+            { "help",               no_argument,        NULL,   'h' },
+            { "steal",              no_argument,        NULL,   's' },
+            { "version",            no_argument,        NULL,   'V' },
+            { NULL, 0, 0, 0 }
+        };
+        int c;
+
+        c = getopt_long (argc, argv, "+f:hsV", longopts, NULL);
+        if (c == -1)
+            break;
+        switch (c)
+        {
+            case 'f':
+                if (!uint0_scan (optarg, &fd))
+                    strerr_diefu1sys (1, "set fd");
+                break;
+
+            case 'h':
+                dieusage (0);
+
+            case 's':
+                steal = 1;
+                break;
+
+            case 'V':
+                aa_die_version ();
+
+            default:
+                dieusage (1);
+        }
+    }
+    argc -= optind;
+    argv += optind;
+
+    if (argc == 0)
+        dieusage (1);
+
+    if (ioctl (fd, TIOCSCTTY, steal) < 0)
+        strerr_warnwu1sys ("set controlling terminal");
+
+    pathexec_run (argv[0], (char const * const *) argv, envp);
+    strerr_dieexec (111, argv[0]);
+}
diff --git a/src/utils/deps-exe/aa-ctty b/src/utils/deps-exe/aa-ctty
new file mode 100644
index 0000000..30987b4
--- /dev/null
+++ b/src/utils/deps-exe/aa-ctty
@@ -0,0 +1,2 @@
+${LIBANOPA}
+-lskarnet