~ubuntu-branches/ubuntu/trusty/systemd/trusty

« back to all changes in this revision

Viewing changes to src/timedate/timedated.c

Tags: upstream-202
ImportĀ upstreamĀ versionĀ 202

Show diffs side-by-side

added added

removed removed

Lines of Context:
90
90
} TZ;
91
91
 
92
92
static TZ tz = {
 
93
        .zone = NULL,
 
94
        .local_rtc = false,
93
95
        .can_ntp = -1,
94
96
        .use_ntp = -1,
95
97
};
216
218
 
217
219
static int write_data_local_rtc(void) {
218
220
        int r;
219
 
        char *s, *w;
 
221
        _cleanup_free_ char *s = NULL, *w = NULL;
220
222
 
221
223
        r = read_full_file("/etc/adjtime", &s, NULL);
222
224
        if (r < 0) {
234
236
                size_t a, b;
235
237
 
236
238
                p = strchr(s, '\n');
237
 
                if (!p) {
238
 
                        free(s);
 
239
                if (!p)
239
240
                        return -EIO;
240
 
                }
241
241
 
242
242
                p = strchr(p+1, '\n');
243
 
                if (!p) {
244
 
                        free(s);
 
243
                if (!p)
245
244
                        return -EIO;
246
 
                }
247
245
 
248
246
                p++;
249
247
                e = strchr(p, '\n');
250
 
                if (!e) {
251
 
                        free(s);
 
248
                if (!e)
252
249
                        return -EIO;
253
 
                }
254
250
 
255
251
                a = p - s;
256
252
                b = strlen(e);
257
253
 
258
254
                w = new(char, a + (tz.local_rtc ? 5 : 3) + b + 1);
259
 
                if (!w) {
260
 
                        free(s);
 
255
                if (!w)
261
256
                        return -ENOMEM;
262
 
                }
263
257
 
264
258
                *(char*) mempcpy(stpcpy(mempcpy(w, s, a), tz.local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
265
259
 
266
260
                if (streq(w, NULL_ADJTIME_UTC)) {
267
 
                        free(w);
268
 
 
269
 
                        if (unlink("/etc/adjtime") < 0) {
 
261
                        if (unlink("/etc/adjtime") < 0)
270
262
                                if (errno != ENOENT)
271
263
                                        return -errno;
272
 
                        }
273
264
 
274
265
                        return 0;
275
266
                }
276
267
        }
277
268
        label_init("/etc");
278
 
        r = write_one_line_file_atomic_label("/etc/adjtime", w);
279
 
        free(w);
280
 
 
281
 
        return r;
 
269
        return write_string_file_atomic_label("/etc/adjtime", w);
282
270
}
283
271
 
284
272
static char** get_ntp_services(void) {
285
 
        char **r = NULL, **files, **i;
 
273
        _cleanup_strv_free_ char **r = NULL, **files;
 
274
        char **i;
286
275
        int k;
287
276
 
288
277
        k = conf_files_list(&files, ".list", NULL,
295
284
                return NULL;
296
285
 
297
286
        STRV_FOREACH(i, files) {
298
 
                FILE *f;
 
287
                _cleanup_fclose_ FILE *f;
299
288
 
300
289
                f = fopen(*i, "re");
301
290
                if (!f)
302
291
                        continue;
303
292
 
304
293
                for (;;) {
305
 
                        char line[PATH_MAX], *l, **q;
 
294
                        char line[PATH_MAX], *l;
306
295
 
307
296
                        if (!fgets(line, sizeof(line), f)) {
308
297
 
316
305
                        if (l[0] == 0 || l[0] == '#')
317
306
                                continue;
318
307
 
319
 
                        q = strv_append(r, l);
320
 
                        if (!q) {
 
308
                        if (strv_extend(&r, l) < 0) {
321
309
                                log_oom();
322
 
                                break;
 
310
                                return NULL;
323
311
                        }
324
 
 
325
 
                        strv_free(r);
326
 
                        r = q;
327
312
                }
328
 
 
329
 
                fclose(f);
330
313
        }
331
314
 
332
 
        strv_free(files);
 
315
        i = r;
 
316
        r = NULL; /* avoid cleanup */
333
317
 
334
 
        return strv_uniq(r);
 
318
        return strv_uniq(i);
335
319
}
336
320
 
337
321
static int read_ntp(DBusConnection *bus) {
367
351
                        goto finish;
368
352
                }
369
353
 
370
 
                if (reply)
371
 
                        dbus_message_unref(reply);
372
354
                reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
373
355
                if (!reply) {
374
356
                        if (streq(error.name, "org.freedesktop.DBus.Error.FileNotFound")) {
450
432
                        goto finish;
451
433
                }
452
434
 
453
 
                if (reply)
454
 
                        dbus_message_unref(reply);
455
435
                reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
456
436
                if (!reply) {
457
437
                        if (streq(error->name, "org.freedesktop.DBus.Error.FileNotFound") ||
540
520
                        }
541
521
                }
542
522
 
543
 
                if (reply)
544
 
                        dbus_message_unref(reply);
545
523
                reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
546
524
                if (!reply) {
547
525
                        if (streq(error->name, "org.freedesktop.DBus.Error.FileNotFound")) {
816
794
                        struct timespec ts;
817
795
                        struct tm* tm;
818
796
 
 
797
                        if (relative) {
 
798
                                usec_t n, x;
 
799
 
 
800
                                n = now(CLOCK_REALTIME);
 
801
                                x = n + utc;
 
802
 
 
803
                                if ((utc > 0 && x < n) ||
 
804
                                    (utc < 0 && x > n))
 
805
                                        return bus_send_error_reply(connection, message, NULL, -EOVERFLOW);
 
806
 
 
807
                                timespec_store(&ts, x);
 
808
                        } else
 
809
                                timespec_store(&ts, (usec_t) utc);
 
810
 
819
811
                        r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, NULL, &error);
820
812
                        if (r < 0)
821
813
                                return bus_send_error_reply(connection, message, &error, r);
822
814
 
823
 
                        if (relative)
824
 
                                timespec_store(&ts, now(CLOCK_REALTIME) + utc);
825
 
                        else
826
 
                                timespec_store(&ts, utc);
827
 
 
828
815
                        /* Set system clock */
829
816
                        if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
830
817
                                log_error("Failed to set local time: %m");
889
876
        if (!(reply = dbus_message_new_method_return(message)))
890
877
                goto oom;
891
878
 
892
 
        if (!dbus_connection_send(connection, reply, NULL))
 
879
        if (!bus_maybe_send_reply(connection, message, reply))
893
880
                goto oom;
894
881
 
895
882
        dbus_message_unref(reply);