Annotation of fam/fam/IMon.c++, Revision 1.1.1.1
1.1 trev 1: // Copyright (C) 1999 Silicon Graphics, Inc. All Rights Reserved.
2: //
3: // This program is free software; you can redistribute it and/or modify it
4: // under the terms of version 2 of the GNU General Public License as
5: // published by the Free Software Foundation.
6: //
7: // This program is distributed in the hope that it would be useful, but
8: // WITHOUT ANY WARRANTY; without even the implied warranty of
9: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Further, any
10: // license provided herein, whether implied or otherwise, is limited to
11: // this program in accordance with the express provisions of the GNU
12: // General Public License. Patent licenses, if any, provided herein do not
13: // apply to combinations of this program with other product or programs, or
14: // any other product whatsoever. This program is distributed without any
15: // warranty that the program is delivered free of the rightful claim of any
16: // third person by way of infringement or the like. See the GNU General
17: // Public License for more details.
18: //
19: // You should have received a copy of the GNU General Public License along
20: // with this program; if not, write the Free Software Foundation, Inc., 59
21: // Temple Place - Suite 330, Boston MA 02111-1307, USA.
22:
23: #include "IMon.h"
24:
25: #include <assert.h>
26: #include <errno.h>
27: #include <fcntl.h>
28:
29: #if HAVE_IMON
30: #ifdef __sgi
31: #include <sys/imon.h>
32: #else
33: #include <linux/imon.h>
34: #endif
35: #endif
36:
37: #if !defined(__FreeBSD__)
38: # include <sys/sysmacros.h>
39: #endif
40: #include <unistd.h>
41:
42: #include "Interest.h"
43: #include "Log.h"
44: #include "Scheduler.h"
45: #include "alloc.h"
46:
47: int IMon::imonfd = -2;
48: IMon::EventHandler IMon::ehandler = NULL;
49:
50: IMon::IMon(EventHandler h)
51: {
52: assert(ehandler == NULL);
53: ehandler = h;
54: }
55:
56: IMon::~IMon()
57: {
58: if (imonfd >= 0)
59: {
60: // Tell the scheduler.
61:
62: (void) Scheduler::remove_read_handler(imonfd);
63:
64: // Close the inode monitor device.
65:
66: if (close(imonfd) < 0)
67: Log::perror("can't close /dev/imon");
68: else
69: Log::debug("closed /dev/imon");
70: imonfd = -1;
71: }
72: ehandler = NULL;
73: }
74:
75: bool
76: IMon::is_active()
77: {
78: if (imonfd == -2)
79: { imonfd = imon_open();
80: if (imonfd >= 0)
81: { Log::debug("opened /dev/imon");
82: (void) Scheduler::install_read_handler(imonfd, read_handler, NULL);
83: }
84: }
85: return imonfd >= 0;
86: }
87:
88: IMon::Status
89: IMon::express(const char *name, struct stat *status)
90: {
91: if (!is_active())
92: return BAD;
93:
94: return imon_express(name, status);
95: }
96:
97: IMon::Status
98: IMon::revoke(const char *name, dev_t dev, ino_t ino)
99: {
100: if (!is_active())
101: return BAD;
102:
103: return imon_revoke(name, dev, ino);
104: }
105:
106: void
107: IMon::read_handler(int fd, void *)
108: {
109: #if HAVE_IMON
110: qelem_t readbuf[20];
111: int rc = read(fd, readbuf, sizeof readbuf);
112: if (rc < 0)
113: Log::perror("/dev/imon read");
114: else
115: { assert(rc % sizeof (qelem_t) == 0);
116: rc /= sizeof (qelem_t);
117: for (int i = 0; i < rc; i++)
118: if (readbuf[i].qe_what == IMON_OVER)
119: Log::error("imon event queue overflow");
120: else
121: { dev_t dev = readbuf[i].qe_dev;
122: ino_t ino = readbuf[i].qe_inode;
123: intmask_t what = readbuf[i].qe_what;
124: Log::debug("imon said dev %d/%d, ino %ld changed%s%s%s%s%s%s",
125: major(dev), minor(dev), ino,
126: what & IMON_CONTENT ? " CONTENT" : "",
127: what & IMON_ATTRIBUTE ? " ATTRIBUTE" : "",
128: what & IMON_DELETE ? " DELETE" : "",
129: what & IMON_EXEC ? " EXEC" : "",
130: what & IMON_EXIT ? " EXIT" : "",
131: #ifdef IMON_RENAME
132: what & IMON_RENAME ? " RENAME" : "",
133: #endif
134: ""
135: );
136: if (what & IMON_EXEC)
137: (*ehandler)(dev, ino, EXEC);
138: if (what & IMON_EXIT)
139: (*ehandler)(dev, ino, EXIT);
140: if (what & (IMON_CONTENT | IMON_ATTRIBUTE |
141: IMON_DELETE
142: #ifdef IMON_RENAME
143: | IMON_RENAME
144: #endif
145: ))
146: (*ehandler)(dev, ino, CHANGE);
147: }
148: }
149: #else
150: // I forget why/how this happens, but I know it's happened to me
151: // (something about building fam without imon support, but then it
152: // tries and succeeds? in opening /dev/imon?) Anyway, it shouldn't
153: // do that. This should be fixed.
154: Log::critical("imon event received by fam built without imon support... "
155: "tell fam@oss.sgi.com to fix this!");
156: int i_dont_have_IMON = 0;
157: assert(i_dont_have_IMON);
158: #endif // HAVE_IMON
159: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>