xref: /illumos-gate/usr/src/cmd/rpcsvc/rwall_subr.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
27 /*
28  * University Copyright- Copyright (c) 1982, 1986, 1988
29  * The Regents of the University of California
30  * All Rights Reserved
31  *
32  * University Acknowledgment- Portions of this document are derived from
33  * software developed by the University of California, Berkeley, and its
34  * contributors.
35  */
36 
37 /*
38  * rwall_subr.c
39  *	The server procedure for rwalld
40  */
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <rpc/rpc.h>
46 #include <thread.h>
47 #include <rpcsvc/rwall.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <syslog.h>
51 #include <string.h>
52 
53 #define	WALL_PROG	"/usr/sbin/wall"
54 
55 static mutex_t wall_mutex = DEFAULTMUTEX;
56 static char *oldmsg;
57 
58 /* ARGSUSED */
59 bool_t
60 wallproc_wall_1_svc(wrapstring *argp, void *res, struct svc_req *rqstp)
61 {
62 	char *msg;
63 	FILE *fp;
64 	int rval;
65 	struct stat wall;
66 
67 	msg = *argp;
68 
69 	/*
70 	 * Do not wall the same message twice in case of a retransmission
71 	 * in the rare case that two walls arrive close enough with
72 	 * a retransmission we might get a duplicate, but that is OK.
73 	 */
74 	(void) mutex_lock(&wall_mutex);
75 	if ((oldmsg != 0) && (strcmp(msg, oldmsg) == 0)) {
76 		(void) mutex_unlock(&wall_mutex);
77 		return (TRUE);
78 	}
79 
80 	if (oldmsg)
81 		free(oldmsg);
82 	oldmsg = strdup(msg);
83 
84 	rval = stat(WALL_PROG, &wall);
85 
86 	/*
87 	 * Make sure the wall programs exists, is executeable, and runs
88 	 */
89 	if (rval == -1 || (wall.st_mode & S_IXUSR) == 0 ||
90 	    (fp = popen(WALL_PROG, "w")) == NULL) {
91 		syslog(LOG_NOTICE,
92 			"rwall message received but could not execute %s",
93 			WALL_PROG);
94 		syslog(LOG_NOTICE, "%s", msg);
95 #ifdef	DEBUG
96 		(void) fprintf(stderr,
97 			"rwall message received but could not execute %s",
98 			WALL_PROG);
99 		(void) fprintf(stderr, "%s", msg);
100 #endif
101 		(void) mutex_unlock(&wall_mutex);
102 		return (TRUE);
103 	}
104 
105 	(void) fprintf(fp, "%s", msg);
106 	(void) pclose(fp);
107 	(void) mutex_unlock(&wall_mutex);
108 
109 	return (TRUE);
110 }
111 
112 /* ARGSUSED */
113 int
114 wallprog_1_freeresult(SVCXPRT *transp, xdrproc_t proc, caddr_t arg)
115 {
116 	return (TRUE);
117 }
118