Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libutil / ttyaction.c
blob629ef7528a6da8f531a86838f018f021cb82f617
1 /* $NetBSD: ttyaction.c,v 1.18 2005/08/27 17:06:29 elad Exp $ */
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * For each matching "tty" and "action" run the "command."
34 * See fnmatch() for matching the tty name.
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: ttyaction.c,v 1.18 2005/08/27 17:06:29 elad Exp $");
40 #endif /* LIBC_SCCS and not lint */
42 #include <sys/types.h>
43 #include <sys/wait.h>
45 #include <assert.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <fnmatch.h>
50 #include <paths.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
56 #include "util.h"
58 #ifndef _PATH_TTYACTION
59 #define _PATH_TTYACTION "/etc/ttyaction"
60 #endif
62 static const char *actfile = _PATH_TTYACTION;
63 static const char *pathenv = "PATH=" _PATH_STDPATH;
65 int
66 ttyaction(const char *tty, const char *act, const char *user)
68 FILE *fp;
69 char *p1, *p2;
70 const char *argv[4];
71 const char *envp[8];
72 char *lastp;
73 char line[1024];
74 char env_tty[64];
75 char env_act[64];
76 char env_user[256];
77 int error, linenum, status;
78 pid_t pid;
80 _DIAGASSERT(tty != NULL);
81 _DIAGASSERT(act != NULL);
82 _DIAGASSERT(user != NULL);
84 fp = fopen(actfile, "r");
85 if (fp == NULL)
86 return 0;
88 /* Skip the "/dev/" part of the first arg. */
89 if (!strncmp(tty, "/dev/", (size_t)5))
90 tty += 5;
92 /* Args will be: "sh -c ..." */
93 argv[0] = _PATH_BSHELL;
94 argv[1] = "-c";
95 argv[2] = NULL; /* see below */
96 argv[3] = NULL;
99 * Environment needs: TTY, ACT, USER
101 snprintf(env_tty, sizeof(env_tty), "TTY=%s", tty);
102 snprintf(env_act, sizeof(env_act), "ACT=%s", act);
103 snprintf(env_user, sizeof(env_user), "USER=%s", user);
104 envp[0] = pathenv;
105 envp[1] = env_tty;
106 envp[2] = env_act;
107 envp[3] = env_user;
108 envp[4] = NULL;
110 linenum = 0;
111 status = 0;
112 while (fgets(line, (int)sizeof(line), fp)) {
113 linenum++;
115 /* Allow comment lines. */
116 if (line[0] == '#')
117 continue;
119 p1 = strtok_r(line, " \t", &lastp);
120 p2 = strtok_r(NULL, " \t", &lastp);
121 /* This arg goes to end of line. */
122 argv[2] = strtok_r(NULL, "\n", &lastp);
123 if (!p1 || !p2 || !argv[2]) {
124 warnx("%s: line %d format error", actfile, linenum);
125 continue;
127 if (fnmatch(p1, tty, 0) || fnmatch(p2, act, 0))
128 continue;
129 /* OK, this is a match. Run the command. */
130 pid = fork();
131 if (pid == -1) {
132 warnx("fork failed: %s", strerror(errno));
133 continue;
135 if (pid == 0) {
136 /* This is the child. */
137 error = execve(argv[0],
138 (char *const *)__UNCONST(argv),
139 (char *const *)__UNCONST(envp));
140 /* If we get here, it is an error. */
141 warnx("%s: line %d: exec failed: %s",
142 actfile, linenum, strerror(errno));
143 _exit(1);
145 /* This is the parent. */
146 error = waitpid(pid, &status, 0);
147 if (error == -1) {
148 warnx("%s: line %d: wait failed: %s",
149 actfile, linenum, strerror(errno));
150 continue;
152 if (WTERMSIG(status)) {
153 warnx("%s: line %d: child died with signal %d",
154 actfile, linenum, WTERMSIG(status));
155 continue;
158 fclose(fp);
159 return status;