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

Last change on this file since 1157 was 1157, checked in by Bruno Cornec, 17 years ago

2 more files merged from trunk (pass 1)

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