1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
/*
* Copyright (C) 2000-2020, Thomas Maier-Komor
*
* This file is part of mbuffer's source code.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mbconf.h"
#include "input.h"
#include "common.h"
#include "log.h"
#include "dest.h"
#include "globals.h"
#include "settings.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <float.h>
#include <math.h>
#include <pthread.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef __sun
#define waitInput()
#else
static void waitInput(void)
{
if (Status != 0) {
int maxfd = TermQ[0] > In ? TermQ[0] + 1 : In + 1;
int err;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(TermQ[0],&readfds);
FD_SET(In,&readfds);
do {
err = select(maxfd,&readfds,0,0,0);
debugiomsg("inputThread: select(%d, {%d,%d}, 0, 0, 0) = %d\n", maxfd,In,TermQ[0],err);
assert((err > 0) || (errno == EBADF || errno == EINTR));
} while ((err < 0) && (errno == EINTR));
if (FD_ISSET(TermQ[0],&readfds))
pthread_exit((void *)-1);
assert(FD_ISSET(In,&readfds));
}
}
#endif
int promptInteractive()
{
static const char prompt[] = "\nContinue with next volume? Press 'y' to continue or 'n' to finish...";
static const char contmsg[] = "\nyes - continuing with next volume...\n";
static const char donemsg[] = "\nno - input done, waiting for output to finish...\n";
int err;
err = pthread_mutex_lock(&TermMut);
assert(0 == err);
if (-1 == write(STDERR_FILENO,prompt,sizeof(prompt))) {
errormsg("error accessing controlling terminal for manual volume change request: %s\nConsider using autoload option, when running mbuffer without terminal.\n",strerror(errno));
Terminate = 1;
pthread_exit((void *) -1);
}
for (;;) {
char c = 0;
if (-1 == read(STDERR_FILENO,&c,1) && (errno != EINTR)) {
errormsg("error accessing controlling terminal for manual volume change request: %s\nConsider using autoload option, when running mbuffer without terminal.\n",strerror(errno));
Terminate = 1;
pthread_exit((void *) -1);
}
debugmsg("prompt input %c\n",c);
switch (c) {
case 'n':
case 'N':
(void) write(STDERR_FILENO,donemsg,sizeof(donemsg));
err = pthread_mutex_unlock(&TermMut);
assert(0 == err);
return 0;
case 'y':
case 'Y':
(void) write(STDERR_FILENO,contmsg,sizeof(contmsg));
err = pthread_mutex_unlock(&TermMut);
assert(0 == err);
return 1;
default:;
}
}
}
static int requestInputVolume()
{
static struct timespec volstart = {0,0};
const char *cmd;
struct timespec now;
double diff;
unsigned min,hr;
char cmd_buf[15+strlen(Infile)];
debugmsg("requesting new volume for input\n");
(void) clock_gettime(ClockSrc,&now);
if (volstart.tv_sec)
diff = now.tv_sec - volstart.tv_sec + (double) (now.tv_nsec - volstart.tv_nsec) * 1E-9;
else
diff = now.tv_sec - Starttime.tv_sec + (double) (now.tv_nsec - Starttime.tv_nsec) * 1E-9;
if (diff > 3600) {
hr = (unsigned) (diff / 3600);
diff -= hr * 3600;
min = (unsigned) (diff / 60);
diff -= min * 60;
infomsg("time for reading volume: %u:%02u:%02f\n",hr,min,diff);
} else if (diff > 60) {
min = (unsigned) (diff / 60);
diff -= min * 60;
infomsg("time for reading volume: %02u:%02f\n",min,diff);
} else
infomsg("time for reading volume: %02fsec.\n",diff);
if (-1 == close(In))
errormsg("error closing input: %s\n",strerror(errno));
do {
if ((Autoloader) && (Infile)) {
int ret;
if (AutoloadCmd) {
cmd = AutoloadCmd;
} else {
(void) snprintf(cmd_buf, sizeof(cmd_buf), "mt -f %s offline", Infile);
cmd = cmd_buf;
}
infomsg("requesting new input volume with command '%s'\n",cmd);
ret = system(cmd);
if (0 < ret) {
warningmsg("error running \"%s\" to change volume in autoloader: exitcode %d\n",cmd,ret);
Terminate = 1;
pthread_exit((void *) 0);
} else if (0 > ret) {
errormsg("error starting \"%s\" to change volume in autoloader: %s\n", cmd, strerror(errno));
Terminate = 1;
pthread_exit((void *) -1);
}
if (AutoloadTime) {
infomsg("waiting for drive to get ready...\n");
(void) sleep(AutoloadTime);
}
} else if (0 == promptInteractive()) {
return 0;
}
In = open(Infile, O_RDONLY | O_LARGEFILE);
if ((-1 == In) && (errno == EINVAL))
In = open(Infile,O_RDONLY);
if (-1 != In)
enable_directio(In,Infile);
else
errormsg("could not reopen input %s: %s\n",Infile,strerror(errno));
} while (In == -1);
(void) clock_gettime(ClockSrc,&volstart);
diff = volstart.tv_sec - now.tv_sec + (double) (volstart.tv_nsec - now.tv_nsec) * 1E-9;
infomsg("tape-change took %fsec. - continuing with next volume\n",diff);
NumVolumes--;
if (Terminal && ! Autoloader) {
char msg[] = "\nOK - continuing...\n";
(void) write(STDERR_FILENO,msg,sizeof(msg));
}
return 1;
}
void openInput()
{
assert(Infile);
debugmsg("opening input %s\n",Infile);
int flags = O_RDONLY | O_LARGEFILE;
In = open(Infile,flags);
if ((-1 == In) && (errno == EINVAL)) {
flags &= ~O_LARGEFILE;
In = open(Infile,flags);
}
if (-1 == In)
fatal("could not open input file '%s': %s\n",Infile,strerror(errno));
enable_directio(In,Infile);
struct stat st;
if ((0 == fstat(In,&st)) && ((st.st_mode & S_IFMT) == S_IFREG))
InSize = st.st_size;
#ifdef __sun
if (0 == directio(In,DIRECTIO_ON))
infomsg("direct I/O hinting enabled for input\n");
else
infomsg("direct I/O hinting failed for input: %s\n",strerror(errno));
#endif
}
static int devread(unsigned at)
{
static char *DevBuf = 0;
static size_t IFill = 0, Off = 0;
static int hadzero = 0;
int num = 0;
do {
if (IFill) {
size_t s = IFill;
if (IFill > (Blocksize-num))
s = Blocksize-num;
debugmsg("fillop %d, fill %d, off %d\n",s,IFill,Off);
memcpy(Buffer[at]+num,DevBuf+Off,s);
Off += s;
IFill -= s;
num += s;
if (num == Blocksize)
return num;
}
if (hadzero) {
hadzero = 0;
return 0;
}
ssize_t in = read(In,Buffer[at] + num,Blocksize - num);
debugmsg("devread %d = %d\n",Blocksize-num,in);
if (in > 0) {
num += in;
} else if (in == 0) {
if (num)
hadzero = 1;
return num;
} else if (in == -1) {
if ((errno == EINVAL) && disable_directio(In,Infile))
continue;
if (errno != ENOMEM)
return -1;
if (DevBuf == 0) {
// devread is only called when IDevBSize > 0
assert(IDevBSize > 0);
DevBuf = malloc(IDevBSize);
assert(DevBuf);
}
assert(IFill == 0);
ssize_t i2 = read(In,DevBuf,IDevBSize);
debugmsg("devread2 %d = %d %d/%s\n",IDevBSize,i2,errno,strerror(errno));
if ((i2 == -1) && (errno == EINVAL) && disable_directio(In,Infile))
continue;
if (i2 == -1) {
assert(errno != ENOMEM);
return -1;
}
if (i2 == 0) {
if (num)
hadzero = 1;
return num;
}
assert(IFill == 0);
IFill = i2;
Off = 0;
}
} while (num != Blocksize);
return num;
}
int readBlock(unsigned at)
{
int err;
size_t num = 0;
waitInput();
do {
ssize_t in;
if (IDevBSize)
in = devread(at);
else if (InSocket)
in = recv(In,Buffer[at] + num,Blocksize - num,MSG_WAITALL);
else
in = read(In,Buffer[at] + num,Blocksize - num);
debugiomsg("inputThread: read(In, Buffer[%d] + %llu, %llu) = %d\n", at, num, Blocksize - num, in);
if (in > 0) {
num += in;
} else if (((0 == in) || ((-1 == in) && (errno == EIO))) && (Terminal||Autoloader) && (NumVolumes != 1)) {
if (0 == requestInputVolume()) {
Finish = at;
Rest = num;
debugmsg("inputThread: last block has %llu bytes\n",num);
err = pthread_mutex_lock(&HighMut);
assert(err == 0);
err = sem_post(&Buf2Dev);
assert(err == 0);
err = pthread_cond_signal(&PercHigh);
assert(err == 0);
err = pthread_mutex_unlock(&HighMut);
assert(err == 0);
infomsg("inputThread: exiting...\n");
if (Status)
pthread_exit(0);
return 0;
}
} else if (in <= 0) {
/* error or end-of-file */
if ((-1 == in) && (errno == EINVAL) && disable_directio(In,Infile))
continue;
if ((-1 == in) && (errno == EINTR))
continue;
if ((-1 == in) && (Terminate == 0))
errormsg("inputThread: error reading at offset 0x%llx: %s\n",Numin*Blocksize,strerror(errno));
Rest = num;
Finish = at;
debugmsg("inputThread: last block has %llu bytes\n",num);
err = pthread_mutex_lock(&HighMut);
assert(err == 0);
err = sem_post(&Buf2Dev);
assert(err == 0);
err = pthread_cond_signal(&PercHigh);
assert(err == 0);
err = pthread_mutex_unlock(&HighMut);
assert(err == 0);
infomsg("inputThread: exiting...\n");
if (Status)
pthread_exit((void *)(ptrdiff_t) in);
return in;
}
} while (num < Blocksize);
return 1;
}
void *inputThread(void *ignored)
{
int fill = 0;
unsigned at = 0;
long long xfer = 0;
const double startread = StartRead, startwrite = StartWrite;
struct timespec last;
#ifndef __sun
if (Status != 0)
assert(TermQ[0] != -1);
#endif
(void) clock_gettime(ClockSrc,&last);
assert(ignored == 0);
infomsg("inputThread: starting with threadid 0x%lx...\n",(long)pthread_self());
for (;;) {
int err;
if (startread < 1) {
err = pthread_mutex_lock(&LowMut);
assert(err == 0);
err = sem_getvalue(&Buf2Dev,&fill);
assert(err == 0);
if (fill == Numblocks - 1) {
debugmsg("inputThread: buffer full, waiting for it to drain.\n");
pthread_cleanup_push(releaseLock,&LowMut);
err = pthread_cond_wait(&PercLow,&LowMut);
assert(err == 0);
pthread_cleanup_pop(0);
++FullCount;
debugmsg("inputThread: low watermark reached, continuing...\n");
}
err = pthread_mutex_unlock(&LowMut);
assert(err == 0);
}
if (Terminate) { /* for async termination requests */
debugmsg("inputThread: terminating early upon request...\n");
if (-1 == close(In))
errormsg("error closing input: %s\n",strerror(errno));
if (Status)
pthread_exit((void *)1);
return (void *) 1;
}
err = sem_wait(&Dev2Buf); /* Wait for one or more buffer blocks to be free */
assert(err == 0);
if (0 >= readBlock(at)) {
debugmsg("inputThread: no more blocks\n");
return 0;
}
if (MaxReadSpeed)
xfer = enforceSpeedLimit(MaxReadSpeed,xfer,&last);
err = sem_post(&Buf2Dev);
assert(err == 0);
if (startwrite > 0) {
err = pthread_mutex_lock(&HighMut);
assert(err == 0);
err = sem_getvalue(&Buf2Dev,&fill);
assert(err == 0);
if (((double) fill / (double) Numblocks) + DBL_EPSILON >= startwrite) {
err = pthread_cond_signal(&PercHigh);
assert(err == 0);
}
err = pthread_mutex_unlock(&HighMut);
assert(err == 0);
}
if (++at == Numblocks)
at = 0;
Numin++;
}
}
|