r12213: Final fix for #3303 - send rename messages to smbd's
[samba.git] / source / smbd / close.c
1 /* 
2    Unix SMB/CIFS implementation.
3    file closing
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 1992-2004.
6    Copyright (C) Volker Lendecke 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /****************************************************************************
26  Run a file if it is a magic script.
27 ****************************************************************************/
28
29 static void check_magic(files_struct *fsp,connection_struct *conn)
30 {
31         if (!*lp_magicscript(SNUM(conn)))
32                 return;
33
34         DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
35
36         {
37                 char *p;
38                 if (!(p = strrchr_m(fsp->fsp_name,'/')))
39                         p = fsp->fsp_name;
40                 else
41                         p++;
42
43                 if (!strequal(lp_magicscript(SNUM(conn)),p))
44                         return;
45         }
46
47         {
48                 int ret;
49                 pstring magic_output;
50                 pstring fname;
51                 SMB_STRUCT_STAT st;
52                 int tmp_fd, outfd;
53
54                 pstrcpy(fname,fsp->fsp_name);
55                 if (*lp_magicoutput(SNUM(conn)))
56                         pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
57                 else
58                         slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
59
60                 chmod(fname,0755);
61                 ret = smbrun(fname,&tmp_fd);
62                 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
63                 unlink(fname);
64                 if (ret != 0 || tmp_fd == -1) {
65                         if (tmp_fd != -1)
66                                 close(tmp_fd);
67                         return;
68                 }
69                 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
70                 if (outfd == -1) {
71                         close(tmp_fd);
72                         return;
73                 }
74
75                 if (sys_fstat(tmp_fd,&st) == -1) {
76                         close(tmp_fd);
77                         close(outfd);
78                         return;
79                 }
80
81                 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
82                 close(tmp_fd);
83                 close(outfd);
84         }
85 }
86
87 /****************************************************************************
88   Common code to close a file or a directory.
89 ****************************************************************************/
90
91 static int close_filestruct(files_struct *fsp)
92 {   
93         connection_struct *conn = fsp->conn;
94         int ret = 0;
95     
96         if (fsp->fh->fd != -1) {
97                 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1)
98                         ret = -1;
99
100                 delete_write_cache(fsp);
101         }
102
103         conn->num_files_open--;
104         SAFE_FREE(fsp->wbmpx_ptr);
105
106         return ret;
107 }    
108
109 /****************************************************************************
110  If any deferred opens are waiting on this close, notify them.
111 ****************************************************************************/
112
113 static void notify_deferred_opens(struct share_mode_lock *lck)
114 {
115         int i;
116  
117         for (i=0; i<lck->num_share_modes; i++) {
118                 struct share_mode_entry *e = &lck->share_modes[i];
119  
120                 if (!is_deferred_open_entry(e)) {
121                         continue;
122                 }
123  
124                 if (procid_is_me(&e->pid)) {
125                         /*
126                          * We need to notify ourself to retry the open.  Do
127                          * this by finding the queued SMB record, moving it to
128                          * the head of the queue and changing the wait time to
129                          * zero.
130                          */
131                         schedule_deferred_open_smb_message(e->op_mid);
132                 } else {
133                         message_send_pid(e->pid, MSG_SMB_OPEN_RETRY,
134                                          e, sizeof(*e), True);
135                 }
136         }
137 }
138
139 /****************************************************************************
140  Close a file.
141
142  If normal_close is 1 then this came from a normal SMBclose (or equivalent)
143  operation otherwise it came as the result of some other operation such as
144  the closing of the connection. In the latter case printing and
145  magic scripts are not run.
146 ****************************************************************************/
147
148 static int close_normal_file(files_struct *fsp, BOOL normal_close)
149 {
150         BOOL delete_file = False;
151         connection_struct *conn = fsp->conn;
152         int saved_errno = 0;
153         int err = 0;
154         int err1 = 0;
155         struct share_mode_lock *lck;
156
157         remove_pending_lock_requests_by_fid(fsp);
158
159         if (fsp->aio_write_behind) {
160                 /*
161                  * If we're finishing write behind on a close we can get a write
162                  * error here, we must remember this.
163                  */
164                 int ret = wait_for_aio_completion(fsp);
165                 if (ret) {
166                         saved_errno = ret;
167                         err1 = -1;
168                 }
169         } else {
170                 cancel_aio_by_fsp(fsp);
171         }
172  
173         /*
174          * If we're flushing on a close we can get a write
175          * error here, we must remember this.
176          */
177
178         if (close_filestruct(fsp) == -1) {
179                 saved_errno = errno;
180                 err1 = -1;
181         }
182
183         if (fsp->print_file) {
184                 print_fsp_end(fsp, normal_close);
185                 file_free(fsp);
186                 return 0;
187         }
188
189         /*
190          * Lock the share entries, and determine if we should delete
191          * on close. If so delete whilst the lock is still in effect.
192          * This prevents race conditions with the file being created. JRA.
193          */
194
195         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
196
197         if (lck == NULL) {
198                 DEBUG(0, ("close_file: Could not get share mode lock for file %s\n", fsp->fsp_name));
199                 return EINVAL;
200         }
201
202         if (!del_share_mode(lck, fsp)) {
203                 DEBUG(0, ("close_file: Could not delete share entry for file %s\n", fsp->fsp_name));
204         }
205
206         delete_file = lck->delete_on_close;
207
208         if (delete_file) {
209                 int i;
210                 /* See if others still have the file open. If this is the
211                  * case, then don't delete */
212                 for (i=0; i<lck->num_share_modes; i++) {
213                         if (is_valid_share_mode_entry(&lck->share_modes[i])) {
214                                 delete_file = False;
215                                 break;
216                         }
217                 }
218         }
219
220         /* Notify any deferred opens waiting on this close. */
221         notify_deferred_opens(lck);
222         reply_to_oplock_break_requests(fsp);
223
224         /*
225          * NT can set delete_on_close of the last open
226          * reference to a file.
227          */
228
229         if (normal_close && delete_file) {
230                 SMB_STRUCT_STAT sbuf;
231
232                 DEBUG(5,("close_file: file %s. Delete on close was set - deleting file.\n",
233                         fsp->fsp_name));
234
235                 /* We can only delete the file if the name we have
236                    is still valid and hasn't been renamed. */
237
238                 if(SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf) != 0) {
239                         DEBUG(5,("close_file: file %s. Delete on close was set "
240                                 "and stat failed with error %s\n",
241                                 fsp->fsp_name, strerror(errno) ));
242                 } else {
243                         if(sbuf.st_dev != fsp->dev || sbuf.st_ino != fsp->inode) {
244                                 DEBUG(5,("close_file: file %s. Delete on close was set and "
245                                         "dev and/or inode does not match\n",
246                                         fsp->fsp_name ));
247                                 DEBUG(5,("close_file: file %s. stored dev = %x, inode = %.0f "
248                                         "stat dev = %x, inode = %.0f\n",
249                                         fsp->fsp_name,
250                                         (unsigned int)fsp->dev, (double)fsp->inode,
251                                         (unsigned int)sbuf.st_dev, (double)sbuf.st_ino ));
252
253                         } else if(SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
254                                 /*
255                                  * This call can potentially fail as another smbd may have
256                                  * had the file open with delete on close set and deleted
257                                  * it when its last reference to this file went away. Hence
258                                  * we log this but not at debug level zero.
259                                  */
260
261                                 DEBUG(5,("close_file: file %s. Delete on close was set "
262                                         "and unlink failed with error %s\n",
263                                         fsp->fsp_name, strerror(errno) ));
264                         }
265                         process_pending_change_notify_queue((time_t)0);
266                 }
267         }
268
269         talloc_free(lck);
270
271         if(fsp->oplock_type)
272                 release_file_oplock(fsp);
273
274         locking_close_file(fsp);
275
276         err = fd_close(conn, fsp);
277
278         /* Only save errno if fd_close failed and we don't already
279            have an errno saved from a flush call. */
280         if ((err1 != -1) && (err == -1)) {
281                 saved_errno = errno;
282         }
283
284         /* check for magic scripts */
285         if (normal_close) {
286                 check_magic(fsp,conn);
287         }
288
289         /*
290          * Ensure pending modtime is set after close.
291          */
292
293         if(fsp->pending_modtime && fsp->pending_modtime_owner) {
294                 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
295         } else if (fsp->last_write_time) {
296                 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
297         }
298
299         DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
300                 conn->user,fsp->fsp_name,
301                 conn->num_files_open,
302                 (err == -1 || err1 == -1) ? strerror(saved_errno) : ""));
303
304         if (fsp->fsp_name)
305                 string_free(&fsp->fsp_name);
306
307         file_free(fsp);
308
309         if (err == -1 || err1 == -1) {
310                 errno = saved_errno;
311                 return saved_errno;
312         } else {
313                 return 0;
314         }
315 }
316
317 /****************************************************************************
318  Close a directory opened by an NT SMB call. 
319 ****************************************************************************/
320   
321 static int close_directory(files_struct *fsp, BOOL normal_close)
322 {
323         struct share_mode_lock *lck = 0;
324         BOOL delete_dir = False;
325
326         /*
327          * NT can set delete_on_close of the last open
328          * reference to a directory also.
329          */
330
331         lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
332
333         if (lck == NULL) {
334                 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp->fsp_name));
335                 return EINVAL;
336         }
337
338         if (!del_share_mode(lck, fsp)) {
339                 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp->fsp_name));
340         }
341
342         delete_dir = lck->delete_on_close;
343
344         talloc_free(lck);
345
346         if (normal_close && delete_dir) {
347                 BOOL ok = rmdir_internals(fsp->conn, fsp->fsp_name);
348                 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
349                         fsp->fsp_name, ok ? "succeeded" : "failed" ));
350
351                 /*
352                  * Ensure we remove any change notify requests that would
353                  * now fail as the directory has been deleted.
354                  */
355
356                 if(ok) {
357                         remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
358                         remove_pending_change_notify_requests_by_filename(fsp, NT_STATUS_DELETE_PENDING);
359
360                 }
361                 process_pending_change_notify_queue((time_t)0);
362         } else {
363                 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_CANCELLED);
364         }
365
366         /*
367          * Do the code common to files and directories.
368          */
369         close_filestruct(fsp);
370         
371         if (fsp->fsp_name) {
372                 string_free(&fsp->fsp_name);
373         }
374         
375         file_free(fsp);
376         return 0;
377 }
378
379 /****************************************************************************
380  Close a 'stat file' opened internally.
381 ****************************************************************************/
382   
383 static int close_stat(files_struct *fsp)
384 {
385         /*
386          * Do the code common to files and directories.
387          */
388         close_filestruct(fsp);
389         
390         if (fsp->fsp_name)
391                 string_free(&fsp->fsp_name);
392         
393         file_free(fsp);
394         return 0;
395 }
396
397 /****************************************************************************
398  Close a files_struct.
399 ****************************************************************************/
400   
401 int close_file(files_struct *fsp, BOOL normal_close)
402 {
403         if(fsp->is_directory)
404                 return close_directory(fsp, normal_close);
405         else if (fsp->is_stat)
406                 return close_stat(fsp);
407         else
408                 return close_normal_file(fsp, normal_close);
409 }