source: branches/stable/mondo/src/common/libmondo-fifo.c @ 1080

Last change on this file since 1080 was 1080, checked in by bruno, 12 years ago
  • Fix mindi install messages (reported by Andree Leidenfrost)
  • remove paranoid_free/free for mr_free
  • mr_asprintf used everywhere
  • mr_malloc used everywhere
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1/* libmondo-fifo.c
2   $Id: libmondo-fifo.c 1080 2007-01-28 18:04:41Z bruno $
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>
14#include <fcntl.h>
15#include <stdio.h>
16
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 */
37bool g_sigpipe = FALSE;
38
39/**
40 * PID of the "main" process.
41 */
42pid_t g_mastermind_pid = 0;
43
44
45
46/**
47 * Command line with which @c buffer was invoked.
48 */
49char g_sz_call_to_buffer[MAX_STR_LEN];
50
51/**
52 * Size of the buffer used with @c buffer.
53 */
54int g_tape_buffer_size_MB = 0;
55
56/* @} - end of globalGroup */
57
58extern char *ps_options;
59extern char *ps_proc_id;
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 */
72FILE *open_device_via_buffer(char *device, char direction,
73                             long internal_tape_block_size)
74{
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;
84
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++;
102        sprintf(tmp, "Negotiated max buffer of %d MB ", bufsize);
103        log_to_screen(tmp);
104    } else {
105        bufsize = 0;
106        res = 0;
107        log_to_screen
108            ("Cannot negotiate a buffer of ANY size. Using dd instead.");
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    }
124    log_msg(2, "Calling buffer --- command = '%s'", g_sz_call_to_buffer);
125    fres = popen(g_sz_call_to_buffer, sz_dir);
126    if (fres) {
127        log_msg(2, "Successfully opened ('%c') tape device %s", direction,
128                device);
129    } else {
130        log_msg(2, "Failed to open ('%c') tape device %s", direction,
131                device);
132    }
133    sleep(2);
134    sprintf(tmp, "ps %s | grep \"%s\"", ps_options, g_sz_call_to_buffer);
135    if (run_program_and_log_output(tmp, 2)) {
136        log_msg(2, "Warning - I think I failed to open tape, actually.");
137    }
138    g_tape_buffer_size_MB = bufsize;
139    tmp[30] = '\0';
140    sprintf(command, "ps %s | grep buffer | grep -v grep", ps_options);
141    if (run_program_and_log_output(command, 1)) {
142        fres = NULL;
143        log_to_screen("Failed to open tape streamer. Buffer error.");
144    } else {
145        log_to_screen("Buffer successfully started.");
146    }
147
148    mr_free(command);
149    mr_free(tmp);
150    return (fres);
151}
152
153
154/**
155 * Kill @c buffer processes.
156 * Only called in mondoarchive
157 */
158void kill_buffer()
159{
160    char *tmp;
161    char *command;
162
163    if (g_sz_call_to_buffer == NULL) {
164        return;
165    }
166    if (strcmp(g_sz_call_to_buffer,"") == 0) {
167        return;
168    }
169    malloc_string(tmp);
170    malloc_string(command);
171    paranoid_system("sync");
172    sprintf(command,
173            "ps %s | grep -F \"%s\" | grep -Fv grep | awk '{print $2;}' | grep -v PID | head -1", ps_options, g_sz_call_to_buffer);
174    log_msg(2, "kill_buffer() --- command = %s", command);
175    strcpy(tmp, call_program_and_get_last_line_of_output(command));
176    sprintf(command, "kill %s", tmp);
177    log_msg(2, "kill_buffer() --- command = %s", command);
178    if (strlen(tmp) > 0) {
179        run_program_and_log_output(command, TRUE);
180    }
181    mr_free(tmp);
182    mr_free(command);
183}
184
185
186
187
188
189/**
190 * Handler for SIGPIPE.
191 * @param sig The signal that occurred (hopefully SIGPIPE).
192 */
193void sigpipe_occurred(int sig)
194{
195    g_sigpipe = TRUE;
196}
197
198/* @} - end of fifoGroup */
199
200int
201extract_config_file_from_ramdisk(struct s_bkpinfo *bkpinfo,
202                                 char *ramdisk_fname,
203                                 char *output_cfg_file,
204                                 char *output_mountlist_file);
Note: See TracBrowser for help on using the repository browser.