2 Unix SMB/CIFS implementation.
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.
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
)))
34 DEBUG(5,("checking magic for %s\n",fsp
->fsp_name
));
38 if (!(p
= strrchr_m(fsp
->fsp_name
,'/')))
43 if (!strequal(lp_magicscript(SNUM(conn
)),p
))
54 pstrcpy(fname
,fsp
->fsp_name
);
55 if (*lp_magicoutput(SNUM(conn
)))
56 pstrcpy(magic_output
,lp_magicoutput(SNUM(conn
)));
58 slprintf(magic_output
,sizeof(fname
)-1, "%s.out",fname
);
61 ret
= smbrun(fname
,&tmp_fd
);
62 DEBUG(3,("Invoking magic command %s gave %d\n",fname
,ret
));
64 if (ret
!= 0 || tmp_fd
== -1) {
69 outfd
= open(magic_output
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
75 if (sys_fstat(tmp_fd
,&st
) == -1) {
81 transfer_file(tmp_fd
,outfd
,(SMB_OFF_T
)st
.st_size
);
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
;
96 if (fsp
->fh
->fd
!= -1) {
97 if(flush_write_cache(fsp
, CLOSE_FLUSH
) == -1)
100 delete_write_cache(fsp
);
103 conn
->num_files_open
--;
104 SAFE_FREE(fsp
->wbmpx_ptr
);
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
)
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
)) {
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
131 schedule_deferred_open_smb_message(e
->op_mid
);
133 char msg
[MSG_SMB_SHARE_MODE_ENTRY_SIZE
];
135 share_mode_entry_to_message(msg
, e
);
138 message_send_pid(e
->pid
, MSG_SMB_OPEN_RETRY
,
139 msg
, MSG_SMB_SHARE_MODE_ENTRY_SIZE
, True
);
145 /****************************************************************************
146 Deal with removing a share mode on last close.
147 ****************************************************************************/
149 static int close_remove_share_mode(files_struct
*fsp
, enum file_close_type close_type
)
151 connection_struct
*conn
= fsp
->conn
;
152 BOOL delete_file
= False
;
153 struct share_mode_lock
*lck
;
156 * Lock the share entries, and determine if we should delete
157 * on close. If so delete whilst the lock is still in effect.
158 * This prevents race conditions with the file being created. JRA.
161 lck
= get_share_mode_lock(NULL
, fsp
->dev
, fsp
->inode
, NULL
, NULL
);
164 DEBUG(0, ("close_remove_share_mode: Could not get share mode lock for file %s\n", fsp
->fsp_name
));
168 if (!del_share_mode(lck
, fsp
)) {
169 DEBUG(0, ("close_remove_share_mode: Could not delete share entry for file %s\n", fsp
->fsp_name
));
172 delete_file
= (lck
->delete_on_close
| lck
->initial_delete_on_close
);
176 /* See if others still have the file open. If this is the
177 * case, then don't delete */
178 for (i
=0; i
<lck
->num_share_modes
; i
++) {
179 if (is_valid_share_mode_entry(&lck
->share_modes
[i
])) {
186 /* Notify any deferred opens waiting on this close. */
187 notify_deferred_opens(lck
);
188 reply_to_oplock_break_requests(fsp
);
191 * NT can set delete_on_close of the last open
192 * reference to a file.
195 if ((close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) &&
198 SMB_STRUCT_STAT sbuf
;
200 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set - deleting file.\n",
203 /* Become the user who requested the delete. */
205 if (!push_sec_ctx()) {
206 smb_panic("close_remove_share_mode: file %s. failed to push sec_ctx.\n");
209 set_sec_ctx(lck
->delete_token
->uid
,
210 lck
->delete_token
->gid
,
211 lck
->delete_token
->ngroups
,
212 lck
->delete_token
->groups
,
215 /* We can only delete the file if the name we have
216 is still valid and hasn't been renamed. */
218 if(SMB_VFS_STAT(conn
,fsp
->fsp_name
,&sbuf
) != 0) {
219 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
220 "and stat failed with error %s\n",
221 fsp
->fsp_name
, strerror(errno
) ));
223 if(sbuf
.st_dev
!= fsp
->dev
|| sbuf
.st_ino
!= fsp
->inode
) {
224 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set and "
225 "dev and/or inode does not match\n",
227 DEBUG(5,("close_remove_share_mode: file %s. stored dev = %x, inode = %.0f "
228 "stat dev = %x, inode = %.0f\n",
230 (unsigned int)fsp
->dev
, (double)fsp
->inode
,
231 (unsigned int)sbuf
.st_dev
, (double)sbuf
.st_ino
));
233 } else if(SMB_VFS_UNLINK(conn
,fsp
->fsp_name
) != 0) {
235 * This call can potentially fail as another smbd may have
236 * had the file open with delete on close set and deleted
237 * it when its last reference to this file went away. Hence
238 * we log this but not at debug level zero.
241 DEBUG(5,("close_remove_share_mode: file %s. Delete on close was set "
242 "and unlink failed with error %s\n",
243 fsp
->fsp_name
, strerror(errno
) ));
249 process_pending_change_notify_queue((time_t)0);
256 /****************************************************************************
259 close_type can be NORMAL_CLOSE=0,SHUTDOWN_CLOSE,ERROR_CLOSE.
260 printing and magic scripts are only run on normal close.
261 delete on close is done on normal and shutdown close.
262 ****************************************************************************/
264 static int close_normal_file(files_struct
*fsp
, enum file_close_type close_type
)
266 connection_struct
*conn
= fsp
->conn
;
271 remove_pending_lock_requests_by_fid(fsp
);
273 if (fsp
->aio_write_behind
) {
275 * If we're finishing write behind on a close we can get a write
276 * error here, we must remember this.
278 int ret
= wait_for_aio_completion(fsp
);
284 cancel_aio_by_fsp(fsp
);
288 * If we're flushing on a close we can get a write
289 * error here, we must remember this.
292 if (close_filestruct(fsp
) == -1) {
297 if (fsp
->print_file
) {
298 print_fsp_end(fsp
, close_type
);
303 /* If this is an old DOS or FCB open and we have multiple opens on
304 the same handle we only have one share mode. Ensure we only remove
305 the share mode on the last close. */
307 if (fsp
->fh
->ref_count
== 1) {
308 /* Should we return on error here... ? */
309 close_remove_share_mode(fsp
, close_type
);
312 if(fsp
->oplock_type
) {
313 release_file_oplock(fsp
);
316 locking_close_file(fsp
);
318 err
= fd_close(conn
, fsp
);
320 /* Only save errno if fd_close failed and we don't already
321 have an errno saved from a flush call. */
322 if ((err1
!= -1) && (err
== -1)) {
326 /* check for magic scripts */
327 if (close_type
== NORMAL_CLOSE
) {
328 check_magic(fsp
,conn
);
332 * Ensure pending modtime is set after close.
335 if(fsp
->pending_modtime
&& fsp
->pending_modtime_owner
) {
336 set_filetime(conn
, fsp
->fsp_name
, fsp
->pending_modtime
);
337 } else if (fsp
->last_write_time
) {
338 set_filetime(conn
, fsp
->fsp_name
, fsp
->last_write_time
);
341 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
342 conn
->user
,fsp
->fsp_name
,
343 conn
->num_files_open
,
344 (err
== -1 || err1
== -1) ? strerror(saved_errno
) : ""));
348 if (err
== -1 || err1
== -1) {
356 /****************************************************************************
357 Close a directory opened by an NT SMB call.
358 ****************************************************************************/
360 static int close_directory(files_struct
*fsp
, enum file_close_type close_type
)
362 struct share_mode_lock
*lck
= 0;
363 BOOL delete_dir
= False
;
366 * NT can set delete_on_close of the last open
367 * reference to a directory also.
370 lck
= get_share_mode_lock(NULL
, fsp
->dev
, fsp
->inode
, NULL
, NULL
);
373 DEBUG(0, ("close_directory: Could not get share mode lock for %s\n", fsp
->fsp_name
));
377 if (!del_share_mode(lck
, fsp
)) {
378 DEBUG(0, ("close_directory: Could not delete share entry for %s\n", fsp
->fsp_name
));
381 delete_dir
= (lck
->delete_on_close
| lck
->initial_delete_on_close
);
383 if ((close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) &&
388 /* Become the user who requested the delete. */
390 if (!push_sec_ctx()) {
391 smb_panic("close_directory: failed to push sec_ctx.\n");
394 set_sec_ctx(lck
->delete_token
->uid
,
395 lck
->delete_token
->gid
,
396 lck
->delete_token
->ngroups
,
397 lck
->delete_token
->groups
,
402 ok
= rmdir_internals(fsp
->conn
, fsp
->fsp_name
);
404 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
405 fsp
->fsp_name
, ok
? "succeeded" : "failed" ));
411 * Ensure we remove any change notify requests that would
412 * now fail as the directory has been deleted.
416 remove_pending_change_notify_requests_by_fid(fsp
, NT_STATUS_DELETE_PENDING
);
417 remove_pending_change_notify_requests_by_filename(fsp
, NT_STATUS_DELETE_PENDING
);
420 process_pending_change_notify_queue((time_t)0);
423 remove_pending_change_notify_requests_by_fid(fsp
, NT_STATUS_CANCELLED
);
427 * Do the code common to files and directories.
429 close_filestruct(fsp
);
434 /****************************************************************************
435 Close a 'stat file' opened internally.
436 ****************************************************************************/
438 static int close_stat(files_struct
*fsp
)
441 * Do the code common to files and directories.
443 close_filestruct(fsp
);
448 /****************************************************************************
449 Close a files_struct.
450 ****************************************************************************/
452 int close_file(files_struct
*fsp
, enum file_close_type close_type
)
454 if(fsp
->is_directory
)
455 return close_directory(fsp
, close_type
);
456 else if (fsp
->is_stat
)
457 return close_stat(fsp
);
458 else if (fsp
->fake_file_handle
!= NULL
)
459 return close_fake_file(fsp
);
461 return close_normal_file(fsp
, close_type
);