since.c 40.3 KB
Newer Older
1 2
/* (c) 1998 - 2009 Marc Welz */

3 4
#include <stdio.h>
#include <string.h>
5 6 7
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>
8
#include <errno.h>
9
#include <fcntl.h>
10 11
#include <ctype.h>
#include <limits.h>
12
#include <signal.h>
13 14
#include <time.h>
#include <pwd.h>
15

16
#include <sys/mman.h>
17
#include <sys/types.h>
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
#include <sys/stat.h>
#ifdef USE_INOTIFY
#include <sys/inotify.h>
#endif

/* for embedded or broken systems where no home exists */
#define SINCE_FALLBACK "/tmp/since"

/* default perms */
#define SINCE_MASK (S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP)

/* max format/data string */
#define MAX_FMT 100

/* number of chars to search back for a newline */
#define LINE_SEARCH 160

/* this is used to compute a valid offset for mmapping data files */
/* apparently one should use getpagesize() instead */
#ifdef PAGE_SIZE
#define IO_BUFFER PAGE_SIZE
#else
#define IO_BUFFER 4096
#endif

struct fmt_map{
  int f_bytes;
  char *f_string;
};

struct data_file{
  int d_fd;
  char *d_name;
  dev_t d_dev;
  ino_t d_ino;
  off_t d_had;
  off_t d_now;
  off_t d_pos;
  int d_offset;
  int d_notify;
  unsigned char d_jump:1;
  unsigned char d_write:1;
  unsigned char d_deleted:1;
  unsigned char d_replaced:1;
  unsigned char d_notable:1;
  unsigned char d_moved:1;
};

struct since_state{
  int s_disk_device;
  int s_disk_inode;
  int s_disk_size;

  int s_arch_device;
  int s_arch_inode;
  int s_arch_size;

  char s_fmt[MAX_FMT];
  int s_fmt_used;
  int s_fmt_output;
  int s_fmt_prefix;

  int s_error;
  int s_readonly;
  int s_verbose;
  int s_relaxed;
  int s_follow;
  int s_discard;
  struct timespec s_delay;
  int s_atomic;
  int s_domap;
  int s_nozip;

  char *s_name;
  int s_fd;
93

94 95
  int s_size;
  char *s_buffer;
96

97
  int s_ismap;
98

99 100
  int s_add;
  char *s_append;
101

102 103
  struct data_file *s_data_files;
  unsigned int s_data_count;
104

105 106
  sigset_t s_set;
  int s_notify;
107

108 109
  FILE *s_header;
};
110

111 112 113 114 115 116 117
struct fmt_map fmt_table[] = {
  { sizeof(unsigned int), "x" },
  { sizeof(unsigned long int), "lx" },
  { sizeof(unsigned long long int), "llx" },
  { sizeof(unsigned short int), "hx" },
  { 0, NULL }
};
118

119 120 121 122 123 124 125 126 127 128
/************************************************************/

static void forget_state_file(struct since_state *sn);
static int tmp_state_file(struct since_state *sn, int (*call)(struct since_state *sn));

volatile int since_run = 1;

/* misc supporting functions ********************************/

void handle_signal(int s)
129
{
130 131
  since_run = 0;
}
132

133 134 135 136 137 138 139 140 141 142 143 144
static void forget_state_file(struct since_state *sn)
{
  if(sn->s_buffer){
    if(sn->s_ismap){
      munmap(sn->s_buffer, sn->s_size);
      sn->s_ismap = 0;
    } else {
      free(sn->s_buffer);
    }
    sn->s_buffer = NULL;
  }
  sn->s_size = 0;
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
  if(sn->s_append){
    free(sn->s_append);
    sn->s_append = NULL;
  }
  sn->s_add = 0;
}

static int tmp_state_file(struct since_state *sn, int (*call)(struct since_state *sn))
{
  char canon[PATH_MAX], tmp[PATH_MAX], *tptr;
  int result, tfd, nfd, mode, flags;

  if(realpath(sn->s_name, canon) == NULL){
    fprintf(stderr, "since: unable to establish true location of %s: %s\n", sn->s_name, strerror(errno));
    return -1;
  }

  result = snprintf(tmp, PATH_MAX, "%s.%d", canon, getpid());
  if((result < 0) || (result >= PATH_MAX)){
    fprintf(stderr, "since: tmp file of %s exeeded limits\n", canon);
    return -1;
  }

  if(sn->s_verbose > 1){
    fprintf(stderr, "since: creating tmp file %s\n", tmp);
  }

  mode = SINCE_MASK;
  flags = O_RDWR | O_CREAT | O_EXCL;
#ifdef O_NOFOLLOW
  flags |= O_NOFOLLOW;
177
#endif
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

  nfd = open(tmp, flags, mode);
  if(nfd < 0){
    fprintf(stderr, "since: unable to create tmp file %s: %s\n", tmp, strerror(errno));
    return -1;
  }

  tfd = sn->s_fd;
  tptr = sn->s_name;

  sn->s_fd = nfd;
  sn->s_name = tmp;

  result = (*call)(sn);

  sn->s_fd = tfd;
  sn->s_name = tptr;

  if(result < 0){
    close(nfd);
    unlink(tmp);
  }

  if(rename(tmp, sn->s_name)){
    fprintf(stderr, "since: unable to rename %s to %s: %s\n", tmp, sn->s_name, strerror(errno));
    close(nfd);
    unlink(tmp);
    return -1;
  }

  sn->s_fd = nfd;
  close(tfd);
210 211 212

  return 0;
}
213

214
/* sincefile stuff ******************************************/
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
static void init_state(struct since_state *sn)
{
  struct stat st;

  sn->s_arch_device = sizeof(st.st_dev);
  sn->s_arch_inode = sizeof(st.st_ino);
  sn->s_arch_size = sizeof(st.st_size);

  sn->s_disk_device = sn->s_arch_device;
  sn->s_disk_inode = sn->s_arch_inode;
  sn->s_disk_size = sn->s_arch_size;

  sn->s_fmt_used = 0;
  sn->s_fmt_output = 0;
  sn->s_fmt_prefix = 0;

  sn->s_error = 0;
  sn->s_readonly = 0;
  sn->s_verbose = 1;
  sn->s_relaxed = 0;
  sn->s_follow = 0;
  sn->s_discard = 0;
  sn->s_delay.tv_sec = 1;
  sn->s_delay.tv_nsec = 0;
  sn->s_atomic = 0;
  sn->s_domap = 1;
  sn->s_nozip = 0;

  sn->s_name = NULL;
  sn->s_fd = (-1);

  sn->s_size = 0;
  sn->s_buffer = NULL;
  sn->s_ismap = 0;

  sn->s_add = 0;
  sn->s_append = NULL;

  sn->s_data_files = NULL;
  sn->s_data_count = 0;

  sn->s_notify = (-1);

  sn->s_header = stdout;
}

void destroy_state(struct since_state *sn)
263
{
264
  int i;
265 266 267
  struct data_file *df;

  forget_state_file(sn);
268

269 270 271 272 273 274 275 276
  if(sn->s_data_files){
    for(i = 0; i < sn->s_data_count; i++){
      df = &(sn->s_data_files[i]);
      if(df->d_fd >= 0){
        close(df->d_fd);
        df->d_fd = (-1);
      }
      df->d_offset = (-1);
277
    }
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    free(sn->s_data_files);
    sn->s_data_files = NULL;
  }
  sn->s_data_count = 0;

  if(sn->s_notify >= 0){
    close(sn->s_notify);
    sn->s_notify = (-1);
  }

  if(sn->s_fd >= 0){
    close(sn->s_fd);
    sn->s_fd = (-1);
  }

  if(sn->s_name){
    free(sn->s_name);
    sn->s_name = NULL;
  }
}
298

299
/* open state files *****************************************/
300

301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
static int try_state_file(struct since_state *sn, char *path, char *append, int more)
{
  int flags, mode, plen, alen, result;
  char *tmp;

  if(path == NULL){
    return -1;
  }

  if(append){
    plen = strlen(path);
    alen = strlen(append);
    tmp = malloc(plen + alen + 2);
    if(tmp == NULL){
      fprintf(stderr, "since: unable to allocate %d bytes\n", plen + alen + 2);
      return -1;
317
    }
318 319
    if((plen > 0) && (path[plen - 1] == '/')){
      plen--;
320
    }
321 322 323 324 325 326 327 328
    strcpy(tmp, path);
    tmp[plen] = '/';
    strcpy(tmp + plen + 1, append);
  } else {
    tmp = strdup(path);
    if(tmp == NULL){
      fprintf(stderr, "since: unable to duplicate %s\n", path);
      return -1;
329
    }
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  }
  sn->s_name = tmp;

  flags = (sn->s_readonly ? O_RDONLY : (O_RDWR | O_CREAT)) | more;

  mode = SINCE_MASK;

  if(sn->s_verbose > 2){
    fprintf(stderr, "since: attempting to open %s\n", sn->s_name);
  }

  sn->s_fd = open(sn->s_name, flags, mode);
  if(sn->s_fd >= 0){
    if(sn->s_verbose > 1){
      fprintf(stderr, "since: opened %s\n", sn->s_name);
345
    }
346 347 348 349 350 351 352 353
    return 0;
  }

  result = -1;

  if(errno == ENOENT){
    if(sn->s_readonly){
      result = 1;
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
  }

  if(result < 0){
    fprintf(stderr, "since: unable to open %s: %s\n", sn->s_name, strerror(errno));
  }

  free(sn->s_name);
  sn->s_name = NULL;

  return result;
}

static int open_state_file(struct since_state *sn, char *name)
{
  char *ptr;
  int more;
  struct passwd *pw;
  int result;

  if(name){
    return try_state_file(sn, name, NULL, 0);
  }

  ptr = getenv("SINCE");
  if((result = try_state_file(sn, ptr, NULL, 0)) >= 0){
    return result;
  }

  ptr = getenv("HOME");
  if(ptr == NULL){
    pw = getpwuid(getuid());
    if(pw){
      ptr = pw->pw_dir;
388
    }
389 390 391 392
  }
  if((result = try_state_file(sn, ptr, ".since", 0)) >= 0){
    return result;
  }
393

394 395 396 397 398 399 400 401 402 403 404
#ifdef O_NOFOLLOW
  more = O_NOFOLLOW;
#else
  more = 0;
#endif
  if((result = try_state_file(sn, SINCE_FALLBACK, NULL, more)) >= 0){
    if(sn->s_verbose > 0){
      fprintf(stderr, "since: using fallback %s\n", SINCE_FALLBACK);
    }
    return result;
  }
405

406 407
  fprintf(stderr, "since: unable to open any state file\n");
  return -1;
408 409
}

410
/* acquire content of state file ****************************/
411

412
static int load_state_file(struct since_state *sn)
413
{
414
  struct stat st;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  int prot, flags;
  int rr;
  /* we assume that state file can fit into address space */
  unsigned int rt;

#ifdef DEBUG
  if(sn->s_buffer){
    fprintf(stderr, "since: logic problem: loading over unallocated buffer\n");
    abort();
  }
#endif

  sn->s_ismap = 0;

  if(sn->s_fd < 0){
    /* where there is no state file and invoked as readonly */
    return 1;
  }

  if(fstat(sn->s_fd, &st)){
    fprintf(stderr, "since: unable to stat %s: %s\n", sn->s_name, strerror(errno));
    return -1;
  }

  sn->s_size = st.st_size;
  if(sn->s_size == 0){
    return 0; /* empty, possibly new file */
  }

  if(sn->s_domap){
    /* not really clear if this buys us anything, actually may make updates non-atomic as we rely on mmap to write back changes */
    prot = PROT_READ;
    if(!(sn->s_readonly)) prot |= PROT_WRITE;
    flags = sn->s_atomic ? MAP_PRIVATE : MAP_SHARED;
    sn->s_buffer = mmap(NULL, sn->s_size, prot, flags, sn->s_fd, 0);
    if((void *)(sn->s_buffer) != MAP_FAILED){
#ifdef DEBUG
      fprintf(stderr, "since: have mapped %s (size=%u)\n", sn->s_name, sn->s_size);
#endif
      sn->s_ismap = 1;
      return 0;
    }
  }

  sn->s_buffer = malloc(sn->s_size);
  if(sn->s_buffer == NULL){
    fprintf(stderr, "since: unable to allocate %d bytes to load state file %s: %s\n", sn->s_size, sn->s_name, strerror(errno));
    return -1;
  }

  for(rt = 0; rt < sn->s_size;){
    rr = read(sn->s_fd, sn->s_buffer + rt, sn->s_size - rt);
    if(rr < 0){
      switch(errno){
        case EAGAIN :
        case EINTR :
          break;
        default :
          fprintf(stderr, "since: read of %s failed after %d bytes: %s\n", sn->s_name, rt, strerror(errno));
          return -1;
      }
    }
    if(rr == 0){
      fprintf(stderr, "since: premature end of %s: %d bytes outstanding\n", sn->s_name, (unsigned int)(sn->s_size) - rt);
      return -1;
    }
    rt += rr;
  }

#ifdef DEBUG
  fprintf(stderr, "since: have read %s (size=%u)\n", sn->s_name, sn->s_size);
#endif

  return 0;
}
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
/* test if state file is sane *******************************/

static int check_state_file(struct since_state *sn)
{
  int i, x, w, d, sep[3];

  sep[0] = ':';
  sep[1] = ':';
  sep[2] = '\n';

  if((sn->s_buffer == NULL) || (sn->s_size == 0)){
    if(sn->s_verbose > 2){
      fprintf(stderr, "since: will not check an empty or nonexistant file\n");
    }
    return 1;
  }

  w = 0;

  for(i = 0, x = 0; (i < sn->s_size) && (x < 3); i++){
    if(isxdigit(sn->s_buffer[i])){
      /* nothing */
    } else if(sn->s_buffer[i] == sep[x]){
      d = (i - w);
      if(d % 2){
        fprintf(stderr, "since: data field has to contain an even number of bytes, not %d\n", d);
        return -1;
      }
      d = d / 2;
520
#ifdef DEBUG
521
      fprintf(stderr, "check: field[%d] is %d bytes\n", x, d);
522
#endif
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
      switch(x){
        case 0 : sn->s_disk_device = d; break;
        case 1 : sn->s_disk_inode = d; break;
        case 2 : sn->s_disk_size = d; break;
      }
      x++;
      w = i + 1;
    } else {
      fprintf(stderr, "since: corrupt state file %s at %d\n", sn->s_name, i);
      return -1;
    }
  }

  if(x < 3){
    fprintf(stderr, "since: no fields within %d bytes in file %s\n", i, sn->s_name);
    return -1;
  }

  return 0;
}

/* rewrite state file if field size changes *****************/

static int internal_upgrade_state_file(struct since_state *sn)
{
  char line[MAX_FMT], *ptr;
  int result, sw, sr, i, k, x, pad[3], end[3], sep[3];

#define bounded_delta(x, y)   (((x) < (y)) ? 0 : ((x) - (y)))

  pad[0] = bounded_delta(sn->s_arch_device, sn->s_disk_device) * 2;
  pad[1] = bounded_delta(sn->s_arch_inode, sn->s_disk_inode) * 2;
  pad[2] = bounded_delta(sn->s_arch_size, sn->s_disk_size) * 2;

  end[0] = sn->s_disk_device * 2;
  end[1] = sn->s_disk_inode * 2;
  end[2] = sn->s_disk_size * 2;

  sep[0] = ':';
  sep[1] = ':';
  sep[2] = '\n';

#undef bounded_delta

  sw = 0;
  sr = 0;
  for(x = 0; x < 3; x++){
    sw += pad[x] + end[x] + 1;
    sr += end[x] + 1;
#ifdef DEBUG
    fprintf(stderr, "since: padding[%d]: have=%d, expand=%d\n", x, end[x], pad[x]);
#endif
  }
#ifdef DEBUG
  fprintf(stderr, "since: total: %d->%d\n", sr, sw);
#endif
  if(sw >= MAX_FMT){
    fprintf(stderr, "since: major logic failure: want to rewrite to %d, limit at %d\n", sw, MAX_FMT);
    return -1;
  }

  for(i = 0; i < sn->s_size; i+= sr){
    ptr = sn->s_buffer + i;
    k = 0;
    for(x = 0; x < 3; x++){
      if(pad[x] > 0){
        memset(line + k, '0', pad[x]);
        k += pad[x];
      }
      memcpy(line + k, ptr, end[x]);
      if(ptr[end[x]] != sep[x]){
        fprintf(stderr, "since: data corruption: expected a separator, not 0x%x in line at %d in %s\n", ptr[end[x]], i, sn->s_name);
        return -1;
      }
      k += end[x];
      line[k] = sep[x];
      k++;
      ptr += end[x] + 1;
    }
    result = write(sn->s_fd, line, k);
    if(result != sw){
      fprintf(stderr, "since: unable to write line to %s: %s\n", sn->s_name, (result < 0) ? strerror(errno) : "incomplete write");
      return -1;
    }
  }

  sn->s_disk_device += (pad[0] / 2);
  sn->s_disk_inode += (pad[1] / 2);
  sn->s_disk_size += (pad[2] / 2);

  if(lseek(sn->s_fd, 0, SEEK_SET) != 0){
    fprintf(stderr, "since: unable to rewind tmp file to start: %s\n", strerror(errno));
    return -1;
  }

  return 0;
}

static int maybe_upgrade_state_file(struct since_state *sn)
{
  if((sn->s_disk_device >= sn->s_arch_device) &&
     (sn->s_disk_inode >= sn->s_arch_inode) &&
     (sn->s_disk_size >= sn->s_arch_size)){
    if(sn->s_verbose > 4){
      fprintf(stderr, "since: state file data fields greater or equal to architecture, no rewrite needed\n");
    }
    return 0;
  }

  if(sn->s_readonly){
    fprintf(stderr, "since: need to rewrite state file %s, but invoked as readonly\n", sn->s_name);
    return -1;
  }

  if((sn->s_buffer == NULL) || (sn->s_size == 0)){
    fprintf(stderr, "since: major logic failure, attempting to upgrade empty file\n");
    return -1;
  }

  if(tmp_state_file(sn, &internal_upgrade_state_file)){
    return -1;
  }

  forget_state_file(sn);

  return load_state_file(sn);
}

/* fmt logic ************************************************/

static int make_fmt_field(struct since_state *sn, int disk, int arch, int suffix)
{
  int have, pad, i, result;

  have = MAX_FMT - sn->s_fmt_used;

  if(disk < arch){
    return -1;
  }

  if(disk > arch){
    pad = (disk - arch) * 2;
    if(pad > have){
      return -1;
    }
    for(i = 0; i < pad; i++){
      sn->s_fmt[sn->s_fmt_used++] = '0';
    }
    have -= pad;
  }

  for(i = 0; fmt_table[i].f_bytes > 0; i++){
    if(fmt_table[i].f_bytes == arch){
      result = snprintf(sn->s_fmt + sn->s_fmt_used, have, "%%0%d%s%c", arch * 2, fmt_table[i].f_string, suffix);
      if((result < 0) || (result >= have)){
        return -1;
      }

      sn->s_fmt_used += result;
      return 0;
    }
  }

  return -1;
}

static int make_fmt_string(struct since_state *sn)
{
  int result;

  sn->s_fmt_used = 0;
  sn->s_fmt_output = 0;
  sn->s_fmt_prefix = 0;

  result = 0;

  result += make_fmt_field(sn, sn->s_disk_device, sn->s_arch_device, ':');
  result += make_fmt_field(sn, sn->s_disk_inode, sn->s_arch_inode, ':');
  /* meh, we wanted to use \n, but snprintf runs over the edge */
  result += make_fmt_field(sn, sn->s_disk_size, sn->s_arch_size, '\0');

  if(result){
    fprintf(stderr, "since: internal logic failure generating format string\n");
    return -1;
  }

#ifdef DEBUG
  fprintf(stderr, "since: fmt string is %s", sn->s_fmt);
#endif

  sn->s_fmt_prefix = (2 * sn->s_disk_device) + 1 + (2 * sn->s_disk_inode);
  sn->s_fmt_output = sn->s_fmt_prefix + 1 + (2 * sn->s_disk_size); /* WARNING: missing \n, added manually */

  if((sn->s_fmt_output + 1) >= MAX_FMT){
    fprintf(stderr, "since: oversize fields: limit=%d, wanted=%d\n", MAX_FMT, sn->s_fmt_output + 1);
    return -1;
  }

  return 0;
}

/* datafile stuff *******************************************/

static char *ignore_suffix[] = { ".gz", ".bz2", ".Z", ".zip", NULL };

static int setup_data(struct since_state *sn, char *name)
{
  struct data_file *tmp;
  struct stat st;
  int fd, i;
  char *suffix;

  if(sn->s_nozip){
    suffix = strrchr(name, '.');
    if(suffix){
      for(i = 0; ignore_suffix[i]; i++){
        if(!strcmp(suffix, ignore_suffix[i])){
          if(sn->s_verbose > 4){
            fprintf(stderr, "since: not displaying presumed compressed file %s\n", name);
          }
          return 0;
        }
      }
    }
  }

  fd = open(name, O_RDONLY);
  if(fd < 0){
    fprintf(stderr, "since: unable to open %s: %s\n", name, strerror(errno));
    return 1;
  }

  if(fstat(fd, &st)){
    fprintf(stderr, "since: unable to fstat %s: %s\n", name, strerror(errno));
    return 1;
  }

  if(!(S_IFREG & st.st_mode)){
    fprintf(stderr, "since: unable to handle special file %s\n", name);
    return 1;
  }

  tmp = realloc(sn->s_data_files, sizeof(struct data_file) * (sn->s_data_count + 1));
  if(tmp == NULL){
    fprintf(stderr, "since: unable to allocate %u bytes for %s\n", sizeof(struct data_file) * (sn->s_data_count * 1), name);
    return -1;
  }

  sn->s_data_files = tmp;
  tmp = &(sn->s_data_files[sn->s_data_count]);
  sn->s_data_count++;

  tmp->d_name = name;
  tmp->d_fd = fd;

  tmp->d_dev = st.st_dev;
  tmp->d_ino = st.st_ino;

  tmp->d_had = 0;
  tmp->d_now = st.st_size;
  tmp->d_pos = 0;

  tmp->d_write = 0;
  tmp->d_jump = 0;
  tmp->d_deleted = 0;
  tmp->d_replaced = 0;
  tmp->d_moved = 0;
  tmp->d_notable = 1;

  tmp->d_offset = (-1);
  tmp->d_notify = (-1);

  return 0;
}

/* lookup stuff *********************************************/

static int lookup_entries(struct since_state *sn)
{
  char line[MAX_FMT], *end;
  int i, j, result;
  struct data_file *df;
805

806 807 808
  if((sn->s_buffer == NULL) || (sn->s_size == 0)){ /* file empty, nothing to look up */
    return 0;
  }
809

810 811 812 813 814 815 816
  /* could sort/index stuff to do better than O(n^2) */
  for(i = 0; i < sn->s_data_count; i++){
    df = &(sn->s_data_files[i]);
    result = snprintf(line, MAX_FMT, sn->s_fmt, df->d_dev, df->d_ino, 0);
    if(result != sn->s_fmt_output){
      fprintf(stderr, "since: logic problem: expected state line to be %d bytes, printed %d\n", sn->s_fmt_output, result);
      return -1;
817
    }
818 819 820 821 822 823 824 825 826 827 828 829
    for(j = 0; j < sn->s_size; j += (sn->s_fmt_output + 1)){
      if(!memcmp(sn->s_buffer + j, line, sn->s_fmt_prefix)){
        df->d_offset = j;
        if(sn->s_arch_size > sizeof(unsigned long)){
          df->d_had = strtoull(sn->s_buffer + j + sn->s_fmt_prefix + 1, &end, 16);
        } else {
          df->d_had = strtoul(sn->s_buffer + j + sn->s_fmt_prefix + 1, &end, 16);
        }
        if(end[0] != '\n'){
          fprintf(stderr, "since: parse problem: unable to convert value at offset %d to number\n", j + sn->s_fmt_prefix + 1);
          return -1;
        }
830

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
        if(df->d_had > df->d_now){
          fprintf(stderr, "since: considering %s to be truncated, displaying from start\n", df->d_name);
          df->d_had = 0;
          df->d_write = 1;
        }

        if(df->d_pos != df->d_had){
          /* pos is the value which gets saved */
          df->d_pos = df->d_had;
          df->d_jump = 1;
        }

        if(sn->s_verbose > 3){
          /* this seems a bit risky, what if longs are bigger than wordsize ? */
#if _FILE_OFFSET_BITS > __WORDSIZE
          fprintf(stderr, "since: found record for %s at offset %d, now=%Lu, had=%Lu\n", df->d_name, j, df->d_now, df->d_had);
#else
          fprintf(stderr, "since: found record for %s at offset %d, now=%ld, had=%ld\n", df->d_name, j, df->d_now, df->d_had);
#endif
        }
        break;
      }
    }
  }

  return 0;
857 858
}

859
/* refresh using stat and sleep *****************************/
860

861
static int setup_watch(struct since_state *sn)
862
{
863 864 865
#ifdef USE_INOTIFY
  struct data_file *df;
  unsigned int i;
866

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
  sn->s_notify = inotify_init();
  if(sn->s_notify < 0){
    if(sn->s_verbose > 3){
      fprintf(stderr, "since: unable to use inotify: %s\n", strerror(errno));
    }
    return 1;
  }

  for(i = 0; i < sn->s_data_count; i++){
    df = &(sn->s_data_files[i]);
    df->d_notify = inotify_add_watch(sn->s_notify, df->d_name, IN_MODIFY | IN_DELETE_SELF | IN_MOVE_SELF);
    if(df->d_notify < 0){
      fprintf(stderr, "since: unable to register notification for %s: %s\n", df->d_name, strerror(errno));
      close(sn->s_notify);
      sn->s_notify = (-1);
      return 1;
    }
  }
#endif
  return 0;
}
888

889 890 891 892
static int check_file(struct since_state *sn, struct data_file *df)
{
  struct stat st;
  int again;
893

894 895 896 897 898 899 900
  if(stat(df->d_name, &st) == 0){
    if((st.st_ino != df->d_ino) ||
       (st.st_dev != df->d_dev)){
      if(df->d_replaced == 0){
        df->d_replaced = 1;
        df->d_notable = 1;
      }
901
#ifdef DEBUG
902
      fprintf(stderr, "check: file %s no longer matches\n", df->d_name);
903
#endif
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
      /* TODO: could re-open filename here, redo lookup, etc */
      again = 1;
    } else {
      again = 0;
      /* TODO: could note a rename back to the old name */
      df->d_replaced = 0;
      df->d_moved = 0;
    }
  } else {
    switch(errno){
      case ENOENT :
        if(df->d_moved == 0){
          df->d_moved = 1;
          df->d_notable = 1;
        }
        break;
        /* TODO: could quit on some critical errors sooner */
    }
    again = 1;
  }

  if(again){
    if(fstat(df->d_fd, &st)){
      fprintf(stderr, "since: unable to stat %s: %s\n", df->d_name, strerror(errno));
      return -1;
    }
    if(st.st_nlink == 0){
      /* TODO: could delete entry to reduce inode collisions */
      if(df->d_deleted == 0){
        df->d_deleted = 1;
        df->d_notable = 1;
      }
    } else {
      if(df->d_moved == 0){
        df->d_moved = 1;
        df->d_notable = 1;
      }
    }
  }
943

944
#ifdef DEBUG
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
  fprintf(stderr, "update: new size=%Lu, old max=%Lu\n", st.st_size, df->d_now);
#endif

  if(st.st_size < df->d_now){
    fprintf(stderr, "since: considering %s to be truncated, displaying from start\n", df->d_name);
    df->d_had = 0;
    df->d_pos = 0;
    df->d_jump = 1;
    df->d_write = 1;
    df->d_notable = 1;
  }

  if(df->d_now < st.st_size){
    df->d_notable = 1;
  }
  df->d_now = st.st_size;

  return 0;
}

static int notify_watch(struct since_state *sn)
{
#ifdef USE_INOTIFY
  struct inotify_event update;
  struct data_file *df;
  char discard[PATH_MAX];
  unsigned int i;
  int result;

974
#ifdef DEBUG
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
  if(sn->s_notify < 0){
    fprintf(stderr, "since: major logic error, no notify file descriptor\n");
    abort();
  }
#endif

  sigprocmask(SIG_UNBLOCK, &(sn->s_set), NULL);
  result = read(sn->s_notify, &update, sizeof(struct inotify_event));
  sigprocmask(SIG_BLOCK, &(sn->s_set), NULL);

  if(result < 0){
    if(since_run == 0){
      return 1;
    }
    if(sn->s_verbose > 1){
      fprintf(stderr, "since: inotify failed: %s\n", strerror(errno));
    }
    return 0;
  }

  if(result != sizeof(struct inotify_event)){
    fprintf(stderr, "since: inotify weirdness: read partial notification\n");
    return -1;
  }

  if(sn->s_verbose > 4){
    fprintf(stderr, "since: inotify mask 0x%x, len %u\n", update.mask, update.len);
  }

  if(update.len > 0){
    result = read(sn->s_notify, discard, update.len);
    if(sn->s_verbose > 3){
      fprintf(stderr, "since: discarding %u bytes of unexpected path information from inotify\n", update.len);
    }
    if(result != update.len){
      fprintf(stderr, "since: inotify weirdness: unable to discard path information\n");
      return -1;
    }
  }

  for(i = 0; i < sn->s_data_count; i++){
    df = &(sn->s_data_files[i]);
    if(update.wd == df->d_notify){

      if(check_file(sn, df) < 0){
        return -1;
      }
    }
  }

  return 0;
#else
1027
#ifdef DEBUG
1028 1029 1030 1031
  fprintf(stderr, "since: major logic error, doing disabled notification\n");
  abort();
#endif
  return -1;
1032
#endif
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
}

static int poll_watch(struct since_state *sn)
{
  unsigned int i;
  struct data_file *df;

  sigprocmask(SIG_UNBLOCK, &(sn->s_set), NULL);
  nanosleep(&(sn->s_delay), NULL);
  sigprocmask(SIG_BLOCK, &(sn->s_set), NULL);

  if(since_run == 0){
    return 1;
  }

  for(i = 0; i < sn->s_data_count; i++){
    df = &(sn->s_data_files[i]);
    if(check_file(sn, df) < 0){
      return -1;
    }
  }

  return 0;
}

int run_watch(struct since_state *sn)
{
  if(sn->s_notify < 0){
    return poll_watch(sn);
  } else {
    return notify_watch(sn);
  }
}

/* display functions ****************************************/

static int display_header(struct since_state *sn, struct data_file *df, int single, int chuck)
{
  off_t delta;
  unsigned int value, z;
  int nada;
  char *suffixes[] = { "b", "kb", "Mb", "Gb", "Tb", NULL} ;

  switch(sn->s_verbose){
    case 0 : return 0;
    case 1 : if(single) return 0; /* WARNING: else fall */
    case 2 : if(df->d_notable == 0) return 0;
  }

  df->d_notable = 0;

  /* possibly do unlocked io here */
  fprintf(sn->s_header, "==> %s ", df->d_name);

  nada = 1;

  if(df->d_deleted){
    fprintf(sn->s_header, "[deleted] ");
    nada = 0;
  } else if(df->d_moved){
    fprintf(sn->s_header, "[moved] ");
    nada = 0;
  }

#if 0
  if(df->d_replaced){
    /* we know about this, but won't say anything (yet) */
    fprintf(sn->s_header, "[replaced]");
  }
#endif

  if(df->d_pos != df->d_now){
    if(chuck){
      fprintf(sn->s_header, "[discarded] ");
    }
    if(sn->s_verbose > 2){
      delta = df->d_now - df->d_pos;
      for(z = 0; (suffixes[z + 1]) && (delta > 9999); z++){
        delta /= 1024;
      }
      value = delta;
      fprintf(sn->s_header, "(+%u%s) ", value, suffixes[z]);
    }
    nada = 0;
  }

  if(nada){
    fprintf(sn->s_header, "[nothing new] ");
  }

  fprintf(sn->s_header, "<==\n");
  fflush(sn->s_header);

  return 0;
}

static int display_buffer(struct since_state *sn, struct data_file *df, char *buffer, unsigned int len)
{
  int wr, result;
  unsigned int wt, i, back;
1133 1134

#ifdef DEBUG
1135 1136 1137 1138 1139 1140
  sleep(1);

  fprintf(stderr, "display: need to display %u bytes for %s\n", len, df->d_name);
  if(len <= 0){
    fprintf(stderr, "since: logic failure: writing out empty string\n");
  }
1141
#endif
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

  wt = 0;
  result = 1; /* used to infer signal */
  since_run = 1;
  sigprocmask(SIG_UNBLOCK, &(sn->s_set), NULL);

  while(since_run){
    wr = write(STDOUT_FILENO, buffer + wt, len - wt);
    switch(wr){
      case -1 :
1152
#ifdef DEBUG
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
        fprintf(stderr, "display: write failed: %s\n", strerror(errno));
#endif
        switch(errno){
          case EINTR :
          case EPIPE :
            since_run = 0;
            result = 1;
          case EAGAIN : /* iffy */
            break;
          default :
            /* unlikely to do anything */
            fprintf(stderr, "since: unable to display output: %s\n", strerror(errno));
            since_run = 0;
            result = (-1);
        }
        break;
      case 0 :
        /* WARNING: unsure how to deal with this one, should we exit */
        break;
      default :
        wt += wr;
        if(wt >= len){
          since_run = 0;
          result = 0;
        }
        break;
    }
  }

  sigprocmask(SIG_BLOCK, &(sn->s_set), NULL);
  since_run = 1;

1185
#ifdef DEBUG
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
  fprintf(stderr, "display: final code is %d, wt %u\n", result, wt);
#endif

  if(result < 0){ /* conventional error */
    return (-1);
  }

  if(result == 0){ /* success */
    df->d_pos += len;
    df->d_write = 1;
    return 0;
  }

  /* user interrupt */

  if(wt <= 0){
    return 1;
  }

  /* no guarantees, just trying to restart on a new line */
  back = (wt <= LINE_SEARCH) ? 0 : (wt - LINE_SEARCH);
  i = wt;

  do{
    i--;
    if(buffer[i] == '\n'){
1212
#ifdef DEBUG
1213 1214 1215 1216
      fprintf(stderr, "recover: to newline at %u\n", i);
#endif
      wt = i + 1;
      break;
1217
    }
1218
  } while(i > back);
1219

1220 1221 1222 1223 1224 1225 1226 1227
#ifdef DEBUG
  fprintf(stderr, "interrupt: at position %u\n", wt);
#endif

  df->d_pos += wt;
  df->d_write = 1;

  return 1;
1228 1229
}

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
static int display_file(struct since_state *sn, struct data_file *df, int single)
{
  char *ptr;
  char buffer[IO_BUFFER];
  int rr, result;
  off_t range, i;
  unsigned int fixup;

  /* WARNING: should not manipulate d_had here, should be done in lookup and refresh, maybe pos resets too */
  if(df->d_had > df->d_now){
    fprintf(stderr, "since: considering %s to be truncated, displaying from start\n", df->d_name);
    df->d_had = 0;
    /* WARNING: d_pos gets saved, not d_had */
    df->d_write = 1;
  }

  if(df->d_pos < df->d_had){
    df->d_jump = 1;
    df->d_pos = df->d_had;
  }

#ifdef DEBUG
  if(df->d_pos > df->d_now){
    fprintf(stderr, "since: logic failure - position pointer has overtaken length\n");
    abort();
  }
#endif

  range = df->d_now - df->d_pos;
  if(range == 0){
    display_header(sn, df, single, 0);
    return 0;
  }

  if((range > IO_BUFFER) && sn->s_domap){
    fixup = df->d_pos & (IO_BUFFER - 1);
    ptr = mmap(NULL, range + range, PROT_READ, MAP_PRIVATE, df->d_fd, df->d_pos - fixup);
    if((void *)(ptr) != MAP_FAILED){
      display_header(sn, df, single, 0);

      result = display_buffer(sn, df, ptr + fixup, range);
      df->d_jump = 1;
      munmap(ptr, range + fixup);

      return result;
#ifdef DEBUG
    } else {
      fprintf(stderr, "display: mmap of %d failed: %s\n", df->d_fd, strerror(errno));
#endif
    }
  }

  if(df->d_jump){
    if(lseek(df->d_fd, df->d_pos, SEEK_SET) != df->d_pos){
      fprintf(stderr, "since: unable to seek in file %s: %s\n", df->d_name, strerror(errno));
      return -1;
    }
    df->d_jump = 0;
  }
1289

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
  display_header(sn, df, single, 0);
  i = 0;
  df->d_write = 1;
  while(df->d_pos < df->d_now){
    rr = read(df->d_fd, buffer, IO_BUFFER);
    switch(rr){
      case -1 :
        switch(errno){
          case EAGAIN :
          case EINTR  :
            break;
          default :
            fprintf(stderr, "since: unable to read from %s: %s\n", df->d_name, strerror(errno));
            return -1;
        }
        break;
      case  0 :
        fprintf(stderr, "since: unexpected eof while reading from %s\n", df->d_name);
        break;
      default :
        result = display_buffer(sn, df, buffer, rr);
        if(result != 0){
          return result;
        }
        break;
    }
  }

  if(df->d_now < df->d_pos){
    /* in case more gets append to file while reading */
    df->d_now = df->d_pos;
  }

  return 0;
}

static int display_files(struct since_state *sn)
1327
{
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
  unsigned int i;
  int result, single;

#ifdef DEBUG
  fprintf(stderr, "display: have %u files to display\n", sn->s_data_count);
#endif

  single = (sn->s_data_count == 1)  ? 1 : 0;

  for(i = 0; i < sn->s_data_count; i++){
    result = display_file(sn, &(sn->s_data_files[i]), single);
    if(result){
      return result;
1341
    }
1342
  }
1343 1344 1345 1346

  return 0;
}

1347 1348 1349
/* discard data **********************************************/

static void discard_files(struct since_state *sn)
1350
{
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
  unsigned int i;
  struct data_file *df;
  int single;

#ifdef DEBUG
  fprintf(stderr, "display: have %u files to display\n", sn->s_data_count);
#endif

  single = (sn->s_data_count == 1)  ? 1 : 0;

  for(i = 0; i < sn->s_data_count; i++){
    df = &(sn->s_data_files[i]);

    display_header(sn, df, single, 1);

    if(df->d_pos != df->d_now){
      df->d_pos = df->d_now;
      df->d_jump = 1;
      df->d_write = 1;
    }
  }

1373 1374
}

1375 1376 1377
/* routines to write out new values to state file ************/

static int append_state_file(struct since_state *sn)
1378
{
1379
  int sofar, result;
1380

1381 1382 1383
  if((sn->s_append == NULL) || (sn->s_add == 0)){
    return 0;
  }
1384

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
  if(lseek(sn->s_fd, sn->s_size, SEEK_SET) != sn->s_size){
    fprintf(stderr, "since: unable to seek to end of file %s: %s\n", sn->s_name, strerror(errno));
    return -1;
  }

  sofar = 0;
  while(sofar < sn->s_add){
    result = write(sn->s_fd, sn->s_append + sofar, sn->s_add - sofar);
    if(result < 0){
      switch(errno){
        case EAGAIN :
        case EINTR  :
          break;
        default :
          fprintf(stderr, "since: unable to write %d bytes to %s: %s\n", sn->s_add - sofar, sn->s_name, strerror(errno));
          return -1;
      }
    } else {
      sofar += result;
      if(result == 0){
        fprintf(stderr, "since: warning: wrote 0 bytes to file to %s\n", sn->s_name);
      }
1407
    }
1408 1409 1410 1411
  }

  return 0;
}
1412

1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
static int internal_update_state_file(struct since_state *sn)
{
  int sofar, result;

#if 0 /* not needed, always run outof tmp where a new file is opened */
  if(lseek(sn->s_fd, 0, SEEK_SET) != 0){
    fprintf(stderr, "since: unable to seek to start of file %s: %s\n", sn->s_name, strerror(errno));
    return -1;
  }
#endif

  sofar = 0;
  while(sofar < sn->s_size){
    result = write(sn->s_fd, sn->s_buffer + sofar, sn->s_size - sofar);
    if(result < 0){
      switch(errno){
        case EAGAIN :
        case EINTR  :
          break;
        default :
          fprintf(stderr, "since: unable to rewrite %d bytes to %s: %s\n", sn->s_size - sofar, sn->s_name, strerror(errno));
          return -1;
      }
    } else {
      sofar += result;
      if(result == 0){
        fprintf(stderr, "since: warning: wrote 0 bytes to file to %s\n", sn->s_name);
      }
1441
    }
1442 1443 1444 1445
  }

  return append_state_file(sn);
}
1446

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
static int update_state_file(struct since_state *sn)
{
  /* WARNING: this invalidates the structure, forces a forget */
  char *tmp, *target;
  int i, j, result, redo, changed;
  struct data_file *df;

  if(sn->s_readonly){
    if(sn->s_verbose > 2){
      fprintf(stderr, "since: readonly, not updatating %s\n", sn->s_name);
1457
    }
1458 1459
    return 1;
  }
1460

1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
  changed = 0;
  redo = sn->s_atomic;

  for(i = 0; i < sn->s_data_count; i++){
    df = &(sn->s_data_files[i]);
    if(df->d_write){
      changed = 1;
      if(df->d_offset < 0){
        /* awkwardly block duplicate entries */
        for(j = sn->s_data_count - 1; j > i; j--){
          if((sn->s_data_files[j].d_ino == df->d_ino) &&
             (sn->s_data_files[j].d_dev == df->d_dev) &&
             (sn->s_data_files[j].d_offset < 0) &&
             sn->s_data_files[j].d_write){
            break;
          }
        }
        if(i != j){
          continue; /* will append later, don't do it for this entry */
        }
        tmp = realloc(sn->s_append, sn->s_add + sn->s_fmt_output + 1);
        if(tmp == NULL){
          fprintf(stderr, "since: unable to allocate %d bytes for append buffer \n", sn->s_add + sn->s_fmt_output + 1);
          return -1;
        }
        sn->s_append = tmp;
        target = sn->s_append + sn->s_add;
        sn->s_add += sn->s_fmt_output + 1;
      } else {
        target = sn->s_buffer + df->d_offset;
        redo = sn->s_ismap ? sn->s_atomic : 1; /* rewrite file completely if not mmaped and we modify existing entries */
      }

      result = snprintf(target, sn->s_fmt_output + 1, sn->s_fmt, df->d_dev, df->d_ino, df->d_pos);
      target[sn->s_fmt_output] = '\n'; /* replace \0 with newline */
      if(result != sn->s_fmt_output){
        fprintf(stderr, "since: logic failure: wanted to print %d, got %d\n", sn->s_fmt_output, result);
        return -1;
      }
    }
  }

  if(changed){
    if(redo){
      result = tmp_state_file(sn, &internal_update_state_file);
    } else {
      result = append_state_file(sn);
    }
  } else {
    result = 0;
  }

  forget_state_file(sn);

  return result;
}

/* main related stuff ***************************************/

static void copying()
{
  printf("since: Copyright (c) 1998-2008 Marc Welz\n");
  printf("       May only be distributed in accordance with the terms of the GNU General\n");
  printf("       Public License v3 or newer as published by the Free Software Foundation\n");
}

static void usage(char *app)
{
  int i;

  printf("since displays the data appended to files since the last time since was run\n\n");

  printf("Usage: %s [option ...] file ...\n", app);

  printf("\nOptions\n");
  printf(" -a        update state file atomically\n");
  printf(" -d int    set the interval when following files\n");
  printf(" -e        print header lines to standard error\n");
  printf(" -f        follow files, periodically check if more data has been appended\n");
  printf(" -h        this help\n");
  printf(" -l        lax mode, do not fail if some files are inaccessible\n");
  printf(" -m        do not use mmap() to access files, use read()\n");
  printf(" -n        do not update since state file\n");
  printf(" -q        reduce verbosity to nothing\n");
  printf(" -s file   specify the state file, overriding SINCE variable and home directory\n");
  printf(" -v        increase verbosity, can be given multiple times\n");
  printf(" -x        ignore files with compressed extensions:");
  for(i = 0; ignore_suffix[i]; i++){
    printf(" %s", ignore_suffix[i]);
  }
  printf("\n");
  printf(" -z        discard initial output\n");
#ifdef VERSION
  printf(" -V        print version information\n");
#endif

  printf("\nExample\n");
  printf(" $ since -lz /var/log/*\n");
  printf(" $ logger foobar\n");
  printf(" $ since -lx /var/log/*\n");
}

int main(int argc, char *argv[])
{
  int i, j, result, dashes;
  struct since_state state, *sn;
  struct sigaction sag;
  char *state_file;

  sn = &state;
  state_file = NULL;

  init_state(sn);

  i = j = 1;
  dashes = 0;

  while (i < argc) {
    if((argv[i][0] == '-') && (dashes == 0)){
      switch (argv[i][j]) {
        case 'c' :
          copying();
          return EX_OK;
        case 'h' :
          usage(argv[0]);
          return EX_OK;
          break;
        case 's' :
          j++;
          if (argv[i][j] == '\0') {
            j = 0;
            i++;
          }
          if (i >= argc) {
            fprintf(stderr, "since: -s needs a filename as parameter\n");
            return EX_USAGE;
          }
          state_file = argv[i] + j;
          i++;
          j = 1;
          break;
        case 'd':
          j++;
          if (argv[i][j] == '\0') {
            j = 0;
            i++;
          }
          if (i >= argc) {
            fprintf(stderr, "since: -d needs an integer parameter\n");
            return EX_USAGE;
          }
          sn->s_delay.tv_sec = atoi(argv[i] + j);
          sn->s_delay.tv_nsec = 0;
          if(sn->s_delay.tv_sec < 0){
            sn->s_delay.tv_sec = 1;
          }
          i++;
          j = 1;
          break;
        case 'a' :
          j++;
          sn->s_atomic = 1;
          break;
        case 'e' :
          j++;
          sn->s_header = stderr;
          break;
        case 'f' :
          j++;
          sn->s_follow = 1;
          break;
        case 'l' :
          j++;
          sn->s_relaxed = 1;
          break;
        case 'm' :
          j++;
          sn->s_domap = 1 - sn->s_domap;
          break;
        case 'n' :
          j++;
          sn->s_readonly = 1;
          break;
        case 'q' :
          j++;
          sn->s_verbose = 0;
          break;
        case 'v' :
          j++;
          sn->s_verbose++;
          break;
        case 'x' :
          j++;
          sn->s_nozip++;
          break;
        case 'z' :
          j++;
          sn->s_discard++;
          break;
#ifdef VERSION
        case 'V' :
          j++;
          printf("since %s\n", VERSION);
          return EX_OK;
#endif
        case '-' :
          if((j == 1) && argv[i][j + 1] == '\0'){
            dashes = 1;
          }
          j++;
          break;
        case '\0':
          j = 1;
          i++;
          break;
        default:
          fprintf(stderr, "since: unknown option -%c (use -h for help)\n", argv[i][j]);
          return EX_USAGE;
      }
    } else {
      result = setup_data(sn, argv[i]);
      if((result < 0) || ((sn->s_relaxed == 0) && result > 0)){
        return EX_OSERR;
      }
1685
      i++;
1686
    }
1687
  }
1688

1689 1690 1691 1692
  if(sn->s_data_count <= 0){
    fprintf(stderr, "since: need at least one filename (use -h for help)\n");
    return EX_USAGE;
  }
1693

1694 1695 1696 1697
  /* try to open a list of files */
  if(open_state_file(sn, state_file) < 0){
    return EX_OSERR;
  }
1698

1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
  /* attempt to load content of said file */
  if(load_state_file(sn) < 0){
    return EX_OSERR;
  }

  /* look at content, gather size of on disk fields */
  if(check_state_file(sn) < 0){
    return EX_DATAERR;
  }

  if(maybe_upgrade_state_file(sn) < 0){
    return EX_OSERR;
  }

  if(make_fmt_string(sn) < 0){
    return EX_SOFTWARE;
  }

  if(lookup_entries(sn) < 0){
    return EX_OSERR;
  }

#ifdef DEBUG
  for(i = 0; i < sn->s_data_count; i++){
    fprintf(stderr, "dump[%d]: name=%s, fd=%d\n", i, sn->s_data_files[i].d_name, sn->s_data_files[i].d_fd);
  }
#endif

  sigemptyset(&(sn->s_set));
  sigaddset(&(sn->s_set), SIGINT);
  sigaddset(&(sn->s_set), SIGPIPE);

  sigprocmask(SIG_BLOCK, &(sn->s_set), NULL);

  sag.sa_handler = &handle_signal;
  sigfillset(&(sag.sa_mask));
  sag.sa_flags = 0;
  sigaction(SIGPIPE, &sag, NULL);
  sigaction(SIGINT, &sag, NULL);

  if(sn->s_discard){
    discard_files(sn);
  } else {
    if(display_files(sn) < 0){
      return EX_OSERR;
    }
  }
  if(sn->s_follow){
    if(setup_watch(sn) < 0){
      return EX_OSERR;
    }
    do{
      result = run_watch(sn);
      if(result == 0){
        result = display_files(sn);
      }
    } while(result == 0);
    if(result < 0){
      return EX_OSERR;
    }
  }

  if(update_state_file(sn) < 0){
    return EX_OSERR;
  }

  destroy_state(sn);

  return EX_OK;
1768
}