r10396: fix build breakage after an incomplete merge from 3.0
[Samba.git] / source / smbd / notify.c
blobad49dc0a2115b112dcecde9ab3e1081252f0b726
1 /*
2 Unix SMB/CIFS implementation.
3 change notify handling
4 Copyright (C) Andrew Tridgell 2000
5 Copyright (C) Jeremy Allison 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 static struct cnotify_fns *cnotify;
26 /****************************************************************************
27 This is the structure to queue to implement NT change
28 notify. It consists of smb_size bytes stored from the
29 transact command (to keep the mid, tid etc around).
30 Plus the fid to examine and notify private data.
31 *****************************************************************************/
33 struct change_notify {
34 struct change_notify *next, *prev;
35 files_struct *fsp;
36 connection_struct *conn;
37 uint32 flags;
38 char request_buf[smb_size];
39 void *change_data;
42 static struct change_notify *change_notify_list;
44 /****************************************************************************
45 Setup the common parts of the return packet and send it.
46 *****************************************************************************/
48 static void change_notify_reply_packet(char *inbuf, NTSTATUS error_code)
50 char outbuf[smb_size+38];
52 memset(outbuf, '\0', sizeof(outbuf));
53 construct_reply_common(inbuf, outbuf);
55 ERROR_NT(error_code);
58 * Seems NT needs a transact command with an error code
59 * in it. This is a longer packet than a simple error.
61 set_message(outbuf,18,0,False);
63 show_msg(outbuf);
64 if (!send_smb(smbd_server_fd(),outbuf))
65 exit_server("change_notify_reply_packet: send_smb failed.");
68 /****************************************************************************
69 Remove an entry from the list and free it, also closing any
70 directory handle if necessary.
71 *****************************************************************************/
73 static void change_notify_remove(struct change_notify *cnbp)
75 cnotify->remove_notify(cnbp->change_data);
76 DLIST_REMOVE(change_notify_list, cnbp);
77 ZERO_STRUCTP(cnbp);
78 SAFE_FREE(cnbp);
81 /****************************************************************************
82 Delete entries by fnum from the change notify pending queue.
83 *****************************************************************************/
85 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
87 struct change_notify *cnbp, *next;
89 for (cnbp=change_notify_list; cnbp; cnbp=next) {
90 next=cnbp->next;
91 if (cnbp->fsp->fnum == fsp->fnum) {
92 change_notify_remove(cnbp);
97 /****************************************************************************
98 Delete entries by mid from the change notify pending queue. Always send reply.
99 *****************************************************************************/
101 void remove_pending_change_notify_requests_by_mid(int mid)
103 struct change_notify *cnbp, *next;
105 for (cnbp=change_notify_list; cnbp; cnbp=next) {
106 next=cnbp->next;
107 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
108 change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
109 change_notify_remove(cnbp);
114 /****************************************************************************
115 Delete entries by filename and cnum from the change notify pending queue.
116 Always send reply.
117 *****************************************************************************/
119 void remove_pending_change_notify_requests_by_filename(files_struct *fsp)
121 struct change_notify *cnbp, *next;
123 for (cnbp=change_notify_list; cnbp; cnbp=next) {
124 next=cnbp->next;
126 * We know it refers to the same directory if the connection number and
127 * the filename are identical.
129 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
130 change_notify_reply_packet(cnbp->request_buf,NT_STATUS_CANCELLED);
131 change_notify_remove(cnbp);
136 /****************************************************************************
137 Return true if there are pending change notifies.
138 ****************************************************************************/
140 int change_notify_timeout(void)
142 return cnotify->select_time;
145 /****************************************************************************
146 Process the change notify queue. Note that this is only called as root.
147 Returns True if there are still outstanding change notify requests on the
148 queue.
149 *****************************************************************************/
151 BOOL process_pending_change_notify_queue(time_t t)
153 struct change_notify *cnbp, *next;
154 uint16 vuid;
156 for (cnbp=change_notify_list; cnbp; cnbp=next) {
157 next=cnbp->next;
159 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : SVAL(cnbp->request_buf,smb_uid);
161 if (cnotify->check_notify(cnbp->conn, vuid, cnbp->fsp->fsp_name, cnbp->flags, cnbp->change_data, t)) {
162 DEBUG(10,("process_pending_change_notify_queue: dir %s changed !\n", cnbp->fsp->fsp_name ));
163 change_notify_reply_packet(cnbp->request_buf,STATUS_NOTIFY_ENUM_DIR);
164 change_notify_remove(cnbp);
168 return (change_notify_list != NULL);
171 /****************************************************************************
172 Now queue an entry on the notify change list.
173 We only need to save smb_size bytes from this incoming packet
174 as we will always by returning a 'read the directory yourself'
175 error.
176 ****************************************************************************/
178 BOOL change_notify_set(char *inbuf, files_struct *fsp, connection_struct *conn, uint32 flags)
180 struct change_notify *cnbp;
182 if((cnbp = SMB_MALLOC_P(struct change_notify)) == NULL) {
183 DEBUG(0,("change_notify_set: malloc fail !\n" ));
184 return -1;
187 ZERO_STRUCTP(cnbp);
189 memcpy(cnbp->request_buf, inbuf, smb_size);
190 cnbp->fsp = fsp;
191 cnbp->conn = conn;
192 cnbp->flags = flags;
193 cnbp->change_data = cnotify->register_notify(conn, fsp->fsp_name, flags);
195 if (!cnbp->change_data) {
196 SAFE_FREE(cnbp);
197 return False;
200 DLIST_ADD(change_notify_list, cnbp);
202 /* Push the MID of this packet on the signing queue. */
203 srv_defer_sign_response(SVAL(inbuf,smb_mid));
205 return True;
208 /****************************************************************************
209 Initialise the change notify subsystem.
210 ****************************************************************************/
212 BOOL init_change_notify(void)
214 #if HAVE_KERNEL_CHANGE_NOTIFY
215 if (lp_kernel_change_notify())
216 cnotify = kernel_notify_init();
217 #endif
218 if (!cnotify) cnotify = hash_notify_init();
220 if (!cnotify) {
221 DEBUG(0,("Failed to init change notify system\n"));
222 return False;
225 return True;