Debian Codegraph @

utils/dioddate.c

raw
1 /*****************************************************************************
2 * Copyright (C) 2010-14 Lawrence Livermore National Security, LLC.
3 * Written by Jim Garlick <[email protected]> LLNL-CODE-423279
4 * All Rights Reserved.
5 *
6 * This file is part of the Distributed I/O Daemon (diod).
7 * For details, see http://code.google.com/p/diod.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the license, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * See also: http://www.gnu.org/licenses
23 *****************************************************************************/
24
25 /* dioddate.c - sync time with server */
26
27 #if HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <time.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #if HAVE_GETOPT_H
41 #include <getopt.h>
42 #endif
43 #include <string.h>
44 #include <errno.h>
45 #include <ctype.h>
46 #include <libgen.h>
47 #include <signal.h>
48
49 #include "9p.h"
50 #include "npfs.h"
51 #include "npclient.h"
52
53 #include "list.h"
54 #include "diod_log.h"
55 #include "diod_sock.h"
56 #include "diod_auth.h"
57
58 #define OPTIONS "s:m:u:t:S"
59 #if HAVE_GETOPT_LONG
60 #define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL)
61 static const struct option longopts[] = {
62 {"server", required_argument, 0, 's'},
63 {"msize", required_argument, 0, 'm'},
64 {"uid", required_argument, 0, 'u'},
65 {"timeout", required_argument, 0, 't'},
66 {"set-time",no_argument, 0, 'S'},
67 {0, 0, 0, 0},
68 };
69 #else
70 #define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt)
71 #endif
72
73 static void sigalarm (int arg);
74
75 static void
76 usage (void)
77 {
78 fprintf (stderr,
79 "Usage: dioddate [OPTIONS] [-s HOST[:PORT]]\n"
80 " -S,--set-time set local time to match server's\n"
81 " -s,--server HOST:PORT server (default localhost:564)\n"
82 " -m,--msize msize (default 65536)\n"
83 " -u,--uid authenticate as uid (default is your euid)\n"
84 " -t,--timeout SECS give up after specified seconds\n"
85 );
86 exit (1);
87 }
88
89 int
90 main (int argc, char *argv[])
91 {
92 char *aname = "ctl";
93 char *server = NULL;
94 int msize = 65536;
95 uid_t uid = geteuid ();
96 int topt = 0;
97 int Sopt = 0;
98 int flags = 0;
99 int fd, c;
100 Npcfsys *fs = NULL;
101 Npcfid *afid = NULL, *root = NULL;
102 char *buf = NULL;
103 struct timeval tv;
104 struct timezone tz;
105
106 diod_log_init (argv[0]);
107
108 opterr = 0;
109 while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) {
110 switch (c) {
111 case 's': /* --server HOST[:PORT] or /path/to/socket */
112 server = optarg;
113 break;
114 case 'm': /* --msize SIZE */
115 msize = strtoul (optarg, NULL, 10);
116 break;
117 case 'u': /* --uid UID */
118 uid = strtoul (optarg, NULL, 10);
119 break;
120 case 't': /* --timeout SECS */
121 topt = strtoul (optarg, NULL, 10);
122 break;
123 case 'S': /* --set-time */
124 Sopt = 1;
125 break;
126 default:
127 usage ();
128 }
129 }
130
131 if (signal (SIGPIPE, SIG_IGN) == SIG_ERR)
132 err_exit ("signal");
133 if (signal (SIGALRM, sigalarm) == SIG_ERR)
134 err_exit ("signal");
135
136 if (topt > 0)
137 alarm (topt);
138
139 if ((fd = diod_sock_connect (server, flags)) < 0)
140 exit (1);
141
142 if (!(fs = npc_start (fd, fd, msize, 0))) {
143 errn (np_rerror (), "error negotiating protocol with server");
144 goto done;
145 }
146 if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) {
147 errn (np_rerror (), "error authenticating to server");
148 goto done;
149 }
150 if (!(root = npc_attach (fs, afid, aname, uid))) {
151 errn (np_rerror (), "error attaching to aname='%s'", aname);
152 goto done;
153 }
154 buf = npc_aget (root, "date");
155 if (!buf) {
156 errn (np_rerror (), "error reading date");
157 goto done;
158 }
159 if (sscanf (buf, "%lu.%lu %d.%d", &tv.tv_sec, &tv.tv_usec,
160 &tz.tz_minuteswest, &tz.tz_dsttime) != 4) {
161 msg ("error scanning returned date: %s", buf);
162 goto done;
163 }
164 if (Sopt) {
165 if (settimeofday (&tv, &tz) < 0)
166 err_exit ("settimeofday");
167 } else {
168 time_t t = tv.tv_sec;
169 printf ("%s", ctime (&t));
170 }
171 done:
172 if (buf)
173 free (buf);
174 if (root && npc_clunk (root) < 0)
175 errn_exit (np_rerror (), "error clunking %s", aname);
176 if (afid && npc_clunk (afid) < 0)
177 errn_exit (np_rerror (), "error clunking afid");
178 if (fs)
179 npc_finish (fs);
180 close (fd);
181
182 diod_log_fini ();
183
184 exit (0);
185 }
186
187 static void sigalarm (int arg)
188 {
189 msg_exit ("timed out");
190 }
191
192 /*
193 * vi:tabstop=4 shiftwidth=4 expandtab
194 */