1 /* 2 * Copyright (c) 2008 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.12 2008/10/08 21:01:54 thomas Exp $ 35 */ 36 37 #include <libgen.h> 38 39 #include "hammer.h" 40 41 static int scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path); 42 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd); 43 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave); 44 static void pseudofs_usage(int code); 45 static int timetosecs(char *str); 46 47 void 48 clrpfs(struct hammer_ioc_pseudofs_rw *pfs, hammer_pseudofs_data_t pfsd, 49 int pfs_id) 50 { 51 bzero(pfs, sizeof(*pfs)); 52 53 if (pfsd) 54 pfs->ondisk = pfsd; 55 else 56 pfs->ondisk = malloc(sizeof(*pfs->ondisk)); 57 bzero(pfs->ondisk, sizeof(*pfs->ondisk)); 58 59 pfs->pfs_id = pfs_id; 60 pfs->bytes = sizeof(*pfs->ondisk); 61 pfs->version = HAMMER_IOC_PSEUDOFS_VERSION; 62 } 63 64 /* 65 * If path is a symlink, return strdup'd path. 66 * If it's a directory via symlink, strip trailing / 67 * from strdup'd path and return the symlink. 68 */ 69 static char* 70 getlink(const char *path) 71 { 72 int i; 73 char *linkpath; 74 struct stat st; 75 76 if (lstat(path, &st)) 77 return(NULL); 78 linkpath = strdup(path); 79 80 if (S_ISDIR(st.st_mode)) { 81 i = strlen(linkpath) - 1; 82 while (i > 0 && linkpath[i] == '/') 83 linkpath[i--] = 0; 84 lstat(linkpath, &st); 85 } 86 if (S_ISLNK(st.st_mode)) 87 return(linkpath); 88 89 free(linkpath); 90 return(NULL); 91 } 92 93 /* 94 * Calculate the PFS id given a path to a file/directory or 95 * a @@%llx:%d softlink. 96 */ 97 int 98 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path) 99 { 100 int fd; 101 102 clrpfs(pfs, NULL, -1); 103 104 /* 105 * Extract the PFS id. 106 * dirname(path) is supposed to be a directory in root PFS. 107 */ 108 if (scanpfsid(pfs, path) == 0) 109 path = dirname(path); /* strips trailing / first if any */ 110 111 /* 112 * Open the path regardless of scanpfsid() result, since some 113 * commands can take a regular file/directory (e.g. pfs-status). 114 */ 115 fd = open(path, O_RDONLY); 116 if (fd < 0) 117 err(1, "Failed to open %s", path); 118 119 /* 120 * If pfs.pfs_id has been set to non -1, the file descriptor fd 121 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS 122 * doesn't depend on inode attributes if it's set to a valid id. 123 */ 124 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) 125 err(1, "Cannot access %s", path); 126 127 return(fd); 128 } 129 130 /* 131 * Extract the PFS id from path. 132 */ 133 static int 134 scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path) 135 { 136 char *linkpath; 137 char buf[64]; 138 uintmax_t dummy_tid; 139 struct stat st; 140 141 if (stat(path, &st)) 142 ; /* possibly slave PFS */ 143 else if (S_ISDIR(st.st_mode)) 144 ; /* possibly master or slave PFS */ 145 else 146 return(-1); /* neither */ 147 148 linkpath = getlink(path); 149 if (linkpath) { 150 /* 151 * Read the symlink assuming it's a link to PFS. 152 */ 153 bzero(buf, sizeof(buf)); 154 if (readlink(linkpath, buf, sizeof(buf) - 1) < 0) { 155 free(linkpath); 156 return(-1); 157 } 158 free(linkpath); 159 path = buf; 160 } 161 162 /* 163 * The symlink created by pfs-master|slave is just a symlink. 164 * One could happen to remove a symlink and relink PFS as 165 * # ln -s ./@@-1:00001 ./link 166 * which results PFS having something extra before @@. 167 * One could also directly use the PFS and results the same. 168 * Get rid of it before we extract the PFS id. 169 */ 170 if (strchr(path, '/')) { 171 path = basename(path); /* strips trailing / first if any */ 172 if (path == NULL) 173 err(1, "basename"); 174 } 175 176 /* 177 * Test and extract the PFS id from the link. 178 * "@@%jx:%d" covers both "@@-1:%05d" format for master PFS 179 * and "@@0x%016jx:%05d" format for slave PFS. 180 */ 181 if (sscanf(path, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) { 182 assert(pfs->pfs_id > 0); 183 return(0); 184 } 185 186 return(-1); 187 } 188 189 void 190 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs) 191 { 192 if (fd >= 0) 193 close(fd); 194 if (pfs->ondisk) { 195 free(pfs->ondisk); 196 pfs->ondisk = NULL; 197 } 198 } 199 200 static void 201 print_pfs_status(char *path) 202 { 203 struct hammer_ioc_pseudofs_rw pfs; 204 int fd; 205 206 fd = getpfs(&pfs, path); 207 printf("%s\t", path); 208 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) { 209 printf("Invalid PFS path %s\n", path); 210 } else { 211 printf("PFS#%d {\n", pfs.pfs_id); 212 dump_pfsd(pfs.ondisk, fd); 213 printf("}\n"); 214 } 215 if (fd >= 0) 216 close(fd); 217 if (pfs.ondisk) 218 free(pfs.ondisk); 219 relpfs(fd, &pfs); 220 } 221 222 void 223 hammer_cmd_pseudofs_status(char **av, int ac) 224 { 225 int i; 226 227 if (ac == 0) { 228 char buf[2] = "."; /* can't be readonly string */ 229 print_pfs_status(buf); 230 return; 231 } 232 233 for (i = 0; i < ac; ++i) 234 print_pfs_status(av[i]); 235 } 236 237 void 238 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave) 239 { 240 struct hammer_ioc_pseudofs_rw pfs; 241 struct hammer_pseudofs_data pfsd; 242 struct stat st; 243 const char *path; 244 char *dirpath; 245 char *linkpath; 246 int pfs_id; 247 int fd; 248 249 if (ac == 0) 250 pseudofs_usage(1); 251 path = av[0]; 252 if (lstat(path, &st) == 0) 253 errx(1, "Cannot create %s, file exists!", path); 254 else if (path[strlen(path) - 1] == '/') 255 errx(1, "Invalid PFS path %s with trailing /", path); 256 257 /* 258 * Figure out the directory prefix, taking care of degenerate 259 * cases. 260 */ 261 dirpath = dirname(path); 262 fd = open(dirpath, O_RDONLY); 263 if (fd < 0) 264 err(1, "Cannot open directory %s", dirpath); 265 266 /* 267 * Avoid foot-shooting. Don't let the user create a PFS 268 * softlink via a PFS. PFS softlinks may only be accessed 269 * via the master filesystem. Checking it here ensures 270 * other PFS commands access PFS under the master filesystem. 271 */ 272 clrpfs(&pfs, &pfsd, -1); 273 274 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs); 275 if (pfs.pfs_id != HAMMER_ROOT_PFSID) { 276 fprintf(stderr, 277 "You are attempting to access a PFS softlink " 278 "from a PFS. It may not represent the PFS\n" 279 "on the main filesystem mount that you " 280 "expect! You may only access PFS softlinks\n" 281 "via the main filesystem mount!\n"); 282 exit(1); 283 } 284 285 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) { 286 clrpfs(&pfs, &pfsd, pfs_id); 287 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) { 288 if (errno != ENOENT) 289 err(1, "Cannot create %s", path); 290 break; 291 } 292 } 293 if (pfs_id == HAMMER_MAX_PFS) 294 errx(1, "Cannot create %s, all PFSs in use", path); 295 else if (pfs_id == HAMMER_ROOT_PFSID) 296 errx(1, "Fatal error: PFS#%d must exist", HAMMER_ROOT_PFSID); 297 298 /* 299 * Create the new PFS 300 */ 301 printf("Creating PFS#%d\t", pfs_id); 302 clrpfs(&pfs, &pfsd, pfs_id); 303 init_pfsd(&pfsd, is_slave); 304 305 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) { 306 printf("failed: %s\n", strerror(errno)); 307 } else { 308 /* special symlink, must be exactly 10 characters */ 309 asprintf(&linkpath, "@@PFS%05d", pfs_id); 310 if (symlink(linkpath, path) < 0) { 311 printf("failed: cannot create symlink: %s\n", 312 strerror(errno)); 313 } else { 314 printf("succeeded!\n"); 315 hammer_cmd_pseudofs_update(av, ac); 316 } 317 } 318 free(dirpath); 319 close(fd); 320 } 321 322 void 323 hammer_cmd_pseudofs_destroy(char **av, int ac) 324 { 325 struct hammer_ioc_pseudofs_rw pfs; 326 char *linkpath; 327 int fd; 328 int i; 329 330 if (ac == 0) 331 pseudofs_usage(1); 332 fd = getpfs(&pfs, av[0]); 333 334 if (pfs.pfs_id == HAMMER_ROOT_PFSID) 335 errx(1, "You cannot destroy PFS#%d", HAMMER_ROOT_PFSID); 336 337 printf("You have requested that PFS#%d (%s) be destroyed\n", 338 pfs.pfs_id, pfs.ondisk->label); 339 printf("This will irrevocably destroy all data on this PFS!!!!!\n"); 340 printf("Do you really want to do this? [y/n] "); 341 fflush(stdout); 342 if (getyn() == 0) 343 errx(1, "No action taken on PFS#%d", pfs.pfs_id); 344 345 if (hammer_is_pfs_master(pfs.ondisk)) { 346 printf("This PFS is currently setup as a MASTER!\n"); 347 printf("Are you absolutely sure you want to destroy it? [y/n] "); 348 fflush(stdout); 349 if (getyn() == 0) 350 errx(1, "No action taken on PFS#%d", pfs.pfs_id); 351 } 352 353 printf("Destroying PFS#%d (%s)", pfs.pfs_id, pfs.ondisk->label); 354 if (DebugOpt) { 355 printf("\n"); 356 } else { 357 printf(" in"); 358 for (i = 5; i; --i) { 359 printf(" %d", i); 360 fflush(stdout); 361 sleep(1); 362 } 363 printf(".. starting destruction pass\n"); 364 } 365 366 /* 367 * Remove the softlink on success. 368 */ 369 if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) { 370 printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id); 371 linkpath = getlink(av[0]); 372 if (linkpath) { 373 if (remove(linkpath) < 0) 374 err(1, "Unable to remove softlink %s", linkpath); 375 free(linkpath); 376 } 377 } else { 378 printf("pfs-destroy of PFS#%d failed: %s\n", 379 pfs.pfs_id, strerror(errno)); 380 } 381 relpfs(fd, &pfs); 382 } 383 384 void 385 hammer_cmd_pseudofs_upgrade(char **av, int ac) 386 { 387 struct hammer_ioc_pseudofs_rw pfs; 388 int fd; 389 390 if (ac == 0) 391 pseudofs_usage(1); 392 fd = getpfs(&pfs, av[0]); 393 394 if (pfs.pfs_id == HAMMER_ROOT_PFSID) { 395 errx(1, "You cannot upgrade PFS#%d" 396 " (It should already be a master)", 397 HAMMER_ROOT_PFSID); 398 } else if (hammer_is_pfs_master(pfs.ondisk)) { 399 errx(1, "It is already a master"); 400 } 401 402 if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) { 403 printf("pfs-upgrade of PFS#%d (%s) succeeded\n", 404 pfs.pfs_id, pfs.ondisk->label); 405 } else { 406 err(1, "pfs-upgrade of PFS#%d (%s) failed", 407 pfs.pfs_id, pfs.ondisk->label); 408 } 409 relpfs(fd, &pfs); 410 } 411 412 void 413 hammer_cmd_pseudofs_downgrade(char **av, int ac) 414 { 415 struct hammer_ioc_pseudofs_rw pfs; 416 int fd; 417 418 if (ac == 0) 419 pseudofs_usage(1); 420 fd = getpfs(&pfs, av[0]); 421 422 if (pfs.pfs_id == HAMMER_ROOT_PFSID) 423 errx(1, "You cannot downgrade PFS#%d", HAMMER_ROOT_PFSID); 424 else if (hammer_is_pfs_slave(pfs.ondisk)) 425 errx(1, "It is already a slave"); 426 427 if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) { 428 printf("pfs-downgrade of PFS#%d (%s) succeeded\n", 429 pfs.pfs_id, pfs.ondisk->label); 430 } else { 431 err(1, "pfs-downgrade of PFS#%d (%s) failed", 432 pfs.pfs_id, pfs.ondisk->label); 433 } 434 relpfs(fd, &pfs); 435 } 436 437 void 438 hammer_cmd_pseudofs_update(char **av, int ac) 439 { 440 struct hammer_ioc_pseudofs_rw pfs; 441 int fd; 442 443 if (ac == 0) 444 pseudofs_usage(1); 445 fd = getpfs(&pfs, av[0]); 446 447 printf("%s\n", av[0]); 448 fflush(stdout); 449 450 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) { 451 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk); 452 if (hammer_is_pfs_slave(pfs.ondisk) && 453 pfs.pfs_id == HAMMER_ROOT_PFSID) { 454 errx(1, "The real mount point cannot be made a PFS " 455 "slave, only PFS sub-directories can be made " 456 "slaves"); 457 } 458 pfs.bytes = sizeof(*pfs.ondisk); 459 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) { 460 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) { 461 dump_pfsd(pfs.ondisk, fd); 462 } else { 463 err(1, "Unable to retrieve PFS configuration " 464 "after successful update"); 465 } 466 } else { 467 err(1, "Unable to adjust PFS configuration"); 468 } 469 } 470 relpfs(fd, &pfs); 471 } 472 473 static void 474 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave) 475 { 476 uint32_t status; 477 478 bzero(pfsd, sizeof(*pfsd)); 479 pfsd->sync_beg_tid = 1; 480 pfsd->sync_end_tid = 1; 481 pfsd->sync_beg_ts = 0; 482 pfsd->sync_end_ts = 0; 483 uuid_create(&pfsd->shared_uuid, &status); 484 uuid_create(&pfsd->unique_uuid, &status); 485 if (is_slave) 486 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE; 487 } 488 489 void 490 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd) 491 { 492 struct hammer_ioc_version version; 493 uint32_t status; 494 char *str = NULL; 495 496 printf(" sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid); 497 printf(" sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid); 498 uuid_to_string(&pfsd->shared_uuid, &str, &status); 499 printf(" shared-uuid=%s\n", str); 500 free(str); 501 uuid_to_string(&pfsd->unique_uuid, &str, &status); 502 printf(" unique-uuid=%s\n", str); 503 free(str); 504 printf(" label=\"%s\"\n", pfsd->label); 505 if (pfsd->snapshots[0]) 506 printf(" snapshots=\"%s\"\n", pfsd->snapshots); 507 if (pfsd->prune_min < (60 * 60 * 24)) { 508 printf(" prune-min=%02d:%02d:%02d\n", 509 pfsd->prune_min / 60 / 60 % 24, 510 pfsd->prune_min / 60 % 60, 511 pfsd->prune_min % 60); 512 } else if (pfsd->prune_min % (60 * 60 * 24)) { 513 printf(" prune-min=%dd/%02d:%02d:%02d\n", 514 pfsd->prune_min / 60 / 60 / 24, 515 pfsd->prune_min / 60 / 60 % 24, 516 pfsd->prune_min / 60 % 60, 517 pfsd->prune_min % 60); 518 } else { 519 printf(" prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24); 520 } 521 522 if (hammer_is_pfs_slave(pfsd)) 523 printf(" operating as a SLAVE\n"); 524 else 525 printf(" operating as a MASTER\n"); 526 527 /* 528 * Snapshots directory cannot be shown when there is no fd since 529 * hammer version can't be retrieved. mirror-dump passes -1 because 530 * its input came from mirror-read output thus no path is available 531 * to open(2). 532 */ 533 if (fd >= 0 && pfsd->snapshots[0] == 0) { 534 bzero(&version, sizeof(version)); 535 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) 536 return; 537 HammerVersion = version.cur_version; 538 if (version.cur_version < 3) { 539 if (hammer_is_pfs_slave(pfsd)) { 540 printf(" snapshots directory not set for " 541 "slave\n"); 542 } else { 543 printf(" snapshots directory for master " 544 "defaults to <pfs>/snapshots\n"); 545 } 546 } else { 547 printf(" snapshots directory defaults to " 548 "/var/hammer/<pfs>\n"); 549 } 550 } 551 } 552 553 static void 554 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd) 555 { 556 char *cmd; 557 char *ptr; 558 int len; 559 uint32_t status; 560 561 while (ac) { 562 cmd = *av; 563 if ((ptr = strchr(cmd, '=')) != NULL) 564 *ptr++ = 0; 565 566 /* 567 * Basic assignment value test 568 */ 569 if (ptr == NULL) 570 errx(1, "option %s requires an assignment", cmd); 571 572 status = uuid_s_ok; 573 if (strcmp(cmd, "sync-beg-tid") == 0) { 574 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16); 575 } else if (strcmp(cmd, "sync-end-tid") == 0) { 576 pfsd->sync_end_tid = strtoull(ptr, NULL, 16); 577 } else if (strcmp(cmd, "shared-uuid") == 0) { 578 uuid_from_string(ptr, &pfsd->shared_uuid, &status); 579 } else if (strcmp(cmd, "unique-uuid") == 0) { 580 uuid_from_string(ptr, &pfsd->unique_uuid, &status); 581 } else if (strcmp(cmd, "label") == 0) { 582 len = strlen(ptr); 583 if (ptr[0] == '"' && ptr[len-1] == '"') { 584 ptr[len-1] = 0; 585 ++ptr; 586 } else if (ptr[0] == '"') { 587 errx(1, "option %s: malformed string", cmd); 588 } 589 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr); 590 } else if (strcmp(cmd, "snapshots") == 0) { 591 len = strlen(ptr); 592 if (ptr[0] != '/') { 593 fprintf(stderr, 594 "option %s: '%s' must be an " 595 "absolute path\n", cmd, ptr); 596 if (ptr[0] == 0) { 597 fprintf(stderr, 598 "use 'snapshots-clear' " 599 "to unset snapshots dir\n"); 600 } 601 exit(1); 602 } 603 if (len >= (int)sizeof(pfsd->snapshots)) { 604 errx(1, "option %s: path too long, %d " 605 "character limit", cmd, len); 606 } 607 snprintf(pfsd->snapshots, sizeof(pfsd->snapshots), 608 "%s", ptr); 609 } else if (strcmp(cmd, "snapshots-clear") == 0) { 610 pfsd->snapshots[0] = 0; 611 } else if (strcmp(cmd, "prune-min") == 0) { 612 pfsd->prune_min = timetosecs(ptr); 613 if (pfsd->prune_min < 0) { 614 errx(1, "option %s: illegal time spec, " 615 "use Nd or [Nd/]hh[:mm[:ss]]", ptr); 616 } 617 } else { 618 errx(1, "invalid option: %s", cmd); 619 } 620 if (status != uuid_s_ok) 621 errx(1, "option %s: error parsing uuid %s", cmd, ptr); 622 --ac; 623 ++av; 624 } 625 } 626 627 static 628 void 629 pseudofs_usage(int code) 630 { 631 fprintf(stderr, 632 "hammer pfs-status <dirpath> ...\n" 633 "hammer pfs-master <dirpath> [options]\n" 634 "hammer pfs-slave <dirpath> [options]\n" 635 "hammer pfs-update <dirpath> [options]\n" 636 "hammer pfs-upgrade <dirpath>\n" 637 "hammer pfs-downgrade <dirpath>\n" 638 "hammer pfs-destroy <dirpath>\n" 639 "\n" 640 "options:\n" 641 " sync-beg-tid=0x16llx\n" 642 " sync-end-tid=0x16llx\n" 643 " shared-uuid=0x16llx\n" 644 " unique-uuid=0x16llx\n" 645 " label=\"string\"\n" 646 " snapshots=\"/path\"\n" 647 " snapshots-clear\n" 648 " prune-min=Nd\n" 649 " prune-min=[Nd/]hh[:mm[:ss]]\n" 650 ); 651 exit(code); 652 } 653 654 /* 655 * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds. 656 * 657 * Return -1 if a parse error occurs. 658 * Return 0x7FFFFFFF if the time exceeds the maximum allowed. 659 */ 660 static 661 int 662 timetosecs(char *str) 663 { 664 int days = 0; 665 int hrs = 0; 666 int mins = 0; 667 int secs = 0; 668 int n; 669 long long v; 670 char *ptr; 671 672 n = strtol(str, &ptr, 10); 673 if (n < 0) 674 return(-1); 675 if (*ptr == 'd') { 676 days = n; 677 ++ptr; 678 if (*ptr == '/') 679 n = strtol(ptr + 1, &ptr, 10); 680 else 681 n = 0; 682 } 683 if (n < 0) 684 return(-1); 685 hrs = n; 686 if (*ptr == ':') { 687 n = strtol(ptr + 1, &ptr, 10); 688 if (n < 0) 689 return(-1); 690 mins = n; 691 if (*ptr == ':') { 692 n = strtol(ptr + 1, &ptr, 10); 693 if (n < 0) 694 return(-1); 695 secs = n; 696 } 697 } 698 if (*ptr) 699 return(-1); 700 v = days * 24 * 60 * 60 + hrs * 60 * 60 + mins * 60 + secs; 701 if (v > 0x7FFFFFFF) 702 v = 0x7FFFFFFF; 703 return((int)v); 704 } 705