r8219: Merge the new open code from HEAD to 3.0. Haven't yet run the torture
[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(files_struct *fsp)
114 {
115         deferred_open_entry *de_array = NULL;
116         int num_de_entries, i;
117         pid_t mypid = sys_getpid();
118
119         if (!lp_defer_sharing_violations()) {
120                 return;
121         }
122
123         num_de_entries = get_deferred_opens(fsp->conn, fsp->dev, fsp->inode, &de_array);
124         for (i = 0; i < num_de_entries; i++) {
125                 deferred_open_entry *entry = &de_array[i];
126                 if (entry->pid == mypid) {
127                         /*
128                          * We need to notify ourself to retry the open.
129                          * Do this by finding the queued SMB record, moving it
130                          * to the head of the queue and changing the wait time to zero.
131                          */
132                         schedule_sharing_violation_open_smb_message(entry->mid);
133                 } else {
134                         send_deferred_open_retry_message(entry);
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         share_mode_entry *share_entry = NULL;
151         size_t share_entry_count = 0;
152         BOOL delete_file = False;
153         connection_struct *conn = fsp->conn;
154         int saved_errno = 0;
155         int err = 0;
156         int err1 = 0;
157
158         remove_pending_lock_requests_by_fid(fsp);
159
160         if (fsp->aio_write_behind) {
161                 /*
162                  * If we're finishing write behind on a close we can get a write
163                  * error here, we must remember this.
164                  */
165                 int ret = wait_for_aio_completion(fsp);
166                 if (ret) {
167                         saved_errno = ret;
168                         err1 = -1;
169                 }
170         } else {
171                 cancel_aio_by_fsp(fsp);
172         }
173  
174         /*
175          * If we're flushing on a close we can get a write
176          * error here, we must remember this.
177          */
178
179         if (close_filestruct(fsp) == -1) {
180                 saved_errno = errno;
181                 err1 = -1;
182         }
183
184         if (fsp->print_file) {
185                 print_fsp_end(fsp, normal_close);
186                 file_free(fsp);
187                 return 0;
188         }
189
190         /*
191          * Lock the share entries, and determine if we should delete
192          * on close. If so delete whilst the lock is still in effect.
193          * This prevents race conditions with the file being created. JRA.
194          */
195
196         lock_share_entry_fsp(fsp);
197
198         share_entry_count = del_share_mode(fsp, &share_entry,
199                                            &delete_file);
200
201         DEBUG(10,("close_normal_file: share_entry_count = %lu for file %s\n",
202                 (unsigned long)share_entry_count, fsp->fsp_name ));
203
204         if (share_entry_count != 0) {
205                 /* We're not the last ones -- don't delete */
206                 delete_file = False;
207         }
208
209         SAFE_FREE(share_entry);
210
211         /* Notify any deferred opens waiting on this close. */
212         notify_deferred_opens(fsp);
213
214         /*
215          * NT can set delete_on_close of the last open
216          * reference to a file.
217          */
218
219         if (normal_close && delete_file) {
220                 DEBUG(5,("close_file: file %s. Delete on close was set - deleting file.\n",
221                         fsp->fsp_name));
222                 if(SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
223                         /*
224                          * This call can potentially fail as another smbd may have
225                          * had the file open with delete on close set and deleted
226                          * it when its last reference to this file went away. Hence
227                          * we log this but not at debug level zero.
228                          */
229
230                 DEBUG(5,("close_file: file %s. Delete on close was set and unlink failed \
231 with error %s\n", fsp->fsp_name, strerror(errno) ));
232                 }
233                 process_pending_change_notify_queue((time_t)0);
234         }
235
236         unlock_share_entry_fsp(fsp);
237
238         if(fsp->oplock_type)
239                 release_file_oplock(fsp);
240
241         locking_close_file(fsp);
242
243         err = fd_close(conn, fsp);
244
245         /* Only save errno if fd_close failed and we don't already
246            have an errno saved from a flush call. */
247         if ((err1 != -1) && (err == -1)) {
248                 saved_errno = errno;
249         }
250
251         /* check for magic scripts */
252         if (normal_close) {
253                 check_magic(fsp,conn);
254         }
255
256         /*
257          * Ensure pending modtime is set after close.
258          */
259
260         if(fsp->pending_modtime && fsp->pending_modtime_owner) {
261                 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
262         } else if (fsp->last_write_time) {
263                 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
264         }
265
266         DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
267                 conn->user,fsp->fsp_name,
268                 conn->num_files_open,
269                 (err == -1 || err1 == -1) ? strerror(saved_errno) : ""));
270
271         if (fsp->fsp_name)
272                 string_free(&fsp->fsp_name);
273
274         file_free(fsp);
275
276         if (err == -1 || err1 == -1) {
277                 errno = saved_errno;
278                 return saved_errno;
279         } else {
280                 return 0;
281         }
282 }
283
284 /****************************************************************************
285  Close a directory opened by an NT SMB call. 
286 ****************************************************************************/
287   
288 static int close_directory(files_struct *fsp, BOOL normal_close)
289 {
290         remove_pending_change_notify_requests_by_fid(fsp);
291
292         /*
293          * NT can set delete_on_close of the last open
294          * reference to a directory also.
295          */
296
297         if (normal_close &&
298             get_delete_on_close_flag(fsp->dev, fsp->inode)) {
299                 BOOL ok = rmdir_internals(fsp->conn, fsp->fsp_name);
300                 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
301                         fsp->fsp_name, ok ? "succeeded" : "failed" ));
302
303                 /*
304                  * Ensure we remove any change notify requests that would
305                  * now fail as the directory has been deleted.
306                  */
307
308                 if(ok) {
309                         remove_pending_change_notify_requests_by_filename(fsp);
310                 }
311                 process_pending_change_notify_queue((time_t)0);
312         }
313
314         /*
315          * Do the code common to files and directories.
316          */
317         close_filestruct(fsp);
318         
319         if (fsp->fsp_name) {
320                 string_free(&fsp->fsp_name);
321         }
322         
323         file_free(fsp);
324         return 0;
325 }
326
327 /****************************************************************************
328  Close a 'stat file' opened internally.
329 ****************************************************************************/
330   
331 static int close_stat(files_struct *fsp)
332 {
333         /*
334          * Do the code common to files and directories.
335          */
336         close_filestruct(fsp);
337         
338         if (fsp->fsp_name)
339                 string_free(&fsp->fsp_name);
340         
341         file_free(fsp);
342         return 0;
343 }
344
345 /****************************************************************************
346  Close a files_struct.
347 ****************************************************************************/
348   
349 int close_file(files_struct *fsp, BOOL normal_close)
350 {
351         if(fsp->is_directory)
352                 return close_directory(fsp, normal_close);
353         else if (fsp->is_stat)
354                 return close_stat(fsp);
355         else
356                 return close_normal_file(fsp, normal_close);
357 }