2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: logger.c /main/3 1995/10/31 12:04:16 rswiston $ */
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
38 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
39 * (c) Copyright 1993, 1994 International Business Machines Corp. *
40 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
41 * (c) Copyright 1993, 1994 Novell, Inc. *
44 static void serve(char *progName, int s, int logfd);
47 main(int argc, char **argv)
54 struct sockaddr_in myaddr_in;
56 extern int optind, optopt;
58 while(EOF != (i1 = getopt(argc, argv, "fl:"))) {
66 open(optarg, O_WRONLY | O_APPEND | O_CREAT, 0644)) < 0) {
67 (void) perror(optarg);
75 #if defined(linux) || defined(sun) || defined(CSRG_BASED)
76 (void) memset((void *) &myaddr_in, (int) '\0', sizeof(myaddr_in));
78 (void) memset(myaddr_in, '\0', sizeof(myaddr_in));
81 /* set up my address for socket... */
82 myaddr_in.sin_family = AF_INET;
83 /* listen to the wildcard address... */
84 myaddr_in.sin_addr.s_addr = INADDR_ANY;
85 /* use port 4444... */
86 myaddr_in.sin_port = 4444;
88 /* create a socket... */
89 s = socket(AF_INET, SOCK_DGRAM, 0);
91 (void) perror("socket");
95 /* bind the servers addr to the socket... */
96 if (bind(s, (struct sockaddr *)&myaddr_in, sizeof(myaddr_in)) == -1) {
97 (void) perror("bind");
101 /* daemonize ourself... */
112 /* unable to fork... */
113 (void) perror("fork");
119 (void) serve(argv[0], s, logfd);
131 logStartStop(char *progName, int logfd, int start)
133 static char *savedProgName = (char *) 0;
134 static int savedLogfd = -1;
139 if (progName && *progName)
140 savedProgName = progName;
145 tstring = ctime(&now);
146 /* remove the trailing '\n'... */
147 tstring[strlen(tstring) - 1] = '\0';
149 (void) snprintf(buffer, sizeof buffer, "%s: %s %s\n",
150 (savedProgName && *savedProgName) ? savedProgName : "logger",
151 start ? "starting" : "terminating",
155 (void) write(savedLogfd, buffer, strlen(buffer));
161 (void) logStartStop(NULL, -1, 0);
165 serve(char *progName, int s, int logfd)
172 struct sockaddr_in clientaddr_in;
175 for (i = 0; i < 2; i++) {
176 if ((i != s) && (i != logfd)) {
181 (void) logStartStop(progName, logfd, 1);
182 (void) signal(SIGINT, shutDown);
183 (void) signal(SIGQUIT, shutDown);
184 (void) signal(SIGTERM, shutDown);
185 (void) signal(SIGUSR1, shutDown);
186 (void) signal(SIGUSR2, shutDown);
188 addrlen = sizeof(clientaddr_in);
190 (void) memset((char *) &clientaddr_in, 0, sizeof(clientaddr_in));
191 cc = recvfrom(s, buffer, sizeof(buffer) - 1, 0,
192 (struct sockaddr *)&clientaddr_in, &addrlen);
198 /* null term the buffer... */
201 hp = gethostbyaddr((char *) &clientaddr_in.sin_addr,
202 sizeof(clientaddr_in.sin_addr), clientaddr_in.sin_family);
206 c1 = inet_ntoa(clientaddr_in.sin_addr);
208 (void) write(logfd, c1, strlen(c1));
209 (void) write(logfd, ": ", 2);
211 (void) write(logfd, buffer, strlen(buffer));
212 (void) write(logfd, "\n", 1);
214 /* find the first space after the id... */
215 for (c1 = buffer; *c1 && (*c1 != ' ') && (*c1 != '\t'); c1++) {
218 (void) strcpy(c1, " ACK");
219 (void) sendto(s, buffer, strlen(buffer), 0, (struct sockaddr *)&clientaddr_in, addrlen);