solaris - Programatically implementing tail -f in pure C -
i'm trying implement solution in pure c monitor new entries made log file records high volume of requests web service.
i tail -f, change in log file results in process getting new changes instantly.
this needs run on solaris 10, unfortunately.
i know question has been asked , answered in other threads, none of solutions acceptable situation
1) solution must not require super user access in way. enterprise production environment, no superuser access available me on system, can't install driver.
2) log file large. parsing entirely, repeatedly new changes not acceptable.
it seems me if can run tail -f non-privileged user, should able same programmatically same user. realize nice hack pipe output tail -f process, though cleaner.
this straightforward - read, , if read 0 bytes, wait specified time. illustration (open own files , improve buffer , error handling taste). have edited show error handling , seeking last lines should occur, , fixed position of sleep(). no means complete example, indication of how things done.
#include <unistd.h> #include <stdio.h> #define nbuf 1024 int main() { char buf[nbuf]; ssize_t rcount, wcount; int fin = 0, fout = 1; /* or use open. */ /* code display last 10 lines goes here. */ while (1) { while ((rcount = read (fin, buf, nbuf)) > 0) { wcount = write (fout, buf, rcount); if (wcount != rcount) { perror("write didn't work."); /* handle error here, exit() or whatever. */ } } if (rcount == -1) { perror("read didn_t work..."); /* handle error here, exit() or else. */ sleep (1); } }
Comments
Post a Comment