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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
31 * This is a proxy daemon for the real Java slpd. This deamon starts
32 * at boot time, and listens for any incoming SLP messages only on
33 * loopback -- this way, only local processes can start the real
34 * daemon. When a message comes in, the proxy daemon dups the message
35 * fds onto fds 0, 1, and 2, and execs the real Java slpd. The purpose
36 * of this approach is for performance: boot time performance is
37 * not degraded by cranking off the (huge) JVM, and systems take
38 * the JVM resource hit only if they actually use SLP.
42 #include <sys/types.h>
43 #include <sys/socket.h>
46 #include <sys/byteorder.h>
47 #include <sys/resource.h>
48 #include <netinet/in.h>
55 /* This is an index which points into the args array at the conf file arg */
58 /* Location of the Java Virtual Machine */
59 #define JAVA_VM "/usr/java/jre/bin/java"
61 static char *slpd_args
[] = {
65 "/usr/share/lib/slp/slpd.jar",
73 * These are global so they can be easily accessed from a signal
74 * handler for cleanup.
82 if (execv(*slpd_args
, slpd_args
) == -1) {
83 openlog("slpd", LOG_PID
, LOG_DAEMON
);
84 syslog(LOG_ERR
, "execv failed: %s", strerror(errno
));
90 * If an alternate config file was specified with -f, make sure slpd
91 * uses that config file. Also, force libslp.so to use that new config
92 * file when checking to see if slpd is a DA. If any other arguments
93 * are given, they are ignored.
96 do_args(int argc
, char *const *argv
)
101 while ((c
= getopt(argc
, argv
, "f:")) != EOF
)
111 char *prefix
= "SLP_CONF_FILE=";
115 env_size
= strlen(prefix
) + strlen(conf
) + 1;
116 if ((conf_env
= malloc(env_size
)) == NULL
) {
117 syslog(LOG_ERR
, "no memory");
120 (void) strlcpy(conf_env
, prefix
, env_size
);
121 (void) strlcat(conf_env
, conf
, env_size
);
123 (void) putenv(conf_env
);
125 slpd_args
[CONF_INDEX
] = conf
;
130 detachfromtty(void) {
133 perror("slpd: can not fork");
143 * Close existing file descriptors, open "/dev/null" as
144 * standard input, output, and error, and detach from
145 * controlling terminal.
148 (void) open("/dev/null", O_RDONLY
);
149 (void) open("/dev/null", O_WRONLY
);
155 cleanup_and_exit(int retval
)
162 main(int argc
, char *const *argv
)
164 struct sockaddr_in bindaddr
;
167 const char *proxyReg
;
174 openlog("slpd", LOG_PID
, LOG_DAEMON
);
178 /* If slpd has been configured to run as a DA, start it and exit */
179 isDA
= SLPGetProperty("net.slp.isDA");
180 proxyReg
= SLPGetProperty("net.slp.serializedRegURL");
181 if ((isDA
&& (strcasecmp(isDA
, "true") == 0)) || proxyReg
) {
186 if ((lfd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0) {
187 syslog(LOG_ERR
, "socket failed: %s", strerror(errno
));
191 (void) setsockopt(lfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof (on
));
193 (void) memset((void *)&bindaddr
, 0, sizeof (bindaddr
));
194 bindaddr
.sin_family
= AF_INET
;
195 bindaddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
196 bindaddr
.sin_port
= htons(427);
198 if (bind(lfd
, (const struct sockaddr
*)&bindaddr
, sizeof (bindaddr
))
200 syslog(LOG_ERR
, "bind failed: %s", strerror(errno
));
204 if (listen(lfd
, 1) < 0) {
205 syslog(LOG_ERR
, "listen failed: %s", strerror(errno
));
209 addrlen
= sizeof (bindaddr
);
210 if ((connfd
= accept(lfd
, (struct sockaddr
*)&bindaddr
, &addrlen
))
212 syslog(LOG_ERR
, "accept failed: %s", strerror(errno
));
218 (void) dup2(connfd
, 0);
219 (void) close(connfd
);