xref: /dflybsd-src/usr.bin/monitor/monitor.c (revision 7485684fa5c3fadb6c7a1da0d8bb6ea5da4e0f2f)
11e3b54fcSMatthew Dillon /*
21e3b54fcSMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
31e3b54fcSMatthew Dillon  *
41e3b54fcSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
51e3b54fcSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
61e3b54fcSMatthew Dillon  *
71e3b54fcSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
81e3b54fcSMatthew Dillon  * modification, are permitted provided that the following conditions
91e3b54fcSMatthew Dillon  * are met:
101e3b54fcSMatthew Dillon  *
111e3b54fcSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
121e3b54fcSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
131e3b54fcSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
141e3b54fcSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
151e3b54fcSMatthew Dillon  *    the documentation and/or other materials provided with the
161e3b54fcSMatthew Dillon  *    distribution.
171e3b54fcSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
181e3b54fcSMatthew Dillon  *    contributors may be used to endorse or promote products derived
191e3b54fcSMatthew Dillon  *    from this software without specific, prior written permission.
201e3b54fcSMatthew Dillon  *
211e3b54fcSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
221e3b54fcSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
231e3b54fcSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
241e3b54fcSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
251e3b54fcSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
261e3b54fcSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
271e3b54fcSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
281e3b54fcSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
291e3b54fcSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
301e3b54fcSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
311e3b54fcSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321e3b54fcSMatthew Dillon  * SUCH DAMAGE.
331e3b54fcSMatthew Dillon  */
341e3b54fcSMatthew Dillon 
351e3b54fcSMatthew Dillon #include <sys/types.h>
361e3b54fcSMatthew Dillon #include <sys/event.h>
371e3b54fcSMatthew Dillon #include <sys/time.h>
381e3b54fcSMatthew Dillon #include <sys/stat.h>
391e3b54fcSMatthew Dillon #include <stdio.h>
401e3b54fcSMatthew Dillon #include <stdarg.h>
411e3b54fcSMatthew Dillon #include <stdlib.h>
421e3b54fcSMatthew Dillon #include <unistd.h>
431e3b54fcSMatthew Dillon #include <string.h>
441e3b54fcSMatthew Dillon #include <fcntl.h>
451e3b54fcSMatthew Dillon 
461e3b54fcSMatthew Dillon typedef struct monitor_elm {
471e3b54fcSMatthew Dillon 	const char *path;
481e3b54fcSMatthew Dillon 	int	fd;
491e3b54fcSMatthew Dillon } *monitor_elm_t;
501e3b54fcSMatthew Dillon 
511e3b54fcSMatthew Dillon static void usage(int exit_code);
521e3b54fcSMatthew Dillon static void monitor_add(const char *path);
531e3b54fcSMatthew Dillon static void monitor_events(void);
541e3b54fcSMatthew Dillon 
551e3b54fcSMatthew Dillon static int VerboseOpt;
561e3b54fcSMatthew Dillon static int QuietOpt;
571e3b54fcSMatthew Dillon static int ExitOpt;
581e3b54fcSMatthew Dillon static int KQueueFd;
591e3b54fcSMatthew Dillon static int NumFiles;
601e3b54fcSMatthew Dillon static int MaxFiles;
611e3b54fcSMatthew Dillon static monitor_elm_t *Elms;
621e3b54fcSMatthew Dillon 
631e3b54fcSMatthew Dillon int
main(int ac,char ** av)641e3b54fcSMatthew Dillon main(int ac, char **av)
651e3b54fcSMatthew Dillon {
661e3b54fcSMatthew Dillon 	int ch;
671e3b54fcSMatthew Dillon 	int i;
681e3b54fcSMatthew Dillon 
691e3b54fcSMatthew Dillon 	while ((ch = getopt(ac, av, "qvx")) != -1) {
701e3b54fcSMatthew Dillon 		switch (ch) {
711e3b54fcSMatthew Dillon 		case 'q':
721e3b54fcSMatthew Dillon 			if (VerboseOpt > 0)
731e3b54fcSMatthew Dillon 				--VerboseOpt;
741e3b54fcSMatthew Dillon 			else
751e3b54fcSMatthew Dillon 				++QuietOpt;
761e3b54fcSMatthew Dillon 			break;
771e3b54fcSMatthew Dillon 		case 'v':
781e3b54fcSMatthew Dillon 			if (QuietOpt > 0)
791e3b54fcSMatthew Dillon 				--QuietOpt;
801e3b54fcSMatthew Dillon 			else
811e3b54fcSMatthew Dillon 				++VerboseOpt;
821e3b54fcSMatthew Dillon 			break;
831e3b54fcSMatthew Dillon 		case 'x':
841e3b54fcSMatthew Dillon 			ExitOpt = 1;
851e3b54fcSMatthew Dillon 			break;
861e3b54fcSMatthew Dillon 		default:
871e3b54fcSMatthew Dillon 			usage(1);
881e3b54fcSMatthew Dillon 			/* not reached */
891e3b54fcSMatthew Dillon 		}
901e3b54fcSMatthew Dillon 	}
911e3b54fcSMatthew Dillon 	ac -= optind;
921e3b54fcSMatthew Dillon 	av += optind;
931e3b54fcSMatthew Dillon 
941e3b54fcSMatthew Dillon 	if (ac < 1) {
951e3b54fcSMatthew Dillon 		usage(1);
961e3b54fcSMatthew Dillon 		/* not reached */
971e3b54fcSMatthew Dillon 	}
981e3b54fcSMatthew Dillon 
991e3b54fcSMatthew Dillon 	if ((KQueueFd = kqueue()) < 0) {
1001e3b54fcSMatthew Dillon 		perror("kqueue");
1011e3b54fcSMatthew Dillon 		exit(1);
1021e3b54fcSMatthew Dillon 	}
1031e3b54fcSMatthew Dillon 	NumFiles = MaxFiles = 16;
1041e3b54fcSMatthew Dillon 	Elms = calloc(MaxFiles, sizeof(monitor_elm_t));
1051e3b54fcSMatthew Dillon 
1061e3b54fcSMatthew Dillon 	for (i = 0; i < ac; ++i) {
1071e3b54fcSMatthew Dillon 		monitor_add(av[i]);
1081e3b54fcSMatthew Dillon 	}
1091e3b54fcSMatthew Dillon 	fflush(stdout);
1101e3b54fcSMatthew Dillon 	do {
1111e3b54fcSMatthew Dillon 		monitor_events();
1121e3b54fcSMatthew Dillon 		fflush(stdout);
1131e3b54fcSMatthew Dillon 	} while (ExitOpt == 0);
1141e3b54fcSMatthew Dillon 	exit(0);
1151e3b54fcSMatthew Dillon }
1161e3b54fcSMatthew Dillon 
1171e3b54fcSMatthew Dillon static
1181e3b54fcSMatthew Dillon void
monitor_add(const char * path)1191e3b54fcSMatthew Dillon monitor_add(const char *path)
1201e3b54fcSMatthew Dillon {
1211e3b54fcSMatthew Dillon 	monitor_elm_t elm;
1221e3b54fcSMatthew Dillon 	struct kevent kev;
123*09af4d5fSSascha Wildner 	int n;
1241e3b54fcSMatthew Dillon 
1251e3b54fcSMatthew Dillon 	elm = malloc(sizeof(*elm));
1261e3b54fcSMatthew Dillon 	bzero(elm, sizeof(*elm));
1271e3b54fcSMatthew Dillon 	elm->path = path;
1281e3b54fcSMatthew Dillon 	elm->fd = open(path, O_RDONLY);
1291e3b54fcSMatthew Dillon 	if (elm->fd < 0) {
1301e3b54fcSMatthew Dillon 		printf("%s\tnot found\n", path);
1311e3b54fcSMatthew Dillon 		return;
1321e3b54fcSMatthew Dillon 	}
1331e3b54fcSMatthew Dillon 	EV_SET(&kev, elm->fd, EVFILT_VNODE, EV_ADD|EV_ENABLE|EV_CLEAR,
1341e3b54fcSMatthew Dillon 	       NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|
1351e3b54fcSMatthew Dillon 	       NOTE_LINK|NOTE_RENAME|NOTE_REVOKE,
1361e3b54fcSMatthew Dillon 	       0, NULL);
1371e3b54fcSMatthew Dillon 	n = kevent(KQueueFd, &kev, 1, NULL, 0, NULL);
138*09af4d5fSSascha Wildner 	if (n < 0) {
139*09af4d5fSSascha Wildner 		perror("kqueue");
140*09af4d5fSSascha Wildner 		exit(1);
141*09af4d5fSSascha Wildner 	}
1421e3b54fcSMatthew Dillon 
1431e3b54fcSMatthew Dillon 	if (elm->fd >= NumFiles) {
1441e3b54fcSMatthew Dillon 		MaxFiles = (elm->fd + 16) * 3 / 2;
1451e3b54fcSMatthew Dillon 		Elms = realloc(Elms, MaxFiles * sizeof(elm));
146a8872c94SAntonio Huete Jimenez 		bzero(&Elms[NumFiles], (MaxFiles - NumFiles) * sizeof(elm));
1471e3b54fcSMatthew Dillon 		NumFiles = MaxFiles;
1481e3b54fcSMatthew Dillon 	}
1491e3b54fcSMatthew Dillon 	Elms[elm->fd] = elm;
1501e3b54fcSMatthew Dillon }
1511e3b54fcSMatthew Dillon 
1521e3b54fcSMatthew Dillon static
1531e3b54fcSMatthew Dillon void
monitor_events(void)1541e3b54fcSMatthew Dillon monitor_events(void)
1551e3b54fcSMatthew Dillon {
1561e3b54fcSMatthew Dillon 	struct kevent kev_array[1];
1571e3b54fcSMatthew Dillon 	struct kevent *kev;
1581e3b54fcSMatthew Dillon 	monitor_elm_t elm;
1591e3b54fcSMatthew Dillon 	struct stat st;
1601e3b54fcSMatthew Dillon 	int bno;
1611e3b54fcSMatthew Dillon 	int i;
1621e3b54fcSMatthew Dillon 	int n;
1631e3b54fcSMatthew Dillon 
1641e3b54fcSMatthew Dillon 	n = kevent(KQueueFd, NULL, 0, kev_array, 1, NULL);
1651e3b54fcSMatthew Dillon 	for (i = 0; i < n; ++i) {
1661e3b54fcSMatthew Dillon 		kev = &kev_array[i];
1671e3b54fcSMatthew Dillon 		elm = Elms[kev->ident];
1681e3b54fcSMatthew Dillon 		printf("%-23s", elm->path);
1691e3b54fcSMatthew Dillon 		if (VerboseOpt && fstat(kev->ident, &st) == 0 &&
1701e3b54fcSMatthew Dillon 		    S_ISREG(st.st_mode)) {
171a276dc6bSMatthew Dillon 			printf(" %10jd", (intmax_t)st.st_size);
1721e3b54fcSMatthew Dillon 		}
1731e3b54fcSMatthew Dillon 		while (QuietOpt == 0 && (bno = ffs(kev->fflags)) > 0) {
1741e3b54fcSMatthew Dillon 			printf(" ");
1751e3b54fcSMatthew Dillon 			--bno;
1761e3b54fcSMatthew Dillon 			kev->fflags &= ~(1 << bno);
1771e3b54fcSMatthew Dillon 			switch (1 << bno) {
1781e3b54fcSMatthew Dillon 			case NOTE_DELETE:
1791e3b54fcSMatthew Dillon 				printf("delete");
1801e3b54fcSMatthew Dillon 				break;
1811e3b54fcSMatthew Dillon 			case NOTE_WRITE:
1821e3b54fcSMatthew Dillon 				printf("write");
1831e3b54fcSMatthew Dillon 				break;
1841e3b54fcSMatthew Dillon 			case NOTE_EXTEND:
1851e3b54fcSMatthew Dillon 				printf("extend");
1861e3b54fcSMatthew Dillon 				break;
1871e3b54fcSMatthew Dillon 			case NOTE_ATTRIB:
1881e3b54fcSMatthew Dillon 				printf("attrib");
1891e3b54fcSMatthew Dillon 				break;
1901e3b54fcSMatthew Dillon 			case NOTE_LINK:
1911e3b54fcSMatthew Dillon 				printf("link");
1921e3b54fcSMatthew Dillon 				break;
1931e3b54fcSMatthew Dillon 			case NOTE_RENAME:
1941e3b54fcSMatthew Dillon 				printf("rename");
1951e3b54fcSMatthew Dillon 				break;
1961e3b54fcSMatthew Dillon 			case NOTE_REVOKE:
1971e3b54fcSMatthew Dillon 				printf("revoke");
1981e3b54fcSMatthew Dillon 				break;
1991e3b54fcSMatthew Dillon 			default:
2001e3b54fcSMatthew Dillon 				printf("%08x", 1 << bno);
2011e3b54fcSMatthew Dillon 				break;
2021e3b54fcSMatthew Dillon 			}
2031e3b54fcSMatthew Dillon 		}
2041e3b54fcSMatthew Dillon 		printf("\n");
2051e3b54fcSMatthew Dillon 	}
2061e3b54fcSMatthew Dillon }
2071e3b54fcSMatthew Dillon 
2081e3b54fcSMatthew Dillon static
2091e3b54fcSMatthew Dillon void
usage(int exit_code)2101e3b54fcSMatthew Dillon usage(int exit_code)
2111e3b54fcSMatthew Dillon {
2121e3b54fcSMatthew Dillon 	fprintf(stderr,
2131e3b54fcSMatthew Dillon 		"monitor [-vx] files...\n"
2141e3b54fcSMatthew Dillon 		"    -v      Be more verbose\n"
2151e3b54fcSMatthew Dillon 		"    -x      Exit after first event reported\n"
2161e3b54fcSMatthew Dillon 	);
2171e3b54fcSMatthew Dillon 	exit(exit_code);
2181e3b54fcSMatthew Dillon }
2191e3b54fcSMatthew Dillon 
220