-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync-periodically.c
424 lines (389 loc) · 13.1 KB
/
sync-periodically.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// file sync-periodically.c in github.com/bstarynk/misc-basile/
/* Copyright 2020 - 2022 Basile Starynkevitch
This sync-periodically program is free software for Linux; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2, or (at your option) any later version.
The sync-periodically program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
The purpose of sync-periodically is to regularily call the sync(2)
system call to avoid losing too much data on a Linux desktop with
64Gbytes of RAM.
See of course https://man7.org/linux/man-pages/man2/sync.2.html
--
*/
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stdatomic.h>
#include <argp.h>
#ifndef SYNPER_GITID
#error compilation command should define SYNPER_GITID
/*** for example in our Makefile:
GIT_ID=$(shell git log --format=oneline -q -1 | cut -c1-10)
***/
#endif
char *synper_progname;
char *synper_pidfile;
char *synper_name;
char synper_selfexe[256];
char synper_host[128];
#define SYNPER_MIN_PERIOD 2 /*minimal sync period, in seconds */
#define SYNPER_MAX_PERIOD 30 /*maximal sync period, in seconds */
/// see http://man7.org/linux/man-pages/man2/sync.2.html
// period for sync(2) in seconds
int synper_period =
(SYNPER_MAX_PERIOD - SYNPER_MIN_PERIOD) / 4 + SYNPER_MIN_PERIOD;
int synper_logperiod = 2000; // period for syslog(3) in seconds;,
volatile atomic_bool synper_stop;
bool synper_daemonized;
#define SYNPER_MIN_LOGPERIOD 100 /*minimal log period, in seconds */
#define SYNPER_MAX_LOGPERIOD 7200 /*maximal log period, in seconds */
void synper_set_signal_handlers (void);
void synper_fatal_at (const char *file, int lin) __attribute__((noreturn));
#define SYNPER_FATAL_AT_BIS(Fil,Lin,Fmt,...) do { \
syslog(LOG_CRIT, "SYNPER FATAL:%s:%d: <%s>\n " Fmt "- %m\n\n", \
Fil, Lin, __func__, ##__VA_ARGS__); \
synper_fatal_at(Fil,Lin); } while(0)
#define SYNPER_FATAL_AT(Fil,Lin,Fmt,...) SYNPER_FATAL_AT_BIS(Fil,Lin,Fmt,##__VA_ARGS__)
#define SYNPER_FATAL(Fmt,...) SYNPER_FATAL_AT(__FILE__,__LINE__,Fmt,##__VA_ARGS__)
#define SYNPER_STRINGIFY_BIS(Arg) #Arg
#define SYNPER_STRINGIFY(Arg) SYNPER_STRINGIFY_BIS(Arg)
struct argp_option synper_options[] = {
/*name, key, arg, flag, doc, group */
{"pid-file", 'P', "FILE", 0,
"write pid to file, ensuring no other process is running", 0},
{"name", 'N', "NAME", 0, "set NAME for logging", 0},
{"sync-period", 'Y', "SYNC-PERIOD", 0,
"call sync(2) every SYNC-PERIOD seconds", 0},
{"log-period", 'L', "LOG-PERIOD", 0,
"do a syslog(3) every LOG-PERIOD seconds", 0},
{"daemon", 'd', NULL, 0, "run as a daemon(3)", 0},
{"version", 'V', NULL, 0, "show version info", 0},
{NULL, 0, NULL, 0, NULL, 0}
};
error_t synper_parse_opt (int key, char *arg, struct argp_state *state);
void synper_signal_handler (int signum __attribute__((unused)));
void synper_syslog_begin (void);
void
synper_signal_handler (int signum __attribute__((unused)))
{
atomic_store (&synper_stop, true);
} /* end synper_signal_handler */
void
synper_fatal_at (const char *file, int lin)
{
syslog (LOG_CRIT, "SYNPER FATAL %s:%d", file, lin);
fflush (NULL);
abort ();
} /* end synper_fatal_at */
error_t
synper_parse_opt (int key, char *arg, struct argp_state *state)
{
if (state == NULL)
SYNPER_FATAL ("null arpg_state key:%c=%d arg=%s", (char) key, key,
arg ? arg : "??");
switch (key)
{
case 'd': // --daemon
{
if (daemon (0, /*no-close: */ 1))
{
int olderr = errno;
SYNPER_FATAL ("failed to daemon(3) pid %ld : %s",
(long) getpid (), strerror (olderr));
}
synper_daemonized = true;
synper_set_signal_handlers ();
};
return 0;
case 'P': // --pid-file
{
synper_pidfile = arg;
}
return 0;
case 'Y': // --sync-period
{
synper_period = atoi (arg ? : "");
}
return 0;
case 'N': // --name
{
synper_name = arg;;
}
return 0;
case 'L': // --log-period
{
synper_logperiod = atoi (arg ? : "");
}
return 0;
case 'V': // --version
{
printf ("%s gitid %s built on %s (executable %s)\n", synper_progname,
SYNPER_STRINGIFY (SYNPER_GITID), __DATE__, synper_selfexe);
printf ("\t run as: '%s --help' to get help.\n", synper_progname);
fflush (NULL);
exit (EXIT_SUCCESS);
}
return -1;
}
return ARGP_ERR_UNKNOWN;
} /* end synper_parse_opt */
void
synper_set_signal_handlers (void)
{
struct sigaction sa;
memset (&sa, 0, sizeof (sa));
sa.sa_handler = synper_signal_handler;
if (sigaction (SIGTERM, &sa, NULL))
SYNPER_FATAL ("failed to install SIGTERM action");
if (sigaction (SIGQUIT, &sa, NULL))
SYNPER_FATAL ("failed to install SIGQUIT action");
} /* end synper_set_signal_handlers */
void
synper_syslog_begin (void)
{
time_t nowt = 0;
time (&nowt);
struct tm nowtm;
memset (&nowtm, 0, sizeof (nowtm));
if (localtime_r (&nowt, &nowtm) == NULL)
SYNPER_FATAL ("failed to get localtime");
char nowbuf[80];
memset (nowbuf, 0, sizeof (nowbuf));
char parentexe[128];
memset (parentexe, 0, sizeof (parentexe));
if (strftime (nowbuf, sizeof (nowbuf), "%Y/%b/%d %T %Z", &nowtm) <= 0)
SYNPER_FATAL ("failed to strftime");
pid_t parentpid = getppid ();
if (parentpid > 0)
{
char parentprocx[64];
memset (parentprocx, 0, sizeof (parentprocx));
snprintf (parentprocx, sizeof (parentprocx), "/proc/%d/exe",
(int) parentpid);
if (readlink (parentprocx, parentexe, sizeof (parentexe) - 1) < 0) {
syslog (LOG_WARNING, "%s failed to readlink from %s (%s)",
synper_progname, parentprocx, strerror(errno));
};
}
else /// according to man page getppid(2) never fails
SYNPER_FATAL ("failed to getppid");
if (synper_name)
syslog (LOG_INFO,
"start of %s named %s git %s build %s\n"
"... with sync period %d seconds"
" and log period %d seconds\n"
"... at %s, pid %d, parentpid %d running %s\n",
synper_progname, synper_name, SYNPER_STRINGIFY (SYNPER_GITID),
__DATE__, synper_period, synper_logperiod, nowbuf,
(int) getpid (), (int) parentpid, parentexe);
else
syslog (LOG_INFO,
"start of %s git %s build %s\n"
"... with sync period %d seconds"
" and log period %d seconds\n"
"... at %s, pid %d, parentpid %d running %s\n",
synper_progname, SYNPER_STRINGIFY (SYNPER_GITID), __DATE__,
synper_period, synper_logperiod, nowbuf,
(int) getpid (), (int) parentpid, parentexe);
if (synper_daemonized)
syslog (LOG_NOTICE, "%s git %s daemonized as pid %ld",
synper_progname, SYNPER_STRINGIFY (SYNPER_GITID),
(long) getpid ());
} /* end synper_syslog_begin */
void
synper_syslog_final (void)
{
syslog (LOG_INFO, "%s git %s ending pid %ld",
synper_progname, SYNPER_STRINGIFY (SYNPER_GITID), (long) getpid ());
} /* end synper_syslog_final */
int
main (int argc, char **argv)
{
synper_progname = argv[0];
if (gethostname (synper_host, sizeof (synper_host)))
{
SYNPER_FATAL ("%s git %s failed to gethostname (%m)",
synper_progname, SYNPER_STRINGIFY (SYNPER_GITID));
}
char myselfexe[sizeof (synper_selfexe)];
memset (myselfexe, 0, sizeof (myselfexe));
{
if (readlink ("/proc/self/exe", myselfexe, sizeof (myselfexe)) > 0
&& myselfexe[sizeof (myselfexe) - 1] == (char) 0
&& myselfexe[0] != (char) 0)
strcpy (synper_selfexe, myselfexe);
else
{
SYNPER_FATAL
("%s failed to readlink /proc/self/exe git %s pid %ld on %s",
synper_progname, SYNPER_STRINGIFY (SYNPER_GITID), (long) getpid (),
synper_host);
}
}
/// sleep a small amount of time to let other processes run....
usleep (2000 + ((int) getpid ()) % 1024);
struct argp argp = { synper_options, synper_parse_opt, "",
"Utility to call sync(2) periodically. GPLv3+ licensed.\n"
"See www.gnu.org/licenses/ for details.\n"
"Source " __FILE__ " on github.com/bstarynk/misc-basile/\n"
"Build on " __DATE__ " git " SYNPER_GITID "\n" "Program options:\n",
/*children: */ NULL,
/*help_filter: */ NULL,
/*argp_domain: */ NULL
};
argp_parse (&argp, argc, argv, 0, 0, NULL); // could run daemon(3)
openlog ("synper", LOG_PID | LOG_NDELAY | LOG_CONS, LOG_DAEMON);
atexit (synper_syslog_final);
time_t nowt = 0;
char timbuf[48];
memset (timbuf, 0, sizeof (timbuf));
if (time (&nowt))
{
ctime_r (&nowt, timbuf);
}
else
SYNPER_FATAL ("%s git %s failed to get current time: %m", synper_progname,
SYNPER_STRINGIFY (SYNPER_GITID));
if (synper_daemonized)
syslog (LOG_INFO,
"%s is daemonized as pid %ld on %s git %s executable %s on %s. See daemon(3)",
synper_progname, (long) getpid, synper_host,
SYNPER_STRINGIFY (SYNPER_GITID), synper_selfexe, timbuf);
else
syslog (LOG_INFO,
"%s is started as pid %ld on %s git %s executable %s on %s.",
synper_progname, (long) getpid, synper_host,
SYNPER_STRINGIFY (SYNPER_GITID), synper_selfexe, timbuf);
if (synper_name)
syslog (LOG_INFO, "%s named %s", synper_progname, synper_name);
if (synper_pidfile)
{
{
char oldexebuf[256];
memset (oldexebuf, 0, sizeof (oldexebuf));
FILE *oldpidfil = fopen (synper_pidfile, "r");
if (oldpidfil)
{
char oldexe[sizeof (synper_selfexe)];
long oldp = 0;
if (fscanf (oldpidfil, " %ld", &oldp) > 0 && oldp > 0
&& kill (oldp, 0))
{
char oldprexebuf[64];
memset (oldprexebuf, 0, sizeof (oldprexebuf));
snprintf (oldprexebuf, sizeof (oldprexebuf),
"/proc/%ld/exe", oldp);
if (readlink (oldprexebuf, oldexe, sizeof (oldexe)) > 0
&& oldexe[sizeof (oldexe) - 1] == (char) 0
&& oldexe[0] != (char) 0 && !strcmp (oldexe, myselfexe))
{
SYNPER_FATAL
("%s (executable %s) is already running as old pid %ld"
" on %s (git %s) - from pid %ld", synper_progname,
myselfexe, oldp, synper_host,
SYNPER_STRINGIFY (SYNPER_GITID), (long) getpid ());
}
}
fclose (oldpidfil);
}
}
{
char backup[256];
memset (backup, 0, sizeof (backup));
snprintf (backup, sizeof (backup) - 1, "%s~", synper_pidfile);
if (strcmp (backup, synper_pidfile) && !access (synper_pidfile, F_OK))
(void) rename (synper_pidfile, backup);
};
FILE *pidfil = fopen (synper_pidfile, "w");
if (!pidfil)
SYNPER_FATAL ("%s failed to open pid file %s : %m",
synper_progname, synper_pidfile);
fprintf (pidfil, "%ld\n", (long) getpid ());
if (fclose (pidfil))
SYNPER_FATAL ("%s failed to close pid file %s : %m",
synper_progname, synper_pidfile);
}
synper_set_signal_handlers ();
usleep (10 * 1024);
if (synper_period < SYNPER_MIN_PERIOD)
synper_period = SYNPER_MIN_PERIOD;
if (synper_period > SYNPER_MAX_PERIOD)
synper_period = SYNPER_MAX_PERIOD;
if (synper_logperiod < SYNPER_MIN_LOGPERIOD)
synper_logperiod = SYNPER_MIN_LOGPERIOD;
if (synper_logperiod < 3 * synper_period)
synper_logperiod = 3 * synper_period;
if (synper_logperiod > SYNPER_MAX_LOGPERIOD)
synper_logperiod = SYNPER_MAX_LOGPERIOD;
synper_syslog_begin ();
if (synper_pidfile)
{
syslog (LOG_INFO, "%s wrote pid-file %s as uid#%d on %s",
synper_progname, synper_pidfile, (int) getuid (), synper_host);
};
sleep (synper_period / 3);
time_t lastlogtime = 0;
long loopcnt = 0;
while (!atomic_load (&synper_stop))
{
time_t nowt = 0;
usleep (2000);
sync ();
(void) sleep (synper_period);
if (atomic_load (&synper_stop))
break;
time (&nowt);
loopcnt++;
if (nowt <= lastlogtime)
{
time_t nextlogtime = nowt + synper_logperiod;
char nowbuf[80];
memset (nowbuf, 0, sizeof (nowbuf));
struct tm nowtm;
memset (&nowtm, 0, sizeof (nowtm));
if (localtime_r (&nowt, &nowtm) == NULL)
SYNPER_FATAL ("failed to get localtime");
if (strftime (nowbuf, sizeof (nowbuf), "%Y/%b/%d %T %Z", &nowtm)
<= 0)
SYNPER_FATAL ("failed to strftime");
syslog (LOG_INFO,
"%s with sync period %d seconds done #%ld at %s\n", argv[0],
synper_period, loopcnt, nowbuf);
lastlogtime = nextlogtime;
}
}; // end while
/// final syslog
{
time_t nowt = 0;
time (&nowt);
char nowbuf[80];
memset (nowbuf, 0, sizeof (nowbuf));
struct tm nowtm;
memset (&nowtm, 0, sizeof (nowtm));
if (localtime_r (&nowt, &nowtm) == NULL)
SYNPER_FATAL ("failed to get localtime");
if (strftime (nowbuf, sizeof (nowbuf), "%Y/%b/%d %T %Z", &nowtm) <= 0)
SYNPER_FATAL ("failed to strftime");
syslog (LOG_NOTICE,
"%s with sync period %d seconds terminating loop #%ld at %s\n",
argv[0], synper_period, loopcnt, nowbuf);
};
exit (EXIT_SUCCESS);
return 0;
} /* end of main */
/****************
** for Emacs...
** Local Variables: ;;
** compile-command: "make sync-periodically" ;;
** End: ;;
****************/