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

Last change on this file since 1344 was 1344, checked in by Bruno Cornec, 13 years ago

Logfile management in mondo (partly)

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