|
65 | 65 | #define LOOP_MAJOR_ID 7
|
66 | 66 | #define SUDOERS_PATH SYSCONFDIR "/sudoers.d/"
|
67 | 67 | #define INSTANCE_ID_FILE DATADIR_PATH "/instance-id"
|
68 |
| -#define LAST_INSTANCE_ID_FILE DATADIR_PATH "/last-instance-id" |
| 68 | +#define FIRST_BOOT_ID_FILE DATADIR_PATH "/first-boot-id" |
| 69 | +#define KERNEL_BOOT_ID_FILE "/proc/sys/kernel/random/boot_id" |
| 70 | + |
| 71 | +G_LOCK_DEFINE(first_boot_id_file); |
69 | 72 |
|
70 | 73 |
|
71 | 74 | void LOG(const char *fmt, ...) {
|
@@ -518,89 +521,74 @@ bool umount_filesystem(const gchar* mountdir, const gchar* loop_device) {
|
518 | 521 | return true;
|
519 | 522 | }
|
520 | 523 |
|
521 |
| -bool save_instance_id(const gchar* instance_id) { |
522 |
| - GString* id = g_string_new(instance_id); |
523 |
| - |
524 |
| - LOG(MOD "Saving instance id '%s'\n", id->str); |
525 |
| - |
526 |
| - if (!write_file(id, INSTANCE_ID_FILE, O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU)) { |
527 |
| - LOG(MOD "Unable to save instance id\n"); |
528 |
| - g_string_free(id, true); |
529 |
| - return false; |
530 |
| - } |
531 |
| - |
532 |
| - g_string_free(id, true); |
533 |
| - return true; |
534 |
| -} |
535 |
| - |
536 |
| -void get_boot_info(bool* firstboot, bool* snapshot) { |
537 |
| - gchar* instance_id = NULL; |
| 524 | +bool save_instance_id(const gchar* id) { |
| 525 | + bool result = false; |
| 526 | + GString* instance_id = g_string_new(id); |
538 | 527 | gchar* last_instance_id = NULL;
|
539 | 528 | struct stat st;
|
540 |
| - static int cache_firstboot = -1; |
541 |
| - static int cache_snapshot = -1; |
542 | 529 |
|
543 |
| - if (cache_firstboot != -1 && cache_snapshot != -1 ) { |
544 |
| - if (snapshot) { |
545 |
| - *snapshot = (bool)cache_snapshot; |
| 530 | + if (stat(INSTANCE_ID_FILE, &st) != 0) { |
| 531 | + if (!write_file(instance_id, INSTANCE_ID_FILE, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) { |
| 532 | + LOG(MOD "Unable to save instance id\n"); |
| 533 | + goto exit; |
546 | 534 | }
|
547 |
| - if (firstboot) { |
548 |
| - *firstboot = (bool)cache_firstboot; |
549 |
| - } |
550 |
| - return; |
551 |
| - } |
552 |
| - |
553 |
| - if (snapshot) { |
554 |
| - *snapshot = false; |
555 |
| - } |
556 |
| - if (firstboot) { |
557 |
| - *firstboot = false; |
558 |
| - } |
559 |
| - |
560 |
| - if (stat(LAST_INSTANCE_ID_FILE, &st) != 0) { |
561 |
| - LOG(MOD "first boot! - '%s' not found\n", LAST_INSTANCE_ID_FILE); |
562 |
| - if (!copy_file(INSTANCE_ID_FILE, LAST_INSTANCE_ID_FILE)) { |
563 |
| - LOG(MOD "Copy file failed\n"); |
| 535 | + } else { |
| 536 | + if (!g_file_get_contents(INSTANCE_ID_FILE, &last_instance_id, NULL, NULL)) { |
| 537 | + LOG(MOD "Unable to read file '%s'\n", INSTANCE_ID_FILE); |
| 538 | + goto exit; |
564 | 539 | }
|
565 |
| - if (firstboot) { |
566 |
| - *firstboot = true; |
| 540 | + if (g_strcmp0(instance_id->str, last_instance_id) != 0) { |
| 541 | + g_free(last_instance_id); |
| 542 | + if (!write_file(instance_id, INSTANCE_ID_FILE, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) { |
| 543 | + LOG(MOD "Unable to save instance id\n"); |
| 544 | + goto exit; |
| 545 | + } |
| 546 | + result = true; |
| 547 | + G_LOCK(first_boot_id_file); |
| 548 | + remove(FIRST_BOOT_ID_FILE); |
| 549 | + G_UNLOCK(first_boot_id_file); |
567 | 550 | }
|
568 |
| - cache_firstboot = 1; |
569 |
| - cache_snapshot = 0; |
570 |
| - return; |
571 | 551 | }
|
572 | 552 |
|
573 |
| - if (!g_file_get_contents(INSTANCE_ID_FILE, &instance_id, NULL, NULL)) { |
574 |
| - LOG(MOD "Unable to read file '%s'\n", INSTANCE_ID_FILE); |
575 |
| - return; |
576 |
| - } |
| 553 | +exit: |
| 554 | + g_string_free(instance_id, true); |
| 555 | + return result; |
| 556 | +} |
577 | 557 |
|
578 |
| - if (!g_file_get_contents(LAST_INSTANCE_ID_FILE, &last_instance_id, NULL, NULL)) { |
579 |
| - LOG(MOD "Unable to read file '%s'\n", LAST_INSTANCE_ID_FILE); |
580 |
| - goto fail1; |
581 |
| - } |
| 558 | +bool is_first_boot(void) { |
| 559 | + struct stat st; |
| 560 | + bool firstboot = false; |
| 561 | + gchar* boot_id; |
| 562 | + gchar* first_boot_id; |
582 | 563 |
|
583 |
| - cache_firstboot = 0; |
584 |
| - cache_snapshot = 0; |
| 564 | + G_LOCK(first_boot_id_file); |
585 | 565 |
|
586 |
| - if (g_strcmp0(instance_id, last_instance_id) != 0) { |
587 |
| - LOG(MOD "first boot!\n"); |
588 |
| - if (!copy_file(INSTANCE_ID_FILE, LAST_INSTANCE_ID_FILE)) { |
589 |
| - LOG(MOD "Copy file failed\n"); |
| 566 | + if (stat(FIRST_BOOT_ID_FILE, &st) != 0) { |
| 567 | + firstboot = true; |
| 568 | + if (!copy_file(KERNEL_BOOT_ID_FILE, FIRST_BOOT_ID_FILE)) { |
| 569 | + LOG(MOD "Copy file '%s' failed\n", KERNEL_BOOT_ID_FILE); |
| 570 | + return false; |
| 571 | + } |
| 572 | + } else { |
| 573 | + if (!g_file_get_contents(KERNEL_BOOT_ID_FILE, &boot_id, NULL, NULL)) { |
| 574 | + LOG(MOD "Unable to read file '%s'\n", KERNEL_BOOT_ID_FILE); |
| 575 | + goto exit; |
590 | 576 | }
|
591 |
| - if (snapshot) { |
592 |
| - *snapshot = true; |
| 577 | + if (!g_file_get_contents(FIRST_BOOT_ID_FILE, &first_boot_id, NULL, NULL)) { |
| 578 | + LOG(MOD "Unable to read file '%s'\n", FIRST_BOOT_ID_FILE); |
| 579 | + g_free(boot_id); |
| 580 | + goto exit; |
593 | 581 | }
|
594 |
| - if (firstboot) { |
595 |
| - *firstboot = true; |
| 582 | + if (g_strcmp0(first_boot_id, boot_id) == 0) { |
| 583 | + firstboot = true; |
596 | 584 | }
|
597 |
| - cache_firstboot = 1; |
598 |
| - cache_snapshot = 1; |
| 585 | + g_free(first_boot_id); |
| 586 | + g_free(boot_id); |
599 | 587 | }
|
600 | 588 |
|
601 |
| - g_free(last_instance_id); |
602 |
| -fail1: |
603 |
| - g_free(instance_id); |
| 589 | +exit: |
| 590 | + G_UNLOCK(first_boot_id_file); |
| 591 | + return firstboot; |
604 | 592 | }
|
605 | 593 |
|
606 | 594 | bool gnode_free(GNode* node, __unused__ gpointer data) {
|
|
0 commit comments