xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.lib/in.timed/in.timed.c (revision 0fb96ba1f1ce26ff8b286f8f928769a6afcb00a6)
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 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * time inetd service - both stream and dgram based.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <netinet/in.h>
36 #include <sys/time.h>
37 #include <syslog.h>
38 #include <inetsvc.h>
39 
40 
41 /*
42  * Return a machine readable date and time, in the form of the
43  * number of seconds since midnight, Jan 1, 1900.  Since gettimeofday
44  * returns the number of seconds since midnight, Jan 1, 1970,
45  * we must add 2208988800 seconds to this figure to make up for
46  * some seventy years Bell Labs was asleep.
47  */
48 static uint32_t
49 machtime(void)
50 {
51 	struct timeval tv;
52 
53 	if (gettimeofday(&tv, NULL) < 0) {
54 		syslog(LOG_INFO, "Unable to get time of day");
55 		return (0);
56 	}
57 	return ((uint32_t)htonl(tv.tv_sec + 2208988800U));
58 }
59 
60 static void
61 machtime_stream(int s)
62 {
63 	uint32_t result = machtime();
64 
65 	(void) safe_write(s, &result, sizeof (result));
66 }
67 
68 /* ARGSUSED3 */
69 static void
70 machtime_dg(int s, const struct sockaddr *sap, int sa_len, const void *buf,
71     size_t sz)
72 {
73 	uint32_t result = machtime();
74 
75 	(void) safe_sendto(s, &result, sizeof (result), 0, sap, sa_len);
76 }
77 
78 int
79 main(int argc, char *argv[])
80 {
81 	opterr = 0;	/* disable getopt error msgs */
82 	switch (getopt(argc, argv, "ds")) {
83 	case 'd':
84 		dg_template(machtime_dg, STDIN_FILENO, NULL, 0);
85 		break;
86 	case 's':
87 		machtime_stream(STDIN_FILENO);
88 		break;
89 	default:
90 		return (1);
91 	}
92 
93 	return (0);
94 }
95