ntpd: add -q option. By Adam Tkac.

function                                             old     new   delta
settime                                              347     375     +28
packed_usage                                       26681   26693     +12
ntpd_main                                           3282    3251     -31
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 40/-31)              Total: 9 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2009-11-23 16:27:16 +01:00
parent 74f8e7810b
commit 8d580c72f4
2 changed files with 30 additions and 31 deletions

View File

@ -3211,13 +3211,14 @@
"Address: 127.0.0.1\n" "Address: 127.0.0.1\n"
#define ntpd_trivial_usage \ #define ntpd_trivial_usage \
"[-dngl] [-p PEER]..." "[-dngql] [-p PEER]..."
#define ntpd_full_usage "\n\n" \ #define ntpd_full_usage "\n\n" \
"NTP client/server\n" \ "NTP client/server\n" \
"\nOptions:" \ "\nOptions:" \
"\n -d Verbose" \ "\n -d Verbose" \
"\n -n Do not daemonize" \ "\n -n Do not daemonize" \
"\n -g Set system time even if offset is > 1000 sec" \ "\n -g Set system time even if offset is > 1000 sec" \
"\n -q Quit after clock is set" \
"\n -l Run as server on port 123" \ "\n -l Run as server on port 123" \
"\n -p PEER Obtain time from PEER (may be repeated)" \ "\n -p PEER Obtain time from PEER (may be repeated)" \

View File

@ -152,6 +152,16 @@ typedef struct {
uint8_t trustlevel; uint8_t trustlevel;
} ntp_peer_t; } ntp_peer_t;
enum {
OPT_n = (1 << 0),
OPT_g = (1 << 1),
OPT_q = (1 << 2),
/* Insert new options above this line. */
/* Non-compat options: */
OPT_p = (1 << 3),
OPT_l = (1 << 4),
};
struct globals { struct globals {
unsigned verbose; unsigned verbose;
@ -164,7 +174,6 @@ struct globals {
uint8_t settime; uint8_t settime;
uint8_t firstadj; uint8_t firstadj;
smallint peer_cnt; smallint peer_cnt;
}; };
#define G (*ptr_to_globals) #define G (*ptr_to_globals)
@ -468,14 +477,14 @@ settime(double offset)
char buf[80]; char buf[80];
time_t tval; time_t tval;
#if 0
if (!G.settime) if (!G.settime)
return; goto bail;
#endif
G.settime = 0;
/* if the offset is small, don't call settimeofday */ /* if the offset is small, don't call settimeofday */
if (offset < SETTIME_MIN_OFFSET && offset > -SETTIME_MIN_OFFSET) if (offset < SETTIME_MIN_OFFSET && offset > -SETTIME_MIN_OFFSET)
return; goto bail;
gettimeofday(&curtime, NULL); /* never fails */ gettimeofday(&curtime, NULL); /* never fails */
@ -486,11 +495,9 @@ settime(double offset)
if (settimeofday(&curtime, NULL) == -1) { if (settimeofday(&curtime, NULL) == -1) {
bb_error_msg("settimeofday"); bb_error_msg("settimeofday");
return; goto bail;
} }
G.settime = 0;
tval = curtime.tv_sec; tval = curtime.tv_sec;
strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval)); strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval));
@ -504,6 +511,10 @@ settime(double offset)
if (p->deadline) if (p->deadline)
p->deadline -= offset; p->deadline -= offset;
} }
bail:
if (option_mask32 & OPT_q)
exit(0);
} }
static void static void
@ -669,7 +680,6 @@ client_dispatch(ntp_peer_t *p)
offset->offset, offset->delay, (int) interval); offset->offset, offset->delay, (int) interval);
client_update(p); client_update(p);
if (!G.settime)
settime(offset->offset); settime(offset->offset);
if (++p->shift >= OFFSET_ARRAY_SIZE) if (++p->shift >= OFFSET_ARRAY_SIZE)
@ -817,13 +827,6 @@ server_dispatch(int fd)
* Note: The kernel time discipline is disabled with this option. * Note: The kernel time discipline is disabled with this option.
*/ */
enum {
OPT_n = (1 << 0),
OPT_g = (1 << 1),
OPT_p = (1 << 2),
OPT_l = (1 << 3),
};
/* By doing init in a separate function we decrease stack usage /* By doing init in a separate function we decrease stack usage
* in main loop. * in main loop.
*/ */
@ -840,7 +843,7 @@ static NOINLINE void ntp_init(char **argv)
peers = NULL; peers = NULL;
opt_complementary = "dd:p::"; /* d: counter, p: list */ opt_complementary = "dd:p::"; /* d: counter, p: list */
opts = getopt32(argv, opts = getopt32(argv,
"ng" /* compat */ "ngq" /* compat */
"p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */ "p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
"d" /* compat */ "d" /* compat */
"46aAbLNx", /* compat, ignored */ "46aAbLNx", /* compat, ignored */
@ -891,7 +894,6 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
int ntpd_main(int argc UNUSED_PARAM, char **argv) int ntpd_main(int argc UNUSED_PARAM, char **argv)
{ {
struct globals g; struct globals g;
unsigned new_cnt;
struct pollfd *pfd; struct pollfd *pfd;
ntp_peer_t **idx2peer; ntp_peer_t **idx2peer;
@ -900,13 +902,12 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
ntp_init(argv); ntp_init(argv);
new_cnt = g.peer_cnt; {
unsigned new_cnt = g.peer_cnt;
idx2peer = xzalloc(sizeof(void *) * new_cnt); idx2peer = xzalloc(sizeof(void *) * new_cnt);
#if ENABLE_FEATURE_NTPD_SERVER /* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */
if (g.listen_fd != -1) pfd = xzalloc(sizeof(pfd[0]) * (new_cnt + ENABLE_FEATURE_NTPD_SERVER));
new_cnt++; }
#endif
pfd = xzalloc(sizeof(pfd[0]) * new_cnt);
while (!bb_got_signal) { while (!bb_got_signal) {
llist_t *item; llist_t *item;
@ -964,11 +965,8 @@ int ntpd_main(int argc UNUSED_PARAM, char **argv)
} }
} }
if (g.settime if ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
&& ((trial_cnt > 0 && sent_cnt == 0) || g.peer_cnt == 0)
) {
settime(0); /* no good peers, don't wait */ settime(0); /* no good peers, don't wait */
}
timeout = nextaction - time(NULL); timeout = nextaction - time(NULL);
if (timeout < 0) if (timeout < 0)