r19692: Another fix.
[Samba.git] / source / smbd / close.c
bloba3ddcae11d004645c351efc05ba46136a359af45
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
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.
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.
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.
23 #include "includes.h"
25 /****************************************************************************
26 Run a file if it is a magic script.
27 ****************************************************************************/
29 static void check_magic(files_struct *fsp,connection_struct *conn)
31 if (!*lp_magicscript(SNUM(conn)))
32 return;
34 DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
37 char *p;
38 if (!(p = strrchr_m(fsp->fsp_name,'/')))
39 p = fsp->fsp_name;
40 else
41 p++;
43 if (!strequal(lp_magicscript(SNUM(conn)),p))
44 return;
48 int ret;
49 pstring magic_output;
50 pstring fname;
51 SMB_STRUCT_STAT st;
52 int tmp_fd, outfd;
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);
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;
69 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
70 if (outfd == -1) {
71 close(tmp_fd);
72 return;
75 if (sys_fstat(tmp_fd,&st) == -1) {
76 close(tmp_fd);
77 close(outfd);
78 return;
81 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
82 close(tmp_fd);
83 close(outfd);
87 /****************************************************************************
88 Common code to close a file or a directory.
89 ****************************************************************************/
91 static int close_filestruct(files_struct *fsp)
93 connection_struct *conn = fsp->conn;
94 int ret = 0;
96 if (fsp->fh->fd != -1) {
97 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1)
98 ret = -1;
100 delete_write_cache(fsp);
103 conn->num_files_open--;
104 SAFE_FREE(fsp->wbmpx_ptr);
106 return ret;
109 /****************************************************************************
110 If any deferred opens are waiting on this close, notify them.
111 ****************************************************************************/
113 static void notify_deferred_opens(struct share_mode_lock *lck)
115 int i;
117 for (i=0; i<lck->num_share_modes; i++) {
118 struct share_mode_entry *e = &lck->share_modes[i];
120 if (!is_deferred_open_entry(e)) {
121 continue;
124 if (procid_is_me(&e->pid)) {
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.
131 schedule_deferred_open_smb_message(e->op_mid);
132 } else {
133 char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
135 share_mode_entry_to_message(msg, e);
137 message_send_pid(e->pid, MSG_SMB_OPEN_RETRY,
138 msg, MSG_SMB_SHARE_MODE_ENTRY_SIZE, True);
143 /****************************************************************************
144 Deal with removing a share mode on last close.
145 ****************************************************************************/
147 static int close_remove_share_mode(files_struct *fsp, enum file_close_type close_type)
149 connection_struct *conn = fsp->conn;
150 BOOL delete_file = False;
151 struct share_mode_lock *lck;
154 * Lock the share entries, and determine if we should delete
155 * on close. If so delete whilst the lock is still in effect.
156 * This prevents race conditions with the file being created. JRA.
159 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
161 if (lck == NULL) {
162 DEBUG(0, ("close_remove_share_mode: Could not get share mode lock for file %s\n", fsp->fsp_name));
163 return EINVAL;
166 if (!del_share_mode(lck, fsp)) {
167 DEBUG(0, ("close_remove_share_mode: Could not delete share entry for file %s\n", fsp->fsp_name));
170 delete_file = (lck->delete_on_close | lck->initial_delete_on_close);
172 if (delete_file) {
173 int i;
174 /* See if others still have the file open. If this is the
175 * case, then don't delete */
176 for (i=0; i<lck->num_share_modes; i++) {
177 if (is_valid_share_mode_entry(&lck->share_modes[i])) {
178 delete_file = False;
179 break;
184 /* Notify any deferred opens waiting on this close. */
185 notify_deferred_opens(lck);
186 reply_to_oplock_break_requests(fsp);
189 * NT can set delete_on_close of the last open
190 * reference to a file.
193 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
194 delete_file &&
195 lck->delete_token) {
196 SMB_STRUCT_STAT sbuf;
198 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set - deleting file.\n",
199 fsp->fsp_name));
201 /* Become the user who requested the delete. */
203 if (!push_sec_ctx()) {
204 smb_panic("close_remove_share_mode: file %s. failed to push sec_ctx.\n");
207 set_sec_ctx(lck->delete_token->uid,
208 lck->delete_token->gid,
209 lck->delete_token->ngroups,
210 lck->delete_token->groups,
211 NULL);
213 /* We can only delete the file if the name we have
214 is still valid and hasn't been renamed. */
216 if(SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf) != 0) {
217 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
218 "and stat failed with error %s\n",
219 fsp->fsp_name, strerror(errno) ));
220 } else {
221 if(sbuf.st_dev != fsp->dev || sbuf.st_ino != fsp->inode) {
222 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set and "
223 "dev and/or inode does not match\n",
224 fsp->fsp_name ));
225 DEBUG(5,("close_remove_share_mode: file %s. stored dev = %x, inode = %.0f "
226 "stat dev = %x, inode = %.0f\n",
227 fsp->fsp_name,
228 (unsigned int)fsp->dev, (double)fsp->inode,
229 (unsigned int)sbuf.st_dev, (double)sbuf.st_ino ));
231 } else if(SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
233 * This call can potentially fail as another smbd may have
234 * had the file open with delete on close set and deleted
235 * it when its last reference to this file went away. Hence
236 * we log this but not at debug level zero.
239 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
240 "and unlink failed with error %s\n",
241 fsp->fsp_name, strerror(errno) ));
244 /* unbecome user. */
245 pop_sec_ctx();
247 process_pending_change_notify_queue((time_t)0);
250 TALLOC_FREE(lck);
251 return 0;
254 /****************************************************************************
255 Close a file.
257 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
258 printing and magic scripts are only run on normal close.
259 delete on close is done on normal and shutdown close.
260 ****************************************************************************/
262 static int close_normal_file(files_struct *fsp, enum file_close_type close_type)
264 connection_struct *conn = fsp->conn;
265 int saved_errno = 0;
266 int err = 0;
267 int err1 = 0;
269 if (fsp->aio_write_behind) {
271 * If we're finishing write behind on a close we can get a write
272 * error here, we must remember this.
274 int ret = wait_for_aio_completion(fsp);
275 if (ret) {
276 saved_errno = ret;
277 err1 = -1;
279 } else {
280 cancel_aio_by_fsp(fsp);
284 * If we're flushing on a close we can get a write
285 * error here, we must remember this.
288 if (close_filestruct(fsp) == -1) {
289 saved_errno = errno;
290 err1 = -1;
293 if (fsp->print_file) {
294 print_fsp_end(fsp, close_type);
295 file_free(fsp);
296 return 0;
299 /* If this is an old DOS or FCB open and we have multiple opens on
300 the same handle we only have one share mode. Ensure we only remove
301 the share mode on the last close. */
303 if (fsp->fh->ref_count == 1) {
304 /* Should we return on error here... ? */
305 close_remove_share_mode(fsp, close_type);
308 if(fsp->oplock_type) {
309 release_file_oplock(fsp);
312 locking_close_file(fsp);
314 err = fd_close(conn, fsp);
316 /* Only save errno if fd_close failed and we don't already
317 have an errno saved from a flush call. */
318 if ((err1 != -1) && (err == -1)) {
319 saved_errno = errno;
322 /* check for magic scripts */
323 if (close_type == NORMAL_CLOSE) {
324 check_magic(fsp,conn);
328 * Ensure pending modtime is set after close.
331 if(fsp->pending_modtime && fsp->pending_modtime_owner) {
332 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
333 } else if (fsp->last_write_time) {
334 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
337 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
338 conn->user,fsp->fsp_name,
339 conn->num_files_open,
340 (err == -1 || err1 == -1) ? strerror(saved_errno) : ""));
342 file_free(fsp);
344 if (err == -1 || err1 == -1) {
345 errno = saved_errno;
346 return saved_errno;
347 } else {
348 return 0;
352 /****************************************************************************
353 Close a directory opened by an NT SMB call.
354 ****************************************************************************/
356 static int close_directory(files_struct *fsp, enum file_close_type close_type)
358 struct share_mode_lock *lck = 0;
359 BOOL delete_dir = False;
362 * NT can set delete_on_close of the last open
363 * reference to a directory also.
366 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
368 if (lck == NULL) {
369 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp->fsp_name));
370 return EINVAL;
373 if (!del_share_mode(lck, fsp)) {
374 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp->fsp_name));
377 delete_dir = (lck->delete_on_close | lck->initial_delete_on_close);
379 if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
380 delete_dir &&
381 lck->delete_token) {
382 BOOL ok;
384 /* Become the user who requested the delete. */
386 if (!push_sec_ctx()) {
387 smb_panic("close_directory: failed to push sec_ctx.\n");
390 set_sec_ctx(lck->delete_token->uid,
391 lck->delete_token->gid,
392 lck->delete_token->ngroups,
393 lck->delete_token->groups,
394 NULL);
396 TALLOC_FREE(lck);
398 ok = rmdir_internals(fsp->conn, fsp->fsp_name);
400 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
401 fsp->fsp_name, ok ? "succeeded" : "failed" ));
403 /* unbecome user. */
404 pop_sec_ctx();
407 * Ensure we remove any change notify requests that would
408 * now fail as the directory has been deleted.
411 if(ok) {
412 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_DELETE_PENDING);
413 remove_pending_change_notify_requests_by_filename(fsp, NT_STATUS_DELETE_PENDING);
416 process_pending_change_notify_queue((time_t)0);
417 } else {
418 TALLOC_FREE(lck);
419 remove_pending_change_notify_requests_by_fid(fsp, NT_STATUS_CANCELLED);
423 * Do the code common to files and directories.
425 close_filestruct(fsp);
426 file_free(fsp);
427 return 0;
430 /****************************************************************************
431 Close a 'stat file' opened internally.
432 ****************************************************************************/
434 static int close_stat(files_struct *fsp)
437 * Do the code common to files and directories.
439 close_filestruct(fsp);
440 file_free(fsp);
441 return 0;
444 /****************************************************************************
445 Close a files_struct.
446 ****************************************************************************/
448 int close_file(files_struct *fsp, enum file_close_type close_type)
450 if(fsp->is_directory)
451 return close_directory(fsp, close_type);
452 else if (fsp->is_stat)
453 return close_stat(fsp);
454 else if (fsp->fake_file_handle != NULL)
455 return close_fake_file(fsp);
456 else
457 return close_normal_file(fsp, close_type);