Skip to content

Instantly share code, notes, and snippets.

@kuenishi kuenishi/watchdog.c
Created Mar 6, 2010

Embed
What would you like to do?
instead of omake -P.
/*
Copyright (c) 2010 kuenishi <kuenishi_at_gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
watchdog.c - A simple directory watching tool
this does something like "omake -P", if you make change
on directory (or its descent files), the watchdog executes
the command specified in the argv.
only works in MacOS (and maybe in *BSD?).
file system event watcher - with kevent. but badly works
because directory's discriptor event can't watch grand-
children's (and their descents') events. the dog watches
all directories within the directory.
*/
#include <stdio.h>
#include <fcntl.h> // for open(2)
#include <stdlib.h> // for exit(3)
// #include <unistd.h> // for pipe(2)
// #include <signal.h> // for signals
#include <string.h> // for strncat(3)
#include <sys/types.h>
#include <sys/time.h>
#ifdef __APPLE__
#include <sys/event.h> // for kevent
#include <sys/syslimits.h> // for ARG_MAX
#endif
#include <dirent.h>
void usage(){
fprintf(stderr, "usage: $> watchdog make some command\n");
}
int prepare(int pollfd, const char * watchee){
struct kevent ev;
int fd;
DIR * dirp = opendir(watchee);
struct dirent* dp;
if( dirp == NULL ){
return -1;
}
while( (dp = readdir(dirp)) != NULL ){
if( dp->d_type == DT_DIR && strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..") ){
prepare(pollfd, dp->d_name);
}
}
closedir(dirp);
fd = open(watchee, O_RDONLY);
if( fd < 0 ){
perror( watchee );
return -1;
}
EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD|EV_CLEAR, //EV_CLEAR make it edge-trigger'd.
NOTE_WRITE|NOTE_DELETE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|NOTE_RENAME|NOTE_REVOKE,
0, NULL );
//printf("polling %s ... ", watchee);
return kevent( pollfd, &ev, 1, NULL, 0, NULL );
}
int main(int args, char ** argv){
char * watchee = ".";
struct kevent ev;
if( args < 2 ){
usage();
exit(0);
}
char cmd[ARG_MAX];
int i;
sprintf(cmd, "");
for(i=1;i<args;++i){
strncat(cmd, argv[i], ARG_MAX);
strncat(cmd, " ", ARG_MAX);
}
int pfd = kqueue();
int nevents;
int flag = 1;
prepare(pfd, watchee);
//pipe(pipes);
//EV_SET(&ev, pipes[0], EVFILT_READ, EV_ADD, 0, 0, NULL );
// not working
//EV_SET(&ev, STDIN_FILENO, EVFILT_READ, EV_ADD, 0, 0, NULL);
fprintf(stderr, "watching '%s' ...\n", watchee);
do{
nevents = kevent( pfd, NULL, 0, &ev, 1, NULL );
if( nevents > 0 ){
switch( ev.fflags ){
case NOTE_DELETE:
printf("NOTE_DELETE");
break;
case NOTE_WRITE:
//printf("NOTE_WRITE - %s\n", cmd);
system(cmd);
break;
case NOTE_EXTEND:
printf("NOTE_EXTEND");
break;
case NOTE_ATTRIB:
break;
case NOTE_LINK:
printf("NOTE_LINK");
break;
case NOTE_RENAME:
printf("NOTE_RENAME");
break;
case NOTE_REVOKE:
printf("NOTE_REVOKE");
break;
default:
printf("unknown event: %d", ev.fflags);
}
}
}while(flag);
close(pfd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.