xref: /illumos-gate/usr/src/cmd/stat/common/common.c (revision 7a088f03b431bdffa96c3b2175964d4d38420caa)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include "statcommon.h"
27 
28 #include <stdarg.h>
29 #include <signal.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <langinfo.h>
34 
35 extern char *cmdname;
36 extern int caught_cont;
37 
38 uint_t timestamp_fmt = NODATE;
39 
40 /*PRINTFLIKE2*/
41 void
42 fail(int do_perror, char *message, ...)
43 {
44 	va_list args;
45 	int save_errno = errno;
46 
47 	va_start(args, message);
48 	(void) fprintf(stderr, "%s: ", cmdname);
49 	(void) vfprintf(stderr, message, args);
50 	va_end(args);
51 	if (do_perror)
52 		(void) fprintf(stderr, ": %s", strerror(save_errno));
53 	(void) fprintf(stderr, "\n");
54 	exit(2);
55 }
56 
57 /*
58  * Sleep until *wakeup + interval, keeping cadence where desired
59  *
60  * *wakeup -	The time we last wanted to wake up. Updated.
61  * interval -	We want to sleep until *wakeup + interval
62  * forever -	Running for infinite periods, so cadence not important
63  * *caught_cont - Global set by signal handler if we got a SIGCONT
64  */
65 void
66 sleep_until(hrtime_t *wakeup, hrtime_t interval, int forever,
67     int *caught_cont)
68 {
69 	hrtime_t now, pause, pause_left;
70 	struct timespec pause_tv;
71 	int status;
72 
73 	now = gethrtime();
74 	pause = *wakeup + interval - now;
75 
76 	if (pause <= 0 || pause < (interval / 4))
77 		if (forever || *caught_cont) {
78 			/* Reset our cadence (see comment below) */
79 			*wakeup = now + interval;
80 			pause = interval;
81 		} else {
82 			/*
83 			 * If we got here, then the time between the
84 			 * output we just did, and the scheduled time
85 			 * for the next output is < 1/4 of our requested
86 			 * interval AND the number of intervals has been
87 			 * requested AND we have never caught a SIGCONT
88 			 * (so we have never been suspended).  In this
89 			 * case, we'll try to stay to the desired
90 			 * cadence, and we will pause for 1/2 the normal
91 			 * interval this time.
92 			 */
93 			pause = interval / 2;
94 			*wakeup += interval;
95 		}
96 	else
97 		*wakeup += interval;
98 	if (pause < 1000)
99 		/* Near enough */
100 		return;
101 
102 	/* Now do the actual sleep */
103 	pause_left = pause;
104 	do {
105 		pause_tv.tv_sec = pause_left / NANOSEC;
106 		pause_tv.tv_nsec = pause_left % NANOSEC;
107 		status = nanosleep(&pause_tv, (struct timespec *)NULL);
108 		if (status < 0)
109 			if (errno == EINTR) {
110 				now = gethrtime();
111 				pause_left = *wakeup - now;
112 				if (pause_left < 1000)
113 					/* Near enough */
114 					return;
115 			} else {
116 				fail(1, "nanosleep failed");
117 			}
118 	} while (status != 0);
119 }
120 
121 /*
122  * Signal handler - so we can be aware of SIGCONT
123  */
124 void
125 cont_handler(int sig_number)
126 {
127 	/* Re-set the signal handler */
128 	(void) signal(sig_number, cont_handler);
129 	caught_cont = 1;
130 }
131 
132 /*
133  * Print timestamp as decimal reprentation of time_t value (-T u was specified)
134  * or in date(1) format (-T d was specified).
135  */
136 void
137 print_timestamp(void)
138 {
139 	time_t t = time(NULL);
140 	static char *fmt = NULL;
141 
142 	/* We only need to retrieve this once per invocation */
143 	if (fmt == NULL)
144 		fmt = nl_langinfo(_DATE_FMT);
145 
146 	if (timestamp_fmt == UDATE) {
147 		(void) printf("%ld\n", t);
148 	} else if (timestamp_fmt == DDATE) {
149 		char dstr[64];
150 		int len;
151 
152 		len = strftime(dstr, sizeof (dstr), fmt, localtime(&t));
153 		if (len > 0)
154 			(void) printf("%s\n", dstr);
155 	}
156 }
157