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 librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: DtTermLogit.c /main/6 1998/07/23 18:08:59 mgreess $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
39 #include <sys/times.h>
42 #include <sys/utsname.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
49 #define LOG_HOST "hpcvusj.cv.hp.com"
50 #define LOG_ADDR "15.0.209.35"
51 #define LOG_PORT "4444"
52 #define WAIT_INTERVAL 5
59 #define logitmain main
60 #endif /* DEBUG_LOGIT */
63 void LogStart(int noFork, int argc, char **argv);
64 void LogFinish(int noFork, int sessions);
65 void LogBumpSessionCount(int count);
66 static void doLog(int noFork, char *msg);
67 static void ding(int sig);
68 static void autoLogFinish();
70 static time_t startTime;
71 static char *id = (char *) 0;
72 static int sessionCount = 0;
73 static int savedNoFork = 0;
74 static int sequence = 0;
77 LogBumpSessionCount(int count)
79 sessionCount += count;
83 LogStart(int noFork, int argc, char **argv)
93 if (getenv("DTTERM_NOLOG")) {
97 /* save away noFork... */
101 (void) strcat(buffer, "START");
102 (void) strcat(buffer, " USER=\"");
103 if (c1 = getlogin()) {
104 (void) strcat(buffer, c1);
106 if ((pw = getpwuid(getuid())) && pw->pw_name && *pw->pw_name) {
107 (void) strcat(buffer, pw->pw_name);
109 (void) strcat(buffer, "???");
113 (void) strcat(buffer, "\"" );
114 if (uname(&uts) != -1) {
115 (void) strcat(buffer, " UNAME=\"");
116 (void) strcat(buffer, uts.sysname);
117 (void) strcat(buffer, " ");
118 (void) strcat(buffer, uts.nodename);
119 (void) strcat(buffer, " ");
120 (void) strcat(buffer, uts.release);
121 (void) strcat(buffer, " ");
122 (void) strcat(buffer, uts.version);
123 (void) strcat(buffer, " ");
124 (void) strcat(buffer, uts.machine);
125 (void) strcat(buffer, "\"");
128 (void) time(&startTime);
129 tstring = ctime(&startTime);
130 /* remove the trailing '\n'... */
131 tstring[strlen(tstring) - 1] = '\0';
132 (void) strcat(buffer, " TIME=\"");
133 (void) strcat(buffer, tstring);
134 (void) strcat(buffer, "\"");
136 (void) strcat(buffer, " ARGS=\"");
137 c1 = buffer + strlen(buffer);
138 for (i1 = 0; i1 < argc; i1++) {
143 for (c2 = argv[i1]; *c2; c2++) {
147 } else if (isprint(*c2)) {
162 *c1++ = '0' + (*c2 / 0100) & 07;
163 *c1++ = '0' + (*c2 / 0010) & 07;
164 *c1++ = '0' + (*c2 / 0001) & 07;
171 (void) doLog(noFork, buffer);
172 atexit(autoLogFinish);
176 LogFinish(int noFork, int sessions)
179 char buffer2[BUFSIZ];
186 if (getenv("DTTERM_NOLOG")) {
191 (void) strcat(buffer, "FINISH");
193 (void) sprintf(buffer2, " SESSIONS=\"%d\"", sessions);
194 (void) strcat(buffer, buffer2);
197 (void) sprintf(buffer2, " ELAPSED=\"%ld\"", now - startTime);
198 (void) strcat(buffer, buffer2);
202 clkTick = sysconf(_SC_CLK_TCK);
204 cpuTime = (tms.tms_utime = tms.tms_stime) / clkTick;
209 (void) sprintf(buffer2, " CPU=\"%ld\"", cpuTime);
210 (void) strcat(buffer, buffer2);
212 (void) doLog(noFork, buffer);
218 (void) LogFinish(savedNoFork, sessionCount);
222 doLog(int noFork, char *msg)
225 char rbuffer[BUFSIZ];
231 int retries = RETRIES;
233 static struct sockaddr_in myaddr_in;
234 static struct sockaddr_in servaddr_in;
236 struct sigaction oldSa;
241 sbuffer = malloc(2*BUFSIZ);
244 (void) sprintf(sbuffer, "%ld.%ld", now, (long)getpid());
245 id = strdup(sbuffer);
248 /* bump the sequence number before we fork()... */
262 (void) memset(&servaddr_in, '\0', sizeof(servaddr_in));
263 (void) memset(&myaddr_in, '\0', sizeof(myaddr_in));
265 /* set up the server address... */
266 servaddr_in.sin_family = AF_INET;
267 hp = gethostbyname((c1 = getenv("DTTERM_LOG_HOST")) ? c1 : LOG_HOST);
270 servaddr_in.sin_addr.s_addr =
271 ((struct in_addr *)(hp->h_addr))->s_addr;
273 servaddr_in.sin_addr.s_addr =
274 inet_addr((c1 = getenv("DTTERM_LOG_ADDR")) ? c1 : LOG_ADDR);
277 servaddr_in.sin_port =
278 atoi((c1 = getenv("DTTERM_LOG_PORT")) ? c1 : LOG_PORT);
280 /* create the socket... */
281 s = socket(AF_INET, SOCK_DGRAM, 0);
289 /* bind to some local address so we can get a reply... */
290 myaddr_in.sin_family = AF_INET;
291 myaddr_in.sin_port = 0;
292 myaddr_in.sin_addr.s_addr = INADDR_ANY;
293 if (bind(s, &myaddr_in, sizeof(myaddr_in)) == -1) {
304 for (i1 = 0; i1 < _NFILE; i1++) {
311 (void) sprintf(thisId, "ID=\"%s.%d\"", id, sequence);
312 (void) sprintf(sbuffer, "%s %s", thisId, msg);
314 /* set up a signal handler... */
315 (void) sigemptyset(&sa.sa_mask);
317 sa.sa_handler = ding;
319 (void) sigaction(SIGALRM, &sa, &oldSa);
321 /* try to send the message... */
322 while (retries > 0) {
323 if (sendto(s, sbuffer, strlen(sbuffer), 0, &servaddr_in,
324 sizeof(servaddr_in)) < 0) {
333 /* set a timeout... */
334 (void) alarm(WAIT_INTERVAL);
336 for (doRead = 1; doRead; ) {
337 if ((len = recv(s, rbuffer, sizeof(rbuffer) -1, 0)) < 0) {
338 if (errno == EINTR) {
350 /* we need to resend before doing another read... */
366 /* null term the string... */
369 /* compare it against the id... */
370 if (!strncmp(rbuffer, thisId, strlen(thisId))) {
376 /* ignore it and get the next one... */
381 /* clear the alarm and re-install the old signal handler... */
383 (void) sigaction(SIGALRM, &oldSa, (struct sigaction *) 0);
394 /* since we are using sigaction, we don't need to reinstall
401 logitmain(int argc, char **argv)
406 extern int optind, optopt;
408 while(EOF != (i1 = getopt(argc, argv, "f"))) {
416 (void) LogStart(noFork, argc, argv);
419 (void) LogFinish(noFork, 123);
422 #endif /* DEBUG_LOGIT */
423 #endif /* LOG_USAGE */