| 1 | /* |
| 2 | * Copyright (c) 2024 |
| 3 | * Tim Woodall. All rights reserved |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
| 26 | * |
| 27 | * SPDX short identifier: BSD-2-Clause |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * faketape |
| 32 | */ |
| 33 | |
| 34 | #include "config.h" // IWYU pragma: keep |
| 35 | #include "faketape-lib.h" |
| 36 | |
| 37 | #include <err.h> |
| 38 | #include <string.h> |
| 39 | #include <fstream> |
| 40 | #include <charconv> |
| 41 | #include <fcntl.h> |
| 42 | #include <sys/mtio.h> |
| 43 | #include <errno.h> |
| 44 | #include <stdint.h> |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <sys/types.h> |
| 48 | #include <unistd.h> |
| 49 | #include <iostream> |
| 50 | #include <memory> |
| 51 | #include <string> |
| 52 | #include <system_error> |
| 53 | #include <vector> |
| 54 | extern "C" { |
| 55 | #include "common/rmtflags.h" |
| 56 | } |
| 57 | #include "rmt/rmt.h" |
| 58 | |
| 59 | static FakeTape tape; |
| 60 | |
| 61 | static std::vector<uint8_t> databuffer; |
| 62 | |
| 63 | template<typename T> static bool convertstring(const std::string& s, T& v); |
| 64 | |
| 65 | static void getstring (std::string& s); |
| 66 | |
| 67 | static bool respond(off_t val, FakeTape::Response r = FakeTape::Response::OK) { |
| 68 | static char resp[BUFSIZ]; |
| 69 | const char* errstr = strerror(val); |
| 70 | char respchar; |
| 71 | switch(r) { |
| 72 | case FakeTape::Response::OK: |
| 73 | respchar = 'A'; |
| 74 | errstr = ""; |
| 75 | break; |
| 76 | case FakeTape::Response::ERROR: |
| 77 | respchar = 'E'; |
| 78 | break; |
| 79 | case FakeTape::Response::FATAL: |
| 80 | respchar = 'F'; |
| 81 | break; |
| 82 | default: |
| 83 | errx(1, "Unhandled Response"); |
| 84 | } |
| 85 | DEBUG(tape, "faketape response: " << respchar << " " << val << " " << errstr); |
| 86 | (void)snprintf(resp, sizeof(resp), "%c%jd\n%s%c", respchar, (intmax_t)val, errstr, *errstr?'\n':0); |
| 87 | if (write(1, resp, strlen(resp)) != (ssize_t)strlen(resp)) { |
| 88 | warn("faketape: write error sending response\n"); |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | static bool ioerror(FakeTape::Response r) |
| 95 | { |
| 96 | return respond(errno, r); |
| 97 | } |
| 98 | |
| 99 | static std::unique_ptr<FakeTapeTimer> process_time; |
| 100 | static std::unique_ptr<FakeTapeTimer> open_time; |
| 101 | static std::unique_ptr<FakeTapeTimer> close_time; |
| 102 | static std::unique_ptr<FakeTapeTimer> write_time; |
| 103 | static std::unique_ptr<FakeTapeTimer> read_time; |
| 104 | |
| 105 | bool process_command() |
| 106 | { |
| 107 | CalcTime ct(*process_time); |
| 108 | using Response = FakeTape::Response; |
| 109 | errno = 0; |
| 110 | char c; |
| 111 | if (read(0, &c, 1) != 1) |
| 112 | return false; |
| 113 | switch (c) { |
| 114 | |
| 115 | case 'O': |
| 116 | { |
| 117 | CalcTime ct(*open_time); |
| 118 | std::string device; |
| 119 | std::string filemode; |
| 120 | getstring(device); |
| 121 | getstring(filemode); |
| 122 | DEBUG(tape, "faketape: O " << device << " " << filemode << std::endl); |
| 123 | tape.close(); |
| 124 | int oflags = rmtflags_toint(filemode.c_str()); |
| 125 | if(Response r = tape.open(device, oflags); r != Response::OK) |
| 126 | return ioerror(r); |
| 127 | else |
| 128 | return respond(0); |
| 129 | } |
| 130 | |
| 131 | case 'C': |
| 132 | { |
| 133 | CalcTime ct(*close_time); |
| 134 | std::string device; |
| 135 | getstring(device); |
| 136 | DEBUG(tape, "faketape: C " << device << " (" << tape.getPos() << " blocks)"); |
| 137 | tape.close(); |
| 138 | return respond(0); |
| 139 | } |
| 140 | |
| 141 | case 'L': |
| 142 | { |
| 143 | std::string count; |
| 144 | std::string pos; |
| 145 | getstring(count); |
| 146 | getstring(pos); |
| 147 | DEBUG(tape, "faketape: L " << count << " " << pos); |
| 148 | |
| 149 | off_t offset; |
| 150 | int cmd; |
| 151 | if (!convertstring(count, offset) || !convertstring(pos, cmd)) |
| 152 | return ioerror(Response::ERROR); |
| 153 | |
| 154 | switch (cmd) { |
| 155 | case SEEK_SET: |
| 156 | tape.blockSeek(offset); |
| 157 | return respond(tape.getPos()); |
| 158 | case SEEK_CUR: |
| 159 | offset += tape.getPos(); |
| 160 | tape.blockSeek(offset); |
| 161 | return respond(tape.getPos()); |
| 162 | case LSEEK_GET_TAPEPOS: |
| 163 | return respond(tape.getPos()); |
| 164 | case LSEEK_IS_MAGTAPE: |
| 165 | return respond(1); |
| 166 | case LSEEK_GO2_TAPEPOS: |
| 167 | tape.blockSeek(offset); |
| 168 | return respond(tape.getPos()); |
| 169 | case SEEK_END: |
| 170 | /* Too hard to do */ |
| 171 | default: |
| 172 | errno = EINVAL; |
| 173 | return ioerror(Response::ERROR); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | case 'W': |
| 178 | { |
| 179 | CalcTime ct(*write_time); |
| 180 | std::string count; |
| 181 | getstring(count); |
| 182 | DEBUG(tape, "faketape: W " << count << " (block = " << tape.getPos() << ")"); |
| 183 | |
| 184 | size_t size; |
| 185 | if (!convertstring(count, size)) { |
| 186 | return ioerror(Response::ERROR); |
| 187 | } |
| 188 | |
| 189 | if (databuffer.size() < size) |
| 190 | databuffer.resize(size); |
| 191 | |
| 192 | ssize_t cc; |
| 193 | for (size_t i = 0; i < size; i += cc) { |
| 194 | cc = read(0, databuffer.data()+i, size - i); |
| 195 | if (cc <= 0) { |
| 196 | DEBUG(tape, "faketape: premature eof i=" << i); |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if(Response r = tape.writeDataAsync(databuffer.data(), size); r != Response::OK) |
| 202 | return ioerror(r); |
| 203 | |
| 204 | return respond(size); |
| 205 | } |
| 206 | |
| 207 | case 'R': |
| 208 | { |
| 209 | CalcTime ct(*read_time); |
| 210 | std::string count; |
| 211 | getstring(count); |
| 212 | DEBUG(tape, "faketape: R " << count << " (block " << tape.getPos() << ")"); |
| 213 | |
| 214 | size_t size; |
| 215 | if (!convertstring(count, size)) |
| 216 | return ioerror(Response::ERROR); |
| 217 | |
| 218 | if (databuffer.size() < size) |
| 219 | databuffer.resize(size); |
| 220 | |
| 221 | size_t readsize; |
| 222 | if(Response r = tape.readData(databuffer.data(), size, &readsize); r != Response::OK) |
| 223 | return respond(EIO, r); |
| 224 | |
| 225 | |
| 226 | if(!respond(readsize)) |
| 227 | return false; |
| 228 | ssize_t cc; |
| 229 | for (size_t i = 0; i < readsize; i += cc) { |
| 230 | cc = write(1, databuffer.data(), readsize); |
| 231 | if (cc <= 0) { |
| 232 | DEBUG(tape, "faketape: failed to write response"); |
| 233 | return false; |
| 234 | } |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | case 'I': |
| 240 | { |
| 241 | std::string sop; |
| 242 | std::string scount; |
| 243 | getstring(sop); |
| 244 | getstring(scount); |
| 245 | DEBUG(tape, "faketape: I " << sop << " " << scount); |
| 246 | |
| 247 | size_t count; |
| 248 | int op; |
| 249 | if (!convertstring(scount, count) || ! convertstring(sop, op)) |
| 250 | return ioerror(Response::ERROR); |
| 251 | |
| 252 | off_t rval = -1; |
| 253 | switch(op) { |
| 254 | case MTRESET: /* 0 +reset drive in case of problems. */ |
| 255 | case MTFSF: /* 1 Forward space over FileMark, |
| 256 | position at first record of next file. */ |
| 257 | case MTBSF: /* 2 Backward space FileMark (position before FM). */ |
| 258 | case MTFSR: /* 3 Forward space record. */ |
| 259 | case MTBSR: /* 4 Backward space record. */ |
| 260 | case MTWEOF: /* 5 Write an end-of-file record (mark). */ |
| 261 | case MTREW: /* 6 Rewind. */ |
| 262 | case MTOFFL: /* 7 Rewind and put the drive offline (eject?). */ |
| 263 | case MTNOP: /* 8 No op, set status only (read with MTIOCGET). */ |
| 264 | case MTRETEN: /* 9 Retension tape. */ |
| 265 | case MTBSFM: /* 10 +backward space FileMark, position at FM. */ |
| 266 | case MTFSFM: /* 11 +forward space FileMark, position at FM. */ |
| 267 | case MTEOM: /* 12 Goto end of recorded media (for appending files). |
| 268 | MTEOM positions after the last FM, ready for |
| 269 | appending another file. */ |
| 270 | case MTERASE: /* 13 Erase tape -- be careful! */ |
| 271 | |
| 272 | case MTRAS1: /* 14 Run self test 1 (nondestructive). */ |
| 273 | case MTRAS2: /* 15 Run self test 2 (destructive). */ |
| 274 | case MTRAS3: /* 16 Reserved for self test 3. */ |
| 275 | |
| 276 | case MTSETBLK: /* 20 Set block length (SCSI). */ |
| 277 | case MTSETDENSITY: /* 21 Set tape density (SCSI). */ |
| 278 | case MTSEEK: /* 22 Seek to block (Tandberg, etc.). */ |
| 279 | case MTTELL: /* 23 Tell block (Tandberg, etc.). */ |
| 280 | case MTSETDRVBUFFER: /* 24 Set the drive buffering according to SCSI-2. |
| 281 | Ordinary buffered operation with code 1. */ |
| 282 | case MTFSS: /* 25 Space forward over setmarks. */ |
| 283 | case MTBSS: /* 26 Space backward over setmarks. */ |
| 284 | case MTWSM: /* 27 Write setmarks. */ |
| 285 | |
| 286 | case MTLOCK: /* 28 Lock the drive door. */ |
| 287 | case MTUNLOCK: /* 29 Unlock the drive door. */ |
| 288 | case MTLOAD: /* 30 Execute the SCSI load command. */ |
| 289 | case MTUNLOAD: /* 31 Execute the SCSI unload command. */ |
| 290 | case MTCOMPRESSION: /* 32 Control compression with SCSI mode page 15. */ |
| 291 | case MTSETPART: /* 33 Change the active tape partition. */ |
| 292 | case MTMKPART: /* 34 Format the tape with one or two partitions. */ |
| 293 | default: |
| 294 | errno = EINVAL; |
| 295 | return respond(rval); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | case 'S': /* status */ |
| 300 | { |
| 301 | DEBUG(tape, "faketape: S\n"); |
| 302 | struct mtget mtget; |
| 303 | mtget.mt_type = 0; |
| 304 | mtget.mt_resid = 0; |
| 305 | mtget.mt_dsreg = 0; |
| 306 | mtget.mt_gstat = 0; |
| 307 | mtget.mt_erreg = 0; |
| 308 | mtget.mt_fileno = 0; |
| 309 | mtget.mt_blkno = 0; |
| 310 | |
| 311 | if (!respond(sizeof (mtget))) |
| 312 | return false; |
| 313 | if (write(1, &mtget, sizeof (mtget)) != sizeof(mtget)) { |
| 314 | DEBUG(tape, "Status write failed"); |
| 315 | return false; |
| 316 | } |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | case 'V': /* version */ |
| 321 | { |
| 322 | std::string op; |
| 323 | getstring(op); |
| 324 | DEBUG(tape, "faketape: V " << op); |
| 325 | return respond(2); |
| 326 | } |
| 327 | |
| 328 | default: |
| 329 | DEBUG(tape, "faketape: garbage command " << c); |
| 330 | return false; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void usage(const char* prog) { |
| 335 | std::cerr << "Usage " << prog << " [ -b <max block size> ] [ <debug log file> ]" << std::endl; |
| 336 | exit(1); |
| 337 | } |
| 338 | |
| 339 | int |
| 340 | main(int argc, char *argv[]) |
| 341 | { |
| 342 | const char* prog = argv[0]; |
| 343 | |
| 344 | process_time = std::make_unique<FakeTapeTimer>(tape, "process_time"); |
| 345 | open_time = std::make_unique<FakeTapeTimer>(tape, "open_time"); |
| 346 | close_time = std::make_unique<FakeTapeTimer>(tape, "close_time"); |
| 347 | write_time = std::make_unique<FakeTapeTimer>(tape, "write_time"); |
| 348 | read_time = std::make_unique<FakeTapeTimer>(tape, "read_time"); |
| 349 | |
| 350 | int ch; |
| 351 | while (( ch = getopt(argc, argv, "b:d")) != -1) |
| 352 | switch(ch) { |
| 353 | |
| 354 | case 'b': { |
| 355 | ssize_t max_block_size = atoi(optarg); |
| 356 | if (max_block_size < 1024 || max_block_size > 16*1024*1024) |
| 357 | errx(1, "Invalid blocksize, must be between 1024 and %d", 16*1024*1024); |
| 358 | tape.setMaxBlockSize(max_block_size); |
| 359 | break; |
| 360 | } |
| 361 | case 'd': { |
| 362 | tape.trace(true); |
| 363 | break; |
| 364 | } |
| 365 | default: |
| 366 | std::cerr << "Unexpected " << ch << std::endl; |
| 367 | usage(prog); |
| 368 | } |
| 369 | argc -= optind; |
| 370 | argv += optind; |
| 371 | |
| 372 | if (argc > 1) |
| 373 | usage(prog); |
| 374 | |
| 375 | if (argc == 1) { |
| 376 | tape.debug = std::make_unique<std::ofstream>(); |
| 377 | tape.debug->rdbuf()->pubsetbuf(0, 0); |
| 378 | static_cast<std::ofstream*>(tape.debug.get())->open(argv[0]); |
| 379 | if (!tape.debug) |
| 380 | err(1, "Can't open debug log %s", argv[0]); |
| 381 | } |
| 382 | |
| 383 | while(process_command()) |
| 384 | /* do nothing */ ; |
| 385 | |
| 386 | process_time.reset(); |
| 387 | open_time.reset(); |
| 388 | close_time.reset(); |
| 389 | write_time.reset(); |
| 390 | read_time.reset(); |
| 391 | } |
| 392 | |
| 393 | static void getstring(std::string& s) |
| 394 | { |
| 395 | while(true) { |
| 396 | char cp; |
| 397 | if (read(0, &cp, 1) != 1) |
| 398 | err(1, "EOF while reading parameter: aborting"); |
| 399 | if (cp == '\n') |
| 400 | break; |
| 401 | s += cp; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | |
| 406 | template<typename T> |
| 407 | static bool convertstring(const std::string& s, T& v) { |
| 408 | auto [ ptr, ec ] = std::from_chars(s.data(), s.data()+s.size(), v); |
| 409 | if (ec != std::errc{}) { |
| 410 | errno = EINVAL; |
| 411 | return false; |
| 412 | } |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | /* vim: set sw=8 sts=8 ts=8 noexpandtab: */ |