| 1 | /* |
| 2 | * Copyright 2011, Haiku, Inc. All rights reserved. |
| 3 | * Distributed under the terms of the MIT License. |
| 4 | * |
| 5 | * Authors: |
| 6 | * Hamish Morrison <hamish@lavabit.com> |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | |
| 11 | #include <Alert.h> |
| 12 | #include <Application.h> |
| 13 | #include <Catalog.h> |
| 14 | #include <File.h> |
| 15 | #include <FindDirectory.h> |
| 16 | #include <kernel/fs_info.h> |
| 17 | #include <Path.h> |
| 18 | #include <Roster.h> |
| 19 | |
| 20 | #include <driver_settings.h> |
| 21 | |
| 22 | |
| 23 | #undef B_TRANSLATE_CONTEXT |
| 24 | #define B_TRANSLATE_CONTEXT "swapcheck" |
| 25 | |
| 26 | |
| 27 | static const char* const kSwapErrorFilePath = "swaperr"; |
| 28 | static const char* const kVirtualMemorySettings = "virtual_memory"; |
| 29 | |
| 30 | |
| 31 | int |
| 32 | main() |
| 33 | { |
| 34 | // Ensure that the swap error file is not old |
| 35 | time_t currentTime = time(NULL); |
| 36 | bigtime_t bootTime = (system_time() / 1000000) + 1; |
| 37 | |
| 38 | BPath path; |
| 39 | if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path) != B_OK) |
| 40 | return 1; |
| 41 | path.Append(kSwapErrorFilePath); |
| 42 | |
| 43 | const char* pathString = path.Path(); |
| 44 | struct stat errFile; |
| 45 | if (stat(pathString, &errFile) < 0) |
| 46 | return 0; |
| 47 | |
| 48 | time_t modTime = currentTime - errFile.st_mtime; |
| 49 | if (modTime >= bootTime) { |
| 50 | unlink(pathString); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | // Currently all swap errors result in the swap file |
| 55 | // being created on the boot device, so update the swap |
| 56 | // settings to reflect that. |
| 57 | void* settings = load_driver_settings(kVirtualMemorySettings); |
| 58 | if (settings != NULL) { |
| 59 | const char* enabled = get_driver_parameter(settings, |
| 60 | "vm", NULL, NULL); |
| 61 | const char* size = get_driver_parameter(settings, |
| 62 | "swap_size", NULL, NULL); |
| 63 | |
| 64 | if (enabled != NULL && size != NULL) { |
| 65 | BPath path; |
| 66 | if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) { |
| 67 | path.Append("kernel/drivers"); |
| 68 | path.Append(kVirtualMemorySettings); |
| 69 | |
| 70 | BFile file; |
| 71 | if (file.SetTo(path.Path(), |
| 72 | B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK) { |
| 73 | fs_info info; |
| 74 | fs_stat_dev(dev_for_path("/boot"), &info); |
| 75 | |
| 76 | char buffer[1024]; |
| 77 | snprintf(buffer, sizeof(buffer), "vm %s\nswap_size %s\n" |
| 78 | "swap_volume_name %s\nswap_volume_device %s\n" |
| 79 | "swap_volume_filesystem %s\n" |
| 80 | "swap_volume_capacity %lld\n", enabled, size, |
| 81 | info.volume_name, info.device_name, info.fsh_name, |
| 82 | info.total_blocks * info.block_size); |
| 83 | file.Write(buffer, strlen(buffer)); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | unload_driver_settings(settings); |
| 88 | } |
| 89 | |
| 90 | int fd = open(pathString, O_RDONLY); |
| 91 | if (fd >= 0) { |
| 92 | char buf[8] = { 0 }; |
| 93 | if (read(fd, &buf, sizeof(buf)) > 0) { |
| 94 | BApplication app("application/x-vnd.Haiku-cmd-swapcheck"); |
| 95 | const char* prompt = NULL; |
| 96 | if (strcmp(buf, "nfnd") == 0) |
| 97 | prompt = B_TRANSLATE("The system could not locate the " |
| 98 | "specified swap volume at boot time. The boot " |
| 99 | "device has been used instead"); |
| 100 | else if (strcmp(buf, "nmnt") == 0) |
| 101 | prompt = B_TRANSLATE("The system could not mount the " |
| 102 | "specified swap volume at boot time. The boot " |
| 103 | "device has been used instead"); |
| 104 | |
| 105 | if (prompt != NULL) { |
| 106 | int32 choice = 0; |
| 107 | choice = (new BAlert("swapcheck", prompt, |
| 108 | B_TRANSLATE("OK"), B_TRANSLATE("Adjust swap " |
| 109 | "settings" B_UTF8_ELLIPSIS)))->Go(); |
| 110 | if (choice == 1) |
| 111 | be_roster->Launch("application/x-vnd." |
| 112 | "Haiku-VirtualMemory"); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | unlink(pathString); |
| 117 | close(fd); |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |