Home | History | Annotate | Line # | Download | only in libutil
ttyaction.c revision 1.2
      1  1.2  gwr /*	$NetBSD: ttyaction.c,v 1.2 1996/11/14 19:16:02 gwr Exp $	*/
      2  1.1  gwr 
      3  1.1  gwr /*-
      4  1.1  gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.1  gwr  * All rights reserved.
      6  1.1  gwr  *
      7  1.1  gwr  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  gwr  * by Gordon W. Ross.
      9  1.1  gwr  *
     10  1.1  gwr  * Redistribution and use in source and binary forms, with or without
     11  1.1  gwr  * modification, are permitted provided that the following conditions
     12  1.1  gwr  * are met:
     13  1.1  gwr  * 1. Redistributions of source code must retain the above copyright
     14  1.1  gwr  *    notice, this list of conditions and the following disclaimer.
     15  1.1  gwr  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  gwr  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  gwr  *    documentation and/or other materials provided with the distribution.
     18  1.1  gwr  * 3. All advertising materials mentioning features or use of this software
     19  1.1  gwr  *    must display the following acknowledgement:
     20  1.1  gwr  *        This product includes software developed by the NetBSD
     21  1.1  gwr  *        Foundation, Inc. and its contributors.
     22  1.1  gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  gwr  *    contributors may be used to endorse or promote products derived
     24  1.1  gwr  *    from this software without specific prior written permission.
     25  1.1  gwr  *
     26  1.1  gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  1.1  gwr  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  gwr  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  gwr  */
     38  1.1  gwr 
     39  1.1  gwr /*
     40  1.1  gwr  * For each matching "tty" and "action" run the "command."
     41  1.1  gwr  * See fnmatch() for matching the tty name.
     42  1.1  gwr  */
     43  1.1  gwr 
     44  1.1  gwr #include <sys/types.h>
     45  1.1  gwr #include <sys/wait.h>
     46  1.1  gwr 
     47  1.1  gwr #include <errno.h>
     48  1.1  gwr #include <fcntl.h>
     49  1.1  gwr #include <unistd.h>
     50  1.1  gwr #include <stdlib.h>
     51  1.1  gwr #include <stdio.h>
     52  1.1  gwr #include <string.h>
     53  1.1  gwr #include <paths.h>
     54  1.1  gwr 
     55  1.1  gwr #include "util.h"
     56  1.1  gwr 
     57  1.1  gwr #ifndef _PATH_TTYACTION
     58  1.1  gwr #define _PATH_TTYACTION "/etc/ttyaction"
     59  1.1  gwr #endif
     60  1.1  gwr 
     61  1.1  gwr static char *actfile = _PATH_TTYACTION;
     62  1.1  gwr static char *pathenv = "PATH=" _PATH_STDPATH;
     63  1.1  gwr 
     64  1.1  gwr int
     65  1.1  gwr ttyaction(tty, act, user)
     66  1.1  gwr 	char *tty;
     67  1.1  gwr 	char *act;
     68  1.1  gwr 	char *user;
     69  1.1  gwr {
     70  1.1  gwr 	FILE *fp;
     71  1.1  gwr 	char *p1, *p2;
     72  1.1  gwr 	char *argv[4];
     73  1.1  gwr 	char *envp[8];
     74  1.1  gwr 	char line[1024];
     75  1.1  gwr 	char env_tty[64];
     76  1.1  gwr 	char env_act[64];
     77  1.1  gwr 	char env_user[256];
     78  1.1  gwr 	int err, linenum, pid, status;
     79  1.1  gwr 
     80  1.1  gwr 	fp = fopen(actfile, "r");
     81  1.1  gwr 	if (fp == NULL)
     82  1.1  gwr 		return 0;
     83  1.1  gwr 
     84  1.2  gwr 	/* Skip the "/dev/" part of the first arg. */
     85  1.2  gwr 	if (!strncmp(tty, "/dev/", 5))
     86  1.2  gwr 		tty += 5;
     87  1.2  gwr 
     88  1.1  gwr 	/* Args will be: "sh -c ..." */
     89  1.1  gwr 	argv[0] = _PATH_BSHELL;
     90  1.1  gwr 	argv[1] = "-c";
     91  1.1  gwr 	argv[2] = NULL;	/* see below */
     92  1.1  gwr 	argv[3] = NULL;
     93  1.1  gwr 
     94  1.1  gwr 	/* Environment needs: TTY, ACT, USER */
     95  1.1  gwr 	sprintf(env_tty, "TTY=%.63s", tty);
     96  1.1  gwr 	sprintf(env_act, "ACT=%.63s", act);
     97  1.1  gwr 	sprintf(env_user, "USER=%.255s", user);
     98  1.1  gwr 	envp[0] = pathenv;
     99  1.1  gwr 	envp[1] = env_tty;
    100  1.1  gwr 	envp[2] = env_act;
    101  1.1  gwr 	envp[3] = env_user;
    102  1.1  gwr 	envp[4] = NULL;
    103  1.1  gwr 
    104  1.1  gwr 	linenum = 0;
    105  1.1  gwr 	status = 0;
    106  1.1  gwr 	while (fgets(line, sizeof(line), fp)) {
    107  1.2  gwr 		linenum++;
    108  1.2  gwr 
    109  1.2  gwr 		/* Allow comment lines. */
    110  1.2  gwr 		if (line[0] == '#')
    111  1.2  gwr 			continue;
    112  1.1  gwr 
    113  1.1  gwr 		p1 = strtok(line, " \t");
    114  1.1  gwr 		p2 = strtok(NULL, " \t");
    115  1.1  gwr 		/* This arg goes to end of line. */
    116  1.1  gwr 		argv[2] = strtok(NULL, "\n");
    117  1.1  gwr 		if (!p1 || !p2 || !argv[2]) {
    118  1.1  gwr 			warnx("%s: line %d format error", actfile, linenum);
    119  1.1  gwr 			continue;
    120  1.1  gwr 		}
    121  1.1  gwr 		if (fnmatch(p1, tty, 0) || fnmatch(p2, act, 0))
    122  1.1  gwr 			continue;
    123  1.1  gwr 		/* OK, this is a match.  Run the command. */
    124  1.1  gwr 		pid = fork();
    125  1.1  gwr 		if (pid == -1) {
    126  1.1  gwr 			warnx("fork failed: %s", strerror(errno));
    127  1.1  gwr 			continue;
    128  1.1  gwr 		}
    129  1.1  gwr 		if (pid == 0) {
    130  1.1  gwr 			/* This is the child. */
    131  1.1  gwr 			err = execve(argv[0], argv, envp);
    132  1.1  gwr 			/* If we get here, it is an error. */
    133  1.1  gwr 			warnx("%s: line %d: exec failed: %s",
    134  1.1  gwr 				  actfile, linenum, strerror(errno));
    135  1.1  gwr 			exit(1);
    136  1.1  gwr 		}
    137  1.1  gwr 		/* This is the parent. */
    138  1.1  gwr 		err = waitpid(pid, &status, 0);
    139  1.1  gwr 		if (err == -1) {
    140  1.1  gwr 			warnx("%s: line %d: wait failed: %s",
    141  1.1  gwr 				  actfile, linenum, strerror(errno));
    142  1.1  gwr 			continue;
    143  1.1  gwr 		}
    144  1.1  gwr 		if (WTERMSIG(status)) {
    145  1.1  gwr 			warnx("%s: line %d: child died with signal %d",
    146  1.1  gwr 				  actfile, linenum, WTERMSIG(status));
    147  1.1  gwr 			continue;
    148  1.1  gwr 		}
    149  1.1  gwr 	}
    150  1.1  gwr 	fclose(fp);
    151  1.1  gwr 	return status;
    152  1.1  gwr }
    153  1.1  gwr 
    154