xref: /titanic_44/usr/src/lib/efcode/efdaemon/efdaemon.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * efdaemon - Emebbed Fcode Interpreter daemon.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Opens /dev/fcode, detaches from tty and reads a request.  Upon successful
33*7c478bd9Sstevel@tonic-gate  * return, invokes the Fcode interpreter via the shell script:
34*7c478bd9Sstevel@tonic-gate  * /usr/lib/efcode/efcode.sh  Waits for completion of the interpreter.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
39*7c478bd9Sstevel@tonic-gate #include <unistd.h>
40*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
41*7c478bd9Sstevel@tonic-gate #include <strings.h>
42*7c478bd9Sstevel@tonic-gate #include <syslog.h>
43*7c478bd9Sstevel@tonic-gate #include <errno.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate char efcode_sh_file[] = "/usr/lib/efcode/efcode.sh";
47*7c478bd9Sstevel@tonic-gate char dev_fcode_file[] = "/dev/fcode";
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate int debug = 0;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate int
52*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
55*7c478bd9Sstevel@tonic-gate 	extern int optind, opterr, optopt;
56*7c478bd9Sstevel@tonic-gate 	int c, fd, nbytes, status;
57*7c478bd9Sstevel@tonic-gate 	char tc;
58*7c478bd9Sstevel@tonic-gate 	pid_t pid, tpid;
59*7c478bd9Sstevel@tonic-gate 	long nerr = 0;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	openlog("efdaemon", LOG_PID|LOG_CONS, LOG_DAEMON);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d")) != EOF) {
64*7c478bd9Sstevel@tonic-gate 		switch (c) {
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 		case 'd':
67*7c478bd9Sstevel@tonic-gate 			debug++;
68*7c478bd9Sstevel@tonic-gate 			break;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 		case '?':
71*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Usage: efdaemon [ -d ]\n");
72*7c478bd9Sstevel@tonic-gate 			exit(1);
73*7c478bd9Sstevel@tonic-gate 		}
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	/*
77*7c478bd9Sstevel@tonic-gate 	 * Ensure we can open /dev/fcode
78*7c478bd9Sstevel@tonic-gate 	 */
79*7c478bd9Sstevel@tonic-gate 	if ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
80*7c478bd9Sstevel@tonic-gate 		/*
81*7c478bd9Sstevel@tonic-gate 		 * Only output message if debug is on.  On most systems,
82*7c478bd9Sstevel@tonic-gate 		 * /dev/fcode will not exist, so this message would pollute the
83*7c478bd9Sstevel@tonic-gate 		 * console.
84*7c478bd9Sstevel@tonic-gate 		 */
85*7c478bd9Sstevel@tonic-gate 		if (debug)
86*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Can't open %s: %s\n", dev_fcode_file,
87*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
88*7c478bd9Sstevel@tonic-gate 		exit(1);
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	close(fd);
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * Ensure that /usr/lib/efcode/efcode.sh exists and is executable.
94*7c478bd9Sstevel@tonic-gate 	 */
95*7c478bd9Sstevel@tonic-gate 	if (access(efcode_sh_file, X_OK | R_OK)) {
96*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: %s\n", efcode_sh_file, strerror(errno));
97*7c478bd9Sstevel@tonic-gate 		exit(1);
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	/*
101*7c478bd9Sstevel@tonic-gate 	 * Fork a child then parent exits so we're a child of initd.
102*7c478bd9Sstevel@tonic-gate 	 */
103*7c478bd9Sstevel@tonic-gate 	if ((pid = fork()) < 0) {
104*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Fork failed: %s\n", strerror(errno));
105*7c478bd9Sstevel@tonic-gate 		exit(1);
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 	if (pid)
108*7c478bd9Sstevel@tonic-gate 		exit(0);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	/*
112*7c478bd9Sstevel@tonic-gate 	 * detach from tty here.
113*7c478bd9Sstevel@tonic-gate 	 */
114*7c478bd9Sstevel@tonic-gate 	setpgrp();
115*7c478bd9Sstevel@tonic-gate 	close(0);
116*7c478bd9Sstevel@tonic-gate 	close(1);
117*7c478bd9Sstevel@tonic-gate 	close(2);
118*7c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR);
119*7c478bd9Sstevel@tonic-gate 	(void) dup(0);
120*7c478bd9Sstevel@tonic-gate 	(void) dup(0);
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	for (;;) {
123*7c478bd9Sstevel@tonic-gate 		while ((fd = open(dev_fcode_file, O_RDONLY)) < 0) {
124*7c478bd9Sstevel@tonic-gate 			nerr++;
125*7c478bd9Sstevel@tonic-gate 			if (nerr == 1)
126*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Can't open %s: %s\n",
127*7c478bd9Sstevel@tonic-gate 				    dev_fcode_file, strerror(errno));
128*7c478bd9Sstevel@tonic-gate 			sleep(1);
129*7c478bd9Sstevel@tonic-gate 		}
130*7c478bd9Sstevel@tonic-gate 		if (nerr > 1) {
131*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Open on %s failed %d times\n",
132*7c478bd9Sstevel@tonic-gate 			    dev_fcode_file, nerr);
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 		nerr = 0;
135*7c478bd9Sstevel@tonic-gate 		nbytes = read(fd, &tc, sizeof (tc));
136*7c478bd9Sstevel@tonic-gate 		if (nbytes < 0) {
137*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Read of %s: %s\n", dev_fcode_file,
138*7c478bd9Sstevel@tonic-gate 			    strerror(errno));
139*7c478bd9Sstevel@tonic-gate 			close(fd);
140*7c478bd9Sstevel@tonic-gate 			continue;
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		if (debug)
143*7c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Got request\n");
144*7c478bd9Sstevel@tonic-gate 		while ((pid = fork()) < 0) {
145*7c478bd9Sstevel@tonic-gate 			nerr++;
146*7c478bd9Sstevel@tonic-gate 			if (nerr == 1)
147*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Fork failed: %s\n",
148*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
149*7c478bd9Sstevel@tonic-gate 			sleep(1);
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 		if ((nerr > 1) && pid) {
152*7c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Fork failed %d times\n", nerr);
153*7c478bd9Sstevel@tonic-gate 		}
154*7c478bd9Sstevel@tonic-gate 		nerr = 0;
155*7c478bd9Sstevel@tonic-gate 		if (pid) {
156*7c478bd9Sstevel@tonic-gate 			tpid = wait(&status);
157*7c478bd9Sstevel@tonic-gate 			if (tpid < 0)
158*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error: %s\n",
159*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
160*7c478bd9Sstevel@tonic-gate 			else if (pid != tpid)
161*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait error, expect pid: %d"
162*7c478bd9Sstevel@tonic-gate 				    " got %d, status: %x\n", pid, tpid, status);
163*7c478bd9Sstevel@tonic-gate 			else if (status)
164*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "Wait pid: %d status: %x\n",
165*7c478bd9Sstevel@tonic-gate 				    pid, status);
166*7c478bd9Sstevel@tonic-gate 			else if (debug)
167*7c478bd9Sstevel@tonic-gate 				syslog(LOG_DEBUG, "Wait: pid: %d\n", pid);
168*7c478bd9Sstevel@tonic-gate 			close(fd);
169*7c478bd9Sstevel@tonic-gate 			continue;
170*7c478bd9Sstevel@tonic-gate 		}
171*7c478bd9Sstevel@tonic-gate 		if (debug)
172*7c478bd9Sstevel@tonic-gate 			syslog(LOG_DEBUG, "Child: %d processing request\n",
173*7c478bd9Sstevel@tonic-gate 			    getpid());
174*7c478bd9Sstevel@tonic-gate 		fcntl(fd, F_DUP2FD, 0);
175*7c478bd9Sstevel@tonic-gate 		while (execl("/bin/sh", "sh", efcode_sh_file, NULL)) {
176*7c478bd9Sstevel@tonic-gate 			nerr++;
177*7c478bd9Sstevel@tonic-gate 			if (nerr == 1)
178*7c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "execl(/bin/sh) failed: %s\n",
179*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
180*7c478bd9Sstevel@tonic-gate 			sleep(1);
181*7c478bd9Sstevel@tonic-gate 		}
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	return (0);
185*7c478bd9Sstevel@tonic-gate }
186