[1] | 1 | /* libmondo-fifo.c
|
---|
[128] | 2 | $Id: libmondo-fifo.c 1107 2007-02-07 23:09:34Z bruno $
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * @file
|
---|
| 7 | * Functions to handle buffering of tape archives as they are read/written.
|
---|
| 8 | * This used the external program @c buffer mostly.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | #include <unistd.h>
|
---|
| 12 | #include <stdio.h>
|
---|
| 13 | #include <signal.h>
|
---|
[128] | 14 | #include <fcntl.h>
|
---|
[1] | 15 | #include <stdio.h>
|
---|
[128] | 16 |
|
---|
[1] | 17 | #include <errno.h>
|
---|
| 18 | #include <sys/types.h>
|
---|
| 19 | #include <sys/stat.h>
|
---|
| 20 | #include <sys/ipc.h>
|
---|
| 21 | #include <sys/shm.h>
|
---|
| 22 | #include <sys/sem.h>
|
---|
| 23 | #include <sys/wait.h>
|
---|
| 24 | #include <pthread.h>
|
---|
| 25 |
|
---|
| 26 | #include "my-stuff.h"
|
---|
| 27 | #include "mondostructures.h"
|
---|
| 28 | #include "libmondo.h"
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * @addtogroup globalGroup
|
---|
| 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /**
|
---|
| 35 | * The SIGPIPE handler sets this to TRUE.
|
---|
| 36 | */
|
---|
[128] | 37 | bool g_sigpipe = FALSE;
|
---|
[1] | 38 |
|
---|
| 39 | /**
|
---|
| 40 | * PID of the "main" process.
|
---|
| 41 | */
|
---|
[128] | 42 | pid_t g_mastermind_pid = 0;
|
---|
[1] | 43 |
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Command line with which @c buffer was invoked.
|
---|
| 48 | */
|
---|
| 49 | char g_sz_call_to_buffer[MAX_STR_LEN];
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Size of the buffer used with @c buffer.
|
---|
| 53 | */
|
---|
[128] | 54 | int g_tape_buffer_size_MB = 0;
|
---|
[1] | 55 |
|
---|
| 56 | /* @} - end of globalGroup */
|
---|
| 57 |
|
---|
[792] | 58 | extern char *ps_options;
|
---|
[928] | 59 | extern char *ps_proc_id;
|
---|
[1] | 60 |
|
---|
| 61 | /**
|
---|
| 62 | * @addtogroup fifoGroup
|
---|
| 63 | * @{
|
---|
| 64 | */
|
---|
| 65 | /**
|
---|
| 66 | * Open a pipe to/from @c buffer.
|
---|
| 67 | * If buffer does not work at all, we use `dd'.
|
---|
| 68 | * @param device The device to read from/write to.
|
---|
| 69 | * @param direction @c 'r' (reading) or @c 'w' (writing).
|
---|
| 70 | * @return A file pointer to/from the @c buffer process.
|
---|
| 71 | */
|
---|
[128] | 72 | FILE *open_device_via_buffer(char *device, char direction,
|
---|
| 73 | long internal_tape_block_size)
|
---|
[1] | 74 | {
|
---|
[128] | 75 | char sz_dir[32];
|
---|
| 76 | char keych;
|
---|
| 77 | char *tmp;
|
---|
| 78 | char *command;
|
---|
| 79 | FILE *fres;
|
---|
| 80 | int bufsize; // in megabytes
|
---|
| 81 | int res;
|
---|
| 82 | int wise_upper_limit;
|
---|
| 83 | int wise_lower_limit;
|
---|
[1] | 84 |
|
---|
[128] | 85 | malloc_string(tmp);
|
---|
| 86 | malloc_string(command);
|
---|
| 87 | assert_string_is_neither_NULL_nor_zerolength(device);
|
---|
| 88 | assert(direction == 'w' || direction == 'r');
|
---|
| 89 | sprintf(sz_dir, "%c", direction);
|
---|
| 90 | wise_upper_limit = (am_I_in_disaster_recovery_mode()? 8 : 32);
|
---|
| 91 | wise_lower_limit = 1; // wise_upper_limit/2 + 1;
|
---|
| 92 | paranoid_system("sync");
|
---|
| 93 | for (bufsize = wise_upper_limit, res = -1;
|
---|
| 94 | res != 0 && bufsize >= wise_lower_limit; bufsize--) {
|
---|
| 95 | sprintf(tmp,
|
---|
| 96 | "dd if=/dev/zero bs=1024 count=16k 2> /dev/null | buffer -o /dev/null -s %ld -m %d%c",
|
---|
| 97 | internal_tape_block_size, bufsize, 'm');
|
---|
| 98 | res = run_program_and_log_output(tmp, 2);
|
---|
| 99 | }
|
---|
| 100 | if (!res) {
|
---|
| 101 | bufsize++;
|
---|
[541] | 102 | sprintf(tmp, "Negotiated max buffer of %d MB ", bufsize);
|
---|
[128] | 103 | log_to_screen(tmp);
|
---|
| 104 | } else {
|
---|
| 105 | bufsize = 0;
|
---|
| 106 | res = 0;
|
---|
| 107 | log_to_screen
|
---|
[541] | 108 | ("Cannot negotiate a buffer of ANY size. Using dd instead.");
|
---|
[128] | 109 | }
|
---|
| 110 | if (direction == 'r') {
|
---|
| 111 | keych = 'i';
|
---|
| 112 | } else {
|
---|
| 113 | keych = 'o';
|
---|
| 114 | }
|
---|
| 115 | if (bufsize) {
|
---|
| 116 | sprintf(g_sz_call_to_buffer,
|
---|
| 117 | "buffer -m %d%c -p%d -B -s%ld -%c %s 2>> %s", bufsize, 'm',
|
---|
| 118 | (direction == 'r') ? 20 : 75, internal_tape_block_size,
|
---|
| 119 | keych, device, MONDO_LOGFILE);
|
---|
| 120 | } else {
|
---|
| 121 | sprintf(g_sz_call_to_buffer, "dd bs=%ld %cf=%s",
|
---|
| 122 | internal_tape_block_size, keych, device);
|
---|
| 123 | }
|
---|
[1107] | 124 | mr_msg(2, "Calling buffer --- command = '%s'", g_sz_call_to_buffer);
|
---|
[128] | 125 | fres = popen(g_sz_call_to_buffer, sz_dir);
|
---|
| 126 | if (fres) {
|
---|
[1107] | 127 | mr_msg(2, "Successfully opened ('%c') tape device %s", direction,
|
---|
[128] | 128 | device);
|
---|
| 129 | } else {
|
---|
[1107] | 130 | mr_msg(2, "Failed to open ('%c') tape device %s", direction,
|
---|
[128] | 131 | device);
|
---|
| 132 | }
|
---|
| 133 | sleep(2);
|
---|
[792] | 134 | sprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_sz_call_to_buffer);
|
---|
[128] | 135 | if (run_program_and_log_output(tmp, 2)) {
|
---|
[1107] | 136 | mr_msg(2, "Warning - I think I failed to open tape, actually.");
|
---|
[128] | 137 | }
|
---|
| 138 | g_tape_buffer_size_MB = bufsize;
|
---|
| 139 | tmp[30] = '\0';
|
---|
[792] | 140 | sprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
|
---|
[128] | 141 | if (run_program_and_log_output(command, 1)) {
|
---|
| 142 | fres = NULL;
|
---|
[541] | 143 | log_to_screen("Failed to open tape streamer. Buffer error.");
|
---|
[128] | 144 | } else {
|
---|
[541] | 145 | log_to_screen("Buffer successfully started.");
|
---|
[128] | 146 | }
|
---|
[1] | 147 |
|
---|
[1080] | 148 | mr_free(command);
|
---|
| 149 | mr_free(tmp);
|
---|
[128] | 150 | return (fres);
|
---|
[1] | 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Kill @c buffer processes.
|
---|
[928] | 156 | * Only called in mondoarchive
|
---|
[1] | 157 | */
|
---|
| 158 | void kill_buffer()
|
---|
| 159 | {
|
---|
[128] | 160 | char *tmp;
|
---|
| 161 | char *command;
|
---|
[1] | 162 |
|
---|
[941] | 163 | if (g_sz_call_to_buffer == NULL) {
|
---|
| 164 | return;
|
---|
| 165 | }
|
---|
| 166 | if (strcmp(g_sz_call_to_buffer,"") == 0) {
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
[128] | 169 | malloc_string(tmp);
|
---|
| 170 | malloc_string(command);
|
---|
| 171 | paranoid_system("sync");
|
---|
| 172 | sprintf(command,
|
---|
[941] | 173 | "ps %s | grep -F \"%s\" | grep -Fv grep | awk '{print $2;}' | grep -v PID | head -1", ps_options, g_sz_call_to_buffer);
|
---|
[1107] | 174 | mr_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[128] | 175 | strcpy(tmp, call_program_and_get_last_line_of_output(command));
|
---|
| 176 | sprintf(command, "kill %s", tmp);
|
---|
[1107] | 177 | mr_msg(2, "kill_buffer() --- command = %s", command);
|
---|
[128] | 178 | if (strlen(tmp) > 0) {
|
---|
| 179 | run_program_and_log_output(command, TRUE);
|
---|
| 180 | }
|
---|
[1080] | 181 | mr_free(tmp);
|
---|
| 182 | mr_free(command);
|
---|
[1] | 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * Handler for SIGPIPE.
|
---|
| 191 | * @param sig The signal that occurred (hopefully SIGPIPE).
|
---|
| 192 | */
|
---|
| 193 | void sigpipe_occurred(int sig)
|
---|
| 194 | {
|
---|
[128] | 195 | g_sigpipe = TRUE;
|
---|
[1] | 196 | }
|
---|
| 197 |
|
---|
| 198 | /* @} - end of fifoGroup */
|
---|
| 199 |
|
---|
[128] | 200 | int
|
---|
| 201 | extract_config_file_from_ramdisk(struct s_bkpinfo *bkpinfo,
|
---|
| 202 | char *ramdisk_fname,
|
---|
| 203 | char *output_cfg_file,
|
---|
| 204 | char *output_mountlist_file);
|
---|