fnotifystat-0.02.05/0000755000175000017500000000000013625734055013212 5ustar ckingckingfnotifystat-0.02.05/fnotifystat.c0000644000175000017500000010405313625734052015730 0ustar ckingcking/* * Copyright (C) 2014-2020 Canonical, Ltd. * * 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 2 * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: Colin Ian King */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define TABLE_SIZE (17627) /* Best if prime */ #define BUFFER_SIZE (4096) #define DEV_HASH_SIZE (1021) #define OPT_VERBOSE (0x00000001) /* Verbose mode */ #define OPT_DIRNAME_STRIP (0x00000002) /* Remove leading path */ #define OPT_PID (0x00000004) /* Select by PID */ #define OPT_SORT_BY_PID (0x00000008) /* Sort by PID */ #define OPT_CUMULATIVE (0x00000010) /* Gather cumulative stats */ #define OPT_TIMESTAMP (0x00000020) /* Show timestamp */ #define OPT_SCALE (0x00000040) /* scale data */ #define OPT_NOSTATS (0x00000080) /* No stats mode */ #define OPT_MERGE (0x00000100) /* Merge events */ #define OPT_DEVICE (0x00000200) /* Stats by mount */ #define OPT_INODE (0x00000400) /* Filenames by inode, dev */ #define OPT_FORCE (0x00000800) /* Force output */ #define PROC_CACHE_LIFE (120) /* Refresh cached pid info timeout */ #define CMD_UNKNOWN "" /* fnotify file activity stats */ typedef struct file_stat { uint64_t open; /* Count of opens */ uint64_t close; /* Count of closes */ uint64_t read; /* Count of reads */ uint64_t write; /* Count of writes */ uint64_t total; /* Total count */ char *path; /* Pathname of file */ struct file_stat *next; /* Next item in hash list */ pid_t pid; /* PID of process touching file */ } file_stat_t; /* process info */ typedef struct proc_info { char *cmdline; /* cmdline of process */ struct proc_info *next; /* Next item in hash list */ double whence; /* When data acquired */ pid_t pid; /* pid of process */ } proc_info_t; /* pathname list info */ typedef struct pathname_t { char *pathname; /* Pathname */ size_t pathlen; /* Length of path */ struct pathname_t *next; /* Next in list */ } pathname_t; /* scaling factor */ typedef struct { const char ch; /* Scaling suffix */ const uint64_t scale; /* Amount to scale by */ } scale_t; /* stashed context for verbose info */ typedef struct { char *filename; /* Filename */ uint64_t mask; /* Event mask */ uint64_t count; /* Merged event count */ file_stat_t *fs; /* File Info */ struct tm tm; /* Time of previous event */ } stash_info_t; /* cached dev names */ typedef struct dev_info { char *name; /* /dev device name */ dev_t dev; /* dev number */ struct dev_info *next; /* next device */ } dev_info_t; static const char *app_name = "fnotifystat"; /* name of this tool */ static file_stat_t *file_stats[TABLE_SIZE]; /* hash table of file stats */ static proc_info_t *proc_infos[TABLE_SIZE]; /* hash table of proc infos */ static proc_info_t *proc_list; /* processes to monitor */ static size_t file_stats_size; /* number of items in hash table */ static unsigned int opt_flags = OPT_SCALE; /* option flags */ static volatile bool stop_fnotifystat = false; /* true -> stop fnotifystat */ static pid_t my_pid; /* pid of this programme */ static bool named_procs = false; static pathname_t *paths_include; /* paths to include */ static pathname_t *paths_exclude; /* paths to exclude */ static dev_info_t *dev_cache[DEV_HASH_SIZE]; /* device to device name hash */ /* * Attempt to catch a range of signals so * we can clean */ static const int signals[] = { /* POSIX.1-1990 */ #ifdef SIGHUP SIGHUP, #endif #ifdef SIGINT SIGINT, #endif #ifdef SIGQUIT SIGQUIT, #endif #ifdef SIGFPE SIGFPE, #endif #ifdef SIGTERM SIGTERM, #endif #ifdef SIGUSR1 SIGUSR1, #endif #ifdef SIGUSR2 SIGUSR2, /* POSIX.1-2001 */ #endif #ifdef SIGXCPU SIGXCPU, #endif #ifdef SIGXFSZ SIGXFSZ, #endif /* Linux various */ #ifdef SIGIOT SIGIOT, #endif #ifdef SIGSTKFLT SIGSTKFLT, #endif #ifdef SIGPWR SIGPWR, #endif #ifdef SIGINFO SIGINFO, #endif #ifdef SIGVTALRM SIGVTALRM, #endif -1, }; /* Time scaling factors */ static const scale_t time_scales[] = { { 's', 1 }, { 'm', 60 }, { 'h', 3600 }, { 'd', 24 * 3600 }, { 'w', 7 * 24 * 3600 }, { 'y', 365 * 24 * 3600 }, }; /* * dev_hash() * hash a device number */ static inline int dev_hash(const dev_t dev) { const int hash = (major(dev) << 16) | minor(dev); return hash % DEV_HASH_SIZE; } /* * dev_find() * find a device by dev number in the device cache */ static char *dev_find(const dev_t dev) { const int hash = dev_hash(dev); dev_info_t *d = dev_cache[hash]; while (d) { if (d->dev == dev) return d->name; d = d->next; } return NULL; } /* * dev_add() * add a device to the device cache */ static void dev_add(const char *name, const dev_t dev) { const int hash = dev_hash(dev); dev_info_t *d = dev_cache[hash]; /* Don't and if we already have it */ while (d) { if (d->dev == dev) return; d = d->next; } d = malloc(sizeof(*d)); if (!d) return; d->name = strdup(name); if (!d->name) { free(d); return; } d->dev = dev; d->next = dev_cache[hash]; dev_cache[hash] = d; } /* * dev_cache_mounts() * add dev number of mount points too */ static void dev_cache_mounts(void) { FILE *fp; char buffer[1024]; fp = fopen("/proc/mounts", "r"); if (!fp) return; while (fgets(buffer, sizeof(buffer), fp)) { struct stat statbuf; char *ptr = buffer; char *path; while (*ptr && *ptr != ' ' && *ptr != '\n') ptr++; if (!*ptr) continue; ptr++; path = ptr; while (*ptr && *ptr != ' ' && *ptr != '\n') ptr++; *ptr = '\0'; if (stat(path, &statbuf) < 0) continue; dev_add(path, statbuf.st_dev); } (void)fclose(fp); } /* * dev_cache_devs() * cache the device name of devices based on device number */ static void dev_cache_devs(const char *dev_path, const int depth, const int max_depth) { DIR *dir; struct dirent *dirp; dir = opendir(dev_path); if (!dir) return; while ((dirp = readdir(dir)) != NULL) { struct stat statbuf; char path[PATH_MAX]; if (dirp->d_name[0] == '.') continue; snprintf(path, sizeof(path), "%s/%s", dev_path, dirp->d_name); if (stat(path, &statbuf) < 0) continue; /* Symlink, ignore */ if ((statbuf.st_mode & S_IFMT) == S_IFLNK) continue; if ((depth < max_depth) && ((statbuf.st_mode & S_IFMT) == S_IFDIR)) { dev_cache_devs(path, depth + 1, max_depth); continue; } /* Not a block dev, ignore */ if ((statbuf.st_mode & S_IFMT) != S_IFBLK) continue; dev_add(path, statbuf.st_rdev); } (void)closedir(dir); } /* * dev_cache_free() * free the device cache */ static void dev_cache_free(void) { size_t i; for (i = 0; i < DEV_HASH_SIZE; i++) { dev_info_t *d = dev_cache[i]; while (d) { dev_info_t *next = d->next; free(d->name); free(d); d = next; } } } /* * dev_name() * find the name relating to a device number */ static char *dev_name(const dev_t dev) { static char buffer[256]; char devstr[32]; char *devname = dev_find(dev); snprintf(devstr, sizeof(devstr), "%u:%u", major(dev), minor(dev)); snprintf(buffer, sizeof(buffer), "%-10.10s%s%s", devstr, devname ? " " : "", devname ? devname : ""); return buffer; } /* * pid_max_digits() * determine (or guess) maximum digits of pids */ static int pid_max_digits(void) { static int max_digits; ssize_t n; int fd; const int default_digits = 6; const int min_digits = 5; char buf[32]; if (max_digits) goto ret; max_digits = default_digits; fd = open("/proc/sys/kernel/pid_max", O_RDONLY); if (fd < 0) goto ret; n = read(fd, buf, sizeof(buf) - 1); (void)close(fd); if (n < 0) goto ret; buf[n] = '\0'; max_digits = 0; while (buf[max_digits] >= '0' && buf[max_digits] <= '9') max_digits++; if (max_digits < min_digits) max_digits = min_digits; ret: return max_digits; } /* * handle_siginfo() * catch SIGUSR1, toggle verbose mode */ static void handle_sigusr1(int dummy) { (void)dummy; opt_flags ^= OPT_VERBOSE; } /* * get_double() * get a double value */ static double get_double(const char *const str, size_t *const len) { double val; *len = strlen(str); errno = 0; val = strtod(str, NULL); if (errno) { (void)fprintf(stderr, "Invalid value %s.\n", str); exit(EXIT_FAILURE); } if (*len == 0) { (void)fprintf(stderr, "Value %s is an invalid size.\n", str); exit(EXIT_FAILURE); } return val; } /* * get_double_scale() * get a value and scale it by the given scale factor */ static double get_double_scale( const char *const str, const scale_t scales[], const char *const msg) { double val; size_t len = strlen(str); int i; char ch; val = get_double(str, &len); len--; ch = str[len]; if (val < 0.0) { (void)printf("Value %s cannot be negative\n", str); exit(EXIT_FAILURE); } if (isdigit(ch) || ch == '.') return val; ch = tolower(ch); for (i = 0; scales[i].ch; i++) { if (ch == scales[i].ch) return val * scales[i].scale; } (void)printf("Illegal %s specifier %c\n", msg, str[len]); exit(EXIT_FAILURE); } /* * get_seconds() * get time in seconds, with scaling suffix support. */ static inline double get_seconds(const char *const str) { return get_double_scale(str, time_scales, "time"); } /* * count_to_str() * double precision count values to strings */ static char *count_to_str( const double val, char *const buf, const size_t buflen) { double v = val; static const char scales[] = " KMB"; if (opt_flags & OPT_SCALE) { size_t i; for (i = 0; i < sizeof(scales) - 1; i++, v /= 1000) { if (v <= 500) break; } (void)snprintf(buf, buflen, "%5.1f%c", v, scales[i]); } else { (void)snprintf(buf, buflen, "%6.1f", v); } return buf; } /* * timeval_to_double() * convert timeval to seconds as a double */ static double timeval_to_double(void) { struct timeval tv; redo: errno = 0; /* clear to be safe */ if (gettimeofday(&tv, NULL) < 0) { if (errno == EINTR) /* should not occur */ goto redo; /* Silently fail */ return -1.0; } return (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0); } /* * get_tm() * fetch tm, will set fields to zero if can't get */ static void get_tm(struct tm *tm) { time_t now = time(NULL); if (now == ((time_t) -1)) { (void)memset(tm, 0, sizeof(struct tm)); } else { (void)localtime_r(&now, tm); } } /* * pr_error() * print error message and exit fatally */ static void __attribute__ ((noreturn)) pr_error(const char *msg) { (void)fprintf(stderr, "Fatal error: %s: errno=%d (%s)\n", msg, errno, strerror(errno)); exit(EXIT_FAILURE); } /* * pr_nomem() * print out of memory error and exit fatally */ static void __attribute__ ((noreturn)) pr_nomem(const char *msg) { (void)fprintf(stderr, "Fatal error: out of memory: %s\n", msg); exit(EXIT_FAILURE); } /* * get_pid_cmdline * get process's /proc/pid/cmdline */ static char *get_pid_cmdline(const pid_t pid) { char buffer[BUFFER_SIZE]; int fd; ssize_t ret = 0; *buffer = '\0'; (void)snprintf(buffer, sizeof(buffer), "/proc/%d/cmdline", pid); if ((fd = open(buffer, O_RDONLY)) > -1) { ret = read(fd, buffer, sizeof(buffer) - 1); (void)close(fd); if (ret > -1) buffer[ret] = '\0'; } /* * No cmdline, could be a kernel thread, so get the comm * field instead */ if (!*buffer) { (void)snprintf(buffer, sizeof(buffer), "/proc/%d/comm", pid); if ((fd = open(buffer, O_RDONLY)) > -1) { ret = read(fd, buffer, sizeof(buffer) - 1); (void)close(fd); if (ret > 0) buffer[ret - 1] = '\0'; /* remove trailing \n */ } } if (ret < 1) { (void)strncpy(buffer, "", sizeof(buffer)); } else { char *ptr; for (ptr = buffer; *ptr && (ptr < buffer + ret); ptr++) { if (*ptr == ' ') { *ptr = '\0'; break; } } } return strdup(basename(buffer)); } /* * handle_sig() * catch signals and flag a stop */ static void handle_sig(int dummy) { (void)dummy; /* Stop unused parameter warning with -Wextra */ stop_fnotifystat = true; } /* * timeval_double() * timeval to a double */ static inline double timeval_double(const struct timeval *tv) { return (double)tv->tv_sec + ((double)tv->tv_usec / 1000000.0); } /* * hash_djb2a() * Hash a string, from Dan Bernstein comp.lang.c (xor version) */ static uint32_t hash_djb2a(const char *str, const pid_t pid) { register uint32_t hash = 5381 + pid; register int c; while ((c = *str++)) { /* (hash * 33) ^ c */ hash = ((hash << 5) + hash) ^ c; } return hash % TABLE_SIZE; } /* * proc_info_get() * get info about a specific process */ static proc_info_t *proc_info_get(const pid_t pid) { const unsigned long h = pid % TABLE_SIZE; proc_info_t *pi = proc_infos[h]; while (pi) { if (pi->pid == pid) { double now = timeval_to_double(); char *cmdline; /* Name lookup failed last time, try again */ if (!strcmp(pi->cmdline, CMD_UNKNOWN)) { free(pi->cmdline); goto update; } if (now < pi->whence + PROC_CACHE_LIFE) return pi; if ((cmdline = get_pid_cmdline(pid)) == NULL) pr_nomem("allocating process command line"); if (!strcmp(pi->cmdline, CMD_UNKNOWN)) { /* It's probably died, so no new process name */ free(cmdline); return pi; } /* Deemed "stale", so refresh stats */ free(pi->cmdline); pi->cmdline = cmdline; pi->whence = timeval_to_double(); return pi; } pi = pi->next; } if ((pi = calloc(1, sizeof(*pi))) == NULL) pr_nomem("allocating process information"); pi->next = proc_infos[h]; proc_infos[h] = pi; pi->pid = pid; update: if ((pi->cmdline = get_pid_cmdline(pid)) == NULL) pr_nomem("allocating process command line"); pi->whence = timeval_to_double(); return pi; } /* * proc_info_get_all() * get and cache all processes */ static void proc_info_get_all(void) { DIR *dir; struct dirent *dirp; dir = opendir("/proc"); if (dir == NULL) { (void)fprintf(stderr, "Cannot open /proc, is it mounted?\n"); exit(EXIT_FAILURE); } while ((dirp = readdir(dir)) != NULL) { if (isdigit(dirp->d_name[0])) { pid_t pid = atoi(dirp->d_name); (void)proc_info_get(pid); } } (void)closedir(dir); } /* * proc_list_free() * free proc list */ static void proc_list_free(proc_info_t **list) { proc_info_t *pi = *list; while (pi) { proc_info_t *next = pi->next; free(pi->cmdline); free(pi); pi = next; } *list = NULL; } /* * proc_info_free() * free process info */ static void proc_info_free(void) { int i; for (i = 0; i < TABLE_SIZE; i++) proc_list_free(&proc_infos[i]); } /* * file_stat_free() * free file stats hash table */ static void file_stat_free(void) { int i; for (i = 0; i < TABLE_SIZE; i++) { file_stat_t *fs = file_stats[i]; while (fs) { file_stat_t *next = fs->next; free(fs->path); free(fs); fs = next; } } } /* * file_stat_get() * get file stats on a file touched by a given process by PID. * existing stats are returned, new stats are allocated and returned. */ static file_stat_t *file_stat_get(const char *str, const pid_t pid) { const unsigned long h = hash_djb2a(str, pid); file_stat_t *fs = file_stats[h]; while (fs) { if (!strcmp(str, fs->path) && (pid == fs->pid)) return fs; fs = fs->next; } if ((fs = calloc(1, sizeof(*fs))) == NULL) pr_nomem("allocating file stats"); if ((fs->path = strdup(str)) == NULL) { free(fs); pr_nomem("allocating file stats pathname"); } fs->next = file_stats[h]; fs->pid = pid; file_stats[h] = fs; file_stats_size++; return fs; } /* * filter_out() * return true if pathname is excluded and not included * - excludes takes higher priority over includes */ static bool filter_out(const char *pathname) { pathname_t *pe; /* Check if pathname is on exclude list */ for (pe = paths_exclude; pe; pe = pe->next) { if (!strncmp(pe->pathname, pathname, pe->pathlen)) return true; } /* No include list, assume include all */ if (!paths_include) return false; /* Check if pathname is on the include list */ for (pe = paths_include; pe; pe = pe->next) { if (!strncmp(pe->pathname, pathname, pe->pathlen)) return false; } return true; } /* * mark() * add a new fanotify mask */ static int mark(int fan_fd, const char *pathname, int *count) { int ret; ret = fanotify_mark(fan_fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_ACCESS| FAN_MODIFY | FAN_OPEN | FAN_CLOSE | FAN_ONDIR | FAN_EVENT_ON_CHILD, AT_FDCWD, pathname); if (ret >= 0) { (*count)++; return 0; } return -1; } /* * fnotify_event_init() * initialize fnotify */ static int fnotify_event_init(void) { int fan_fd, count = 0; FILE* mounts; struct mntent* mount; if ((fan_fd = fanotify_init(0, 0)) < 0) pr_error("cannot initialize fanotify"); /* No paths given, do all mount points */ if ((mounts = setmntent("/proc/self/mounts", "r")) == NULL) { (void)close(fan_fd); pr_error("setmntent cannot get mount points from /proc/self/mounts"); } /* * Gather all mounted file systems and monitor them */ while ((mount = getmntent(mounts)) != NULL) (void)mark(fan_fd, mount->mnt_dir, &count); if (endmntent(mounts) < 0) { (void)close(fan_fd); pr_error("endmntent failed"); } /* This really should not happen, / is always mounted */ if (!count) { (void)fprintf(stderr, "no mount points could be monitored\n"); (void)close(fan_fd); exit(EXIT_FAILURE); } return fan_fd; } /* * fnotify_get_filename() * look up a in-use file descriptor from a given pid * and find the associated filename */ static char *fnotify_get_filename(const pid_t pid, const int fd) { char buf[256]; char path[PATH_MAX]; ssize_t len; char *filename; /* * With fnotifies, fd of the file is added to the process * fd array, so we just pick them up from /proc/self. Use * a pid of -1 for self */ if (pid == -1) snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd); else snprintf(buf, sizeof(buf), "/proc/%d/fd/%d", pid, fd); len = readlink(buf, path, sizeof(path)); if (len < 0) { filename = strdup("(unknown)"); } else { if (opt_flags & OPT_INODE) { struct stat statbuf; if (fstat(fd, &statbuf) < 0) { (void)snprintf(buf, sizeof(buf), "%-10.10s %11s", "(?:?)", "?"); } else { (void)snprintf(buf, sizeof(buf), "%11lu %s", statbuf.st_ino, dev_name(statbuf.st_dev)); } filename = strdup(buf); } else if (opt_flags & OPT_DEVICE) { struct stat statbuf; if (fstat(fd, &statbuf) < 0) { (void)snprintf(buf, sizeof(buf), "?:?"); } else { (void)snprintf(buf, sizeof(buf), "%s", dev_name(statbuf.st_dev)); } filename = strdup(buf); } else { /* * In an ideal world we should allocate the path * based on a lstat'd size, but because this can be * racey on has to re-check, which involves * re-allocing the buffer. Since we need to be * fast let's just fetch up to PATH_MAX-1 of data. */ path[len >= PATH_MAX ? PATH_MAX - 1 : len] = '\0'; filename = strdup(path); } } return filename; } /* * fnotify_mask_to_str() * convert fnotify mask to readable string */ static const char *fnotify_mask_to_str(const int mask) { static char modes[5]; modes[0] = (mask & FAN_OPEN) ? 'O' : '-'; modes[1] = (mask & (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)) ? 'C' : '-'; modes[2] = (mask & FAN_ACCESS) ? 'R' : '-'; modes[3] = (mask & (FAN_MODIFY | FAN_CLOSE_WRITE)) ? 'W' : '-'; modes[4] = '\0'; return modes; } /* * fnotify_event_show() * if event is another masked event on any previous * file activity just or-in the event mask and return, * otherwise dump out the previous event and start * accumulating event masks for the new event. * * calls with null args will flush out the last * pending event. */ static void fnotify_event_show( file_stat_t *fs, char *filename, const uint64_t mask) { struct tm tm; static stash_info_t previous; char str[64]; int pid_size; if (!(opt_flags & OPT_VERBOSE)) { free(filename); return; } get_tm(&tm); if (previous.fs == NULL) { /* Stash for first the first time */ previous.fs = fs; previous.mask = mask; previous.filename = filename; previous.tm = tm; previous.count = 0; return; } /* * merge mode, same fs info and filename.. * then merge flags and wait for next event */ if ((opt_flags & OPT_MERGE) && (fs == previous.fs) && (tm.tm_sec == previous.tm.tm_sec) && (tm.tm_min == previous.tm.tm_min) && (tm.tm_hour == previous.tm.tm_hour) && (tm.tm_mday == previous.tm.tm_mday) && (tm.tm_mon == previous.tm.tm_mon) && (tm.tm_year == previous.tm.tm_year) && (filename != NULL) && !strcmp(filename, previous.filename)) { previous.mask |= mask; previous.count++; free(filename); return; } pid_size = pid_max_digits(); /* * Event for a different file and process has come in * so flush out old.. */ count_to_str((double)previous.count, str, sizeof(str)); (void)printf("%2.2d/%2.2d/%-2.2d %2.2d:%2.2d:%2.2d %s (%4.4s) %*d %-15.15s %s\n", previous.tm.tm_mday, previous.tm.tm_mon + 1, (previous.tm.tm_year+1900) % 100, previous.tm.tm_hour, previous.tm.tm_min, previous.tm.tm_sec, str, fnotify_mask_to_str(previous.mask), pid_size, previous.fs->pid, proc_info_get(previous.fs->pid)->cmdline, (opt_flags & OPT_DIRNAME_STRIP) ? basename(previous.filename) : previous.filename); /* Free old filename and stash new event */ free(previous.filename); previous.fs = fs; previous.mask = mask; previous.filename = filename; previous.tm = tm; previous.count = 0; } /* * fnotify_event_add() * add a new fnotify event */ static int fnotify_event_add(const struct fanotify_event_metadata *metadata) { char *filename; file_stat_t *fs; if ((metadata->fd == FAN_NOFD) && (metadata->fd < 0)) return 0; if (metadata->pid == my_pid) goto tidy; if (opt_flags & OPT_PID) { proc_info_t *p; bool found = false; for (p = proc_list; p; p = p->next) { if (p->pid && p->pid == metadata->pid) { found = true; break; } } if (!found && named_procs) { proc_info_t *cached_p = proc_info_get(metadata->pid); for (p = proc_list; p; p = p->next) { if (p->cmdline && strstr(p->cmdline, cached_p->cmdline)) { found = true; break; } } } if (!found) goto tidy; } filename = fnotify_get_filename(-1, metadata->fd); if (filename == NULL) { pr_error("allocating fnotify filename"); goto tidy; } if (filter_out(filename)) { free(filename); goto tidy; } fs = file_stat_get(filename, metadata->pid); if (metadata->mask & FAN_OPEN) { fs->open++; fs->total++; } if (metadata->mask & (FAN_CLOSE_WRITE | FAN_CLOSE_NOWRITE)) { fs->close++; fs->total++; } if (metadata->mask & FAN_ACCESS) { fs->read++; fs->total++; } if (metadata->mask & (FAN_MODIFY | FAN_CLOSE_WRITE)) { fs->write++; fs->total++; } /* * Note: this stashes filename which is * free'd on next call if fs and filename * are different */ fnotify_event_show(fs, filename, metadata->mask); tidy: (void)close(metadata->fd); return 0; } /* * file_stat_cmp() * compare file stats, sort by total and if they are * the same, sort by pathname */ static int file_stat_cmp(const void *p1, const void *p2) { file_stat_t *const *fs1 = (file_stat_t *const *)p1; file_stat_t *const *fs2 = (file_stat_t *const *)p2; if (opt_flags & OPT_SORT_BY_PID) { if ((*fs1)->pid < (*fs2)->pid) return -1; if ((*fs1)->pid > (*fs2)->pid) return 1; /* Fall through if pids equal */ } if ((*fs1)->total == (*fs2)->total) return strcmp((*fs1)->path, (*fs2)->path); if ((*fs1)->total > (*fs2)->total) return -1; else return 1; } /* * file_stat_dump() * dump file stats and free hash table for next iteration */ static void file_stat_dump(const double duration, const unsigned long top) { file_stat_t **sorted; uint64_t i, j; char ts[32]; double dur = (opt_flags & OPT_CUMULATIVE) ? 1.0 : duration; int pid_size; if (!(opt_flags & OPT_FORCE) && !file_stats_size) return; sorted = calloc(file_stats_size, sizeof(file_stat_t *)); if (sorted == NULL) pr_error("allocating file stats"); for (j = 0, i = 0; i < TABLE_SIZE; i++) { file_stat_t *fs = file_stats[i]; while (fs) { sorted[j++] = fs; fs = fs->next; } if (!(opt_flags & OPT_CUMULATIVE)) file_stats[i] = NULL; } qsort(sorted, file_stats_size, sizeof(file_stat_t *), file_stat_cmp); if (opt_flags & OPT_TIMESTAMP) { struct tm tm; get_tm(&tm); (void)snprintf(ts, sizeof(ts), " [%2.2d/%2.2d/%-2.2d %2.2d:%2.2d:%2.2d]\n", tm.tm_mday, tm.tm_mon + 1, (tm.tm_year+1900) % 100, tm.tm_hour, tm.tm_min, tm.tm_sec); } else { *ts = '\0'; } pid_size = pid_max_digits(); (void)printf("Total Open Close Read Write %*.*s Process %s%s\n", pid_size, pid_size, "PID", (opt_flags & OPT_INODE) ? " Inode Device Device Name" : (opt_flags & OPT_DEVICE) ? "Device Device Name" : "Pathname", ts); for (j = 0; j < file_stats_size; j++) { if (top && j <= top) { char buf[5][32]; (void)printf("%s %s %s %s %s %*d %-15.15s %s\n", count_to_str(sorted[j]->total / dur, buf[0], sizeof(buf[0])), count_to_str(sorted[j]->open / dur, buf[1], sizeof(buf[1])), count_to_str(sorted[j]->close / dur, buf[2], sizeof(buf[2])), count_to_str(sorted[j]->read/ dur, buf[3], sizeof(buf[3])), count_to_str(sorted[j]->write / dur, buf[4], sizeof(buf[4])), pid_size, sorted[j]->pid, proc_info_get(sorted[j]->pid)->cmdline, (opt_flags & OPT_DIRNAME_STRIP) ? basename(sorted[j]->path) : sorted[j]->path); } if (!(opt_flags & OPT_CUMULATIVE)) { free(sorted[j]->path); free(sorted[j]); } } free(sorted); if (!(opt_flags & OPT_CUMULATIVE)) file_stats_size = 0; printf("\n"); } /* * parse_pid_list() * parse pids/process name list */ static int parse_pid_list(char *arg) { char *str, *token; for (str = arg; (token = strtok(str, ",")) != NULL; str = NULL) { pid_t pid = 0; char *name = NULL; proc_info_t *p; /* Hit next option, which means no args given */ if (token[0] == '-') break; if (isdigit(token[0])) { errno = 0; pid = strtol(token, NULL, 10); if (errno) { (void)fprintf(stderr, "Invalid PID specified.\n"); return -1; } } else { name = strdup(token); if (!name) { (void)fprintf(stderr, "Out of memory allocating process name info.\n"); return -1; } named_procs = true; } p = calloc(1, sizeof(proc_info_t)); if (!p) { (void)fprintf(stderr, "Out of memory allocating process info.\n"); free(name); return -1; } p->pid = pid; p->cmdline = name; p->whence = 0.0; /* Not used */ p->next = proc_list; proc_list = p; } if (proc_list == NULL) { (void)fprintf(stderr, "No valid process ID or names given.\n"); return -1; } return 0; } /* * parse_path_list() * parse pathname list */ static int parse_pathname_list(char *arg, pathname_t **list) { char *str, *token; bool added = false; for (str = arg; (token = strtok(str, ",")) != NULL; str = NULL) { pathname_t *p; p = calloc(1, sizeof(pathname_t)); if (!p) { (void)fprintf(stderr, "Out of memory allocating pathname info.\n"); return -1; } p->pathname = strdup(token); if (!p->pathname) { (void)fprintf(stderr, "Out of memory allocating pathname info.\n"); free(p); return -1; } p->pathlen = strlen(p->pathname); p->next = *list; *list = p; added = true; } if (!added) { (void)fprintf(stderr, "No valid pathnames were given.\n"); return -1; } return 0; } /* * pathname_list_free() * free and nullify pathname list */ static void pathname_list_free(pathname_t **list) { pathname_t *p = *list; while (p) { pathname_t *next = p->next; free(p->pathname); free(p); p = next; } *list = NULL; } /* * show_usage() * how to use */ static void show_usage(void) { (void)printf("%s, version %s\n\n", app_name, VERSION); (void)printf("Options are:\n" " -c cumulative totals over time\n" " -d strip directory off the filenames\n" " -D order stats by unique device\n" " -f force output\n" " -h show this help\n" " -i specify pathnames to include on path events\n" " -I order stats by unique device and inode\n" " -m merge events on same file and pid in same second\n" " -n no stats, just -v verbose mode only\n" " -p PID collect stats for just process with pid PID\n" " -P sort stats by process ID\n" " -s disable scaling of file counts\n" " -t N show just the busiest N files\n" " -T show timestamp\n" " -v verbose mode, dump out all file activity\n" " -x specify pathnames to exclude on path events\n"); } int main(int argc, char **argv) { unsigned long count = 0, top = -1; void *buffer; ssize_t len; int fan_fd, ret, i; float duration_secs = 1.0; bool forever = true; struct sigaction new_action; struct timeval tv1, tv2; for (;;) { int c = getopt(argc, argv, "hvdt:p:PcTsi:x:nmMDIf"); if (c == -1) break; switch (c) { case 'c': opt_flags |= OPT_CUMULATIVE; break; case 'd': opt_flags |= OPT_DIRNAME_STRIP; break; case 'D': opt_flags |= OPT_DEVICE; break; case 'f': opt_flags |= OPT_FORCE; break; case 'h': show_usage(); exit(EXIT_SUCCESS); case 'i': if (parse_pathname_list(optarg, &paths_include)) exit(EXIT_FAILURE); break; case 'I': opt_flags |= OPT_INODE; break; case 'm': opt_flags |= OPT_MERGE; break; case 'n': opt_flags |= (OPT_NOSTATS | OPT_VERBOSE); break; case 'p': if (parse_pid_list(optarg) < 0) { (void)fprintf(stderr, "Invalid value for -p option.\n"); exit(EXIT_FAILURE); } opt_flags |= OPT_PID; break; case 'P': opt_flags |= OPT_SORT_BY_PID; break; case 's': opt_flags &= ~OPT_SCALE; break; case 't': errno = 0; top = strtol(optarg, NULL, 10); if (errno) { (void)fprintf(stderr, "Invalid value for -t option.\n"); exit(EXIT_FAILURE); } if (top < 1) { (void)fprintf(stderr, "Value for -t option must be 1 or more.\n"); exit(EXIT_FAILURE); } break; case 'T': opt_flags |= OPT_TIMESTAMP; break; case 'v': opt_flags |= OPT_VERBOSE; break; case 'x': if (parse_pathname_list(optarg, &paths_exclude)) exit(EXIT_FAILURE); break; case '?': (void)printf("Try '%s -h' for more information.\n", app_name); exit(EXIT_FAILURE); default: show_usage(); exit(EXIT_FAILURE); } } if (optind < argc) { duration_secs = get_seconds(argv[optind++]); if (duration_secs < 0.5) { (void)fprintf(stderr, "Duration must be 0.5 or more.\n"); exit(EXIT_FAILURE); } } if (optind < argc) { forever = false; errno = 0; count = strtol(argv[optind++], NULL, 10); if (errno) { (void)fprintf(stderr, "Invalid count value\n"); exit(EXIT_FAILURE); } if (count < 1) { (void)fprintf(stderr, "Count must be > 0\n"); exit(EXIT_FAILURE); } } if ((opt_flags & (OPT_INODE | OPT_DEVICE)) == (OPT_INODE | OPT_DEVICE)) { (void)fprintf(stderr, "Cannot have -I and -D enabled together.\n"); exit(EXIT_FAILURE); } if ((getuid() != 0) || (geteuid() != 0)) { (void)fprintf(stderr, "%s requires root privileges to run.\n", app_name); exit(EXIT_FAILURE); } memset(&new_action, 0, sizeof(new_action)); for (i = 0; signals[i] != -1; i++) { new_action.sa_handler = signals[i] == SIGUSR1 ? handle_sigusr1 : handle_sig; (void)sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if (sigaction(signals[i], &new_action, NULL) < 0) { (void)fprintf(stderr, "sigaction failed: errno=%d (%s)\n", errno, strerror(errno)); exit(EXIT_FAILURE); } } /* Pre-cache dev and mount dev info */ if (opt_flags & (OPT_INODE | OPT_DEVICE)) { dev_cache_devs("/dev", 0, 5); dev_cache_mounts(); } proc_info_get_all(); my_pid = getpid(); ret = posix_memalign(&buffer, BUFFER_SIZE, BUFFER_SIZE); if (ret != 0 || buffer == NULL) { (void)fprintf(stderr,"cannot allocate 4K aligned buffer"); exit(EXIT_FAILURE); } fan_fd = fnotify_event_init(); if (fan_fd < 0) { (void)fprintf(stderr, "cannot init fnotify"); exit(EXIT_FAILURE); } while (!stop_fnotifystat && (forever || count--)) { double duration; if (gettimeofday(&tv1, NULL) < 0) pr_error("gettimeofday failed"); while(!stop_fnotifystat) { fd_set rfds; double remaining; if (gettimeofday(&tv2, NULL) < 0) pr_error("gettimeofday failed"); remaining = duration_secs + timeval_double(&tv1) - timeval_double(&tv2); if (remaining < 0.0) break; FD_ZERO(&rfds); FD_SET(fan_fd, &rfds); tv2.tv_sec = remaining; tv2.tv_usec = (remaining - (int)remaining) * 1000000.0; ret = select(fan_fd + 1, &rfds, NULL, NULL, &tv2); if (ret == -1) { if (errno == EINTR) { stop_fnotifystat = true; break; } pr_error("select failed"); } if (ret == 0) break; if ((len = read(fan_fd, (void *)buffer, BUFFER_SIZE)) > 0) { struct fanotify_event_metadata *metadata; metadata = (struct fanotify_event_metadata *)buffer; while (FAN_EVENT_OK(metadata, len)) { if (stop_fnotifystat || fnotify_event_add(metadata) < 0) break; metadata = FAN_EVENT_NEXT(metadata, len); } } } if (gettimeofday(&tv2, NULL) < 0) pr_error("gettimeofday failed"); duration = timeval_double(&tv2) - timeval_double(&tv1); if (!(opt_flags & OPT_NOSTATS)) file_stat_dump(duration, top); } /* Flush and free previous event */ fnotify_event_show(NULL, NULL, 0); (void)close(fan_fd); free(buffer); file_stat_free(); proc_info_free(); proc_list_free(&proc_list); pathname_list_free(&paths_include); pathname_list_free(&paths_exclude); dev_cache_free(); exit(EXIT_SUCCESS); } fnotifystat-0.02.05/.travis.yml0000644000175000017500000000033113625734052015315 0ustar ckingckingdist: bionic sudo: required matrix: include: - env: PEDANTIC=1 before_install: - sudo apt-get update -q - sudo apt-get install build-essential language: c script: - make -j2 PEDANTIC=$PEDANTIC fnotifystat-0.02.05/Makefile0000644000175000017500000000362613625734052014656 0ustar ckingcking# # Copyright (C) 2014-2020 Canonical, Ltd. # # 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 2 # 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # VERSION=0.02.05 CFLAGS += -Wall -Wextra -DVERSION='"$(VERSION)"' -O2 # # Pedantic flags # ifeq ($(PEDANTIC),1) CFLAGS += -Wabi -Wcast-qual -Wfloat-equal -Wmissing-declarations \ -Wmissing-format-attribute -Wno-long-long -Wpacked \ -Wredundant-decls -Wshadow -Wno-missing-field-initializers \ -Wno-missing-braces -Wno-sign-compare -Wno-multichar endif BINDIR=/usr/sbin MANDIR=/usr/share/man/man8 BASHDIR=/usr/share/bash-completion/completions fnotifystat: fnotifystat.o $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) fnotifystat.8.gz: fnotifystat.8 gzip -c $< > $@ dist: rm -rf fnotifystat-$(VERSION) mkdir fnotifystat-$(VERSION) cp -rp Makefile fnotifystat.c fnotifystat.8 COPYING \ .travis.yml bash-completion fnotifystat-$(VERSION) tar -zcf fnotifystat-$(VERSION).tar.gz fnotifystat-$(VERSION) rm -rf fnotifystat-$(VERSION) clean: rm -f fnotifystat fnotifystat.o fnotifystat.8.gz rm -f fnotifystat-$(VERSION).tar.gz install: fnotifystat fnotifystat.8.gz mkdir -p ${DESTDIR}${BINDIR} cp fnotifystat ${DESTDIR}${BINDIR} mkdir -p ${DESTDIR}${MANDIR} cp fnotifystat.8.gz ${DESTDIR}${MANDIR} mkdir -p ${DESTDIR}${BASHDIR} cp bash-completion/fnotifystat ${DESTDIR}${BASHDIR} fnotifystat-0.02.05/bash-completion/0000755000175000017500000000000013625734052016273 5ustar ckingckingfnotifystat-0.02.05/bash-completion/fnotifystat0000644000175000017500000000254313625734052020574 0ustar ckingcking# fnotifystat tab completion for bash. # # Copyright (C) 2020 Canonical # # 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 2 # 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. _fnotifystat() { local cur prev words cword _init_completion || return case "$prev" in '-p') COMPREPLY=( $(compgen -W '$(command ps axo pid | sed 1d) ' $cur ) ) return 0 ;; '-t') COMPREPLY=( $(compgen -W "numfiles" -- $cur) ) return 0 ;; esac case "$cur" in -*) OPTS="-c -d -D -f -h -i -I -m -n -p -P -s -t -T -v -x" COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) return 0 ;; esac return 0 } # load the completion complete -F _fnotifystat fnotifystat fnotifystat-0.02.05/fnotifystat.80000644000175000017500000001400513625734052015652 0ustar ckingcking.\" Hey, EMACS: -*- nroff -*- .\" First parameter, NAME, should be all caps .\" Second parameter, SECTION, should be 1-8, maybe w/ subsection .\" other parameters are allowed: see man(7), man(1) .TH FNOTIFYSTAT 8 "Apr 14, 2017" .\" Please adjust this date whenever revising the manpage. .\" .\" Some roff macros, for reference: .\" .nh disable hyphenation .\" .hy enable hyphenation .\" .ad l left justify .\" .ad b justify to both left and right margins .\" .nf disable filling .\" .fi enable filling .\" .br insert line break .\" .sp insert n+1 empty lines .\" for manpage-specific macros, see man(7) .SH NAME fnotifystat \- a tool to show file system activity .br .SH SYNOPSIS .B fnotifystat .RI [options] " [delay " [count]] .br .SH DESCRIPTION fnotifystat is a program that dumps the file system activity in a given period of time. .br Statistics are output every 'delay' seconds. One can use the 'm' (minutes), 'h' (hours), 'd' (days), 'w' (weeks) or 'y' (years) suffix to specify large delay intervals. By default the delay is 1 second. Statistics are output 'count' many times before exiting. If count is not specified then fnotifystat will run until it is stopped. Note that if no file activity has occurred during a sample delay period then no new statistics are output. The \-f option will force output if desired. By default, fnotifystat will display the following statistics every sample 'delay' seconds: .IP \[bu] 2 Average number of opens, closes, reads and writes over the sample period .IP \[bu] Average number of opens over the sample period .IP \[bu] Average number of closes over the sample period .IP \[bu] Average number of reads over the sample period .IP \[bu] Average number of writes over the sample period .IP \[bu] Process ID .IP \[bu] Pathname .br .TP The \-I option will display the device (major, minor) number and inode instead of the filename while the \-I option will just show the device (major, minor) number. .br .TP The \-c option will display the cumulative count of the open, close, read and writes rather than the averages. .SH OPTIONS fnotifystat options are as follow: .TP .B \-c dump cumulative totals over all time rather than totals over each sample period. .TP .B \-d strip full directory path off the filenames. .TP .B \-f force statistics output even if no file activity has occurred during the last sampling period. .TP .B \-D order statistics by unique device. Rather than ordering periodic statistics by unique filename, instead order them by unique device (major, minor) number. This allows one to see file activity on a per device basis. .TP .B \-h show help .TP .B \-i pathnames to include in filename activity. By default, all mounted filesystems are monitored, however, this may produce to much information. The \-i option allows one to provide a comma separated list of just the specific paths to monitor. .TP .B \-I order statistics by unique device and inode. Rather than ordering periodic statistics by unique filename, instead order them by unique device (major, minor) and inode number. .TP .B \-m merge events. The default \-v verbose output will show all new events that fnotifystat detects on a file by a specific process. The \-m option will merge all consecutive events that occur on the same file from the same process to reduce the amount of event output. .TP .B \-n no periodic statistics, just verbose mode only. Do not display periodic file activity statistics, but just show the verbose mode \-v file activity. .TP .B \-p proclist monitor specific processes. proclist is a comma separated list of process IDs or process names to monitor. .TP .B \-P sort stats by pid first, then by totals and filename .TP .B \-s turn off data scaling. By default the read, write, open and close counts are displayed by scaling by multiple of thousands with suffixes of K (thousands), M (millions), B (billions). The \-s option disables this, resulting in less aesthetically pleasing unaligned columns of data. .TP .B \-t N only display the top busiest N files. .TP .B \-T show timestamp .TP .B \-v verbose mode, dump all file activity. This will display the following file access information: .TS l. Date (in DD/MM/YY format) Time (in HH:MM:SS format) Event count Access type, O=Open, C=Close, R=Read, W=Write Process ID Process Name Name of accessed file .TE .br Note that the names of deleted filenames cannot be determined and are flagged by the "(deleted)" tag. .TP .B \-x pathnames to exclude from filename activity. By default, all mounted filesystems are monitored, however, this may produce to much information. The \-x option allows one to provide a comma separated list of pathnames to exclude. Matching is performed on partial pathnames, for example \-x /pro will exclude all paths starting with /pro, such as /proc. Note that excluding a file with \-x takes higher precedence over including a file with \-i. .TP .B SIGUSR1 Sending SIGUSR1 to fnotifystat will toggle the verbose option (\-v) on/off. .SH EXAMPLES .LP Show file activity every second until stopped. .RS 8 sudo fnotifystat .RE .LP Show the top 10 active files every 60 seconds until stopped. .RS 8 sudo fnotifystat \-t 10 60 .RE .LP Show file acivity every 10 seconds just 6 times. .RS 8 sudo fnotifystat 10 6 .RE .LP Show file activity of thunderbird and process ID 1827. .RS 8 sudo fnotifystat \-p thunderbird,1827 .RE .LP Show every file notify event and the top 20 active activity files over a single period of 5 minutes. .RS 8 sudo notifystat \-v \-d \-c 5m 1 .RE .LP Just show every file notify event on /sys and /proc and no periodic statisics. .RS 8 sudo fnotifystat \-n \-i /sys,/proc .RE .SH AUTHOR fnotifystat was written by Colin King .PP This manual page was written by Colin King , for the Ubuntu project (but may be used by others). .SH COPYRIGHT Copyright \(co 2014-2018 Canonical Ltd. .br This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. fnotifystat-0.02.05/COPYING0000644000175000017500000004325413625734052014252 0ustar ckingcking GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.