add WITH_SENDFILE profiling data (from Pierre Belanger)
[Samba.git] / source / smbd / error.c
blob6de16f1d5b9599a9bed12dd79f4ad9c293d206de
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 error packet handling
5 Copyright (C) Andrew Tridgell 1992-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 /* these can be set by some functions to override the error codes */
25 int unix_ERR_class=SMB_SUCCESS;
26 int unix_ERR_code=0;
28 /* From lib/error.c */
29 extern struct unix_error_map unix_dos_nt_errmap[];
31 /****************************************************************************
32 Create an error packet from a cached error.
33 ****************************************************************************/
35 int cached_error_packet(char *outbuf,files_struct *fsp,int line,const char *file)
37 write_bmpx_struct *wbmpx = fsp->wbmpx_ptr;
39 int32 eclass = wbmpx->wr_errclass;
40 int32 err = wbmpx->wr_error;
42 /* We can now delete the auxiliary struct */
43 SAFE_FREE(wbmpx);
44 return error_packet(outbuf,NT_STATUS_OK,eclass,err,line,file);
47 /****************************************************************************
48 Create an error packet from errno.
49 ****************************************************************************/
51 int unix_error_packet(char *outbuf,int def_class,uint32 def_code,
52 int line, const char *file)
54 int eclass=def_class;
55 int ecode=def_code;
56 NTSTATUS ntstatus = NT_STATUS_OK;
57 int i=0;
59 if (unix_ERR_class != SMB_SUCCESS) {
60 eclass = unix_ERR_class;
61 ecode = unix_ERR_code;
62 unix_ERR_class = SMB_SUCCESS;
63 unix_ERR_code = 0;
64 } else {
65 while (unix_dos_nt_errmap[i].dos_class != 0) {
66 if (unix_dos_nt_errmap[i].unix_error == errno) {
67 eclass = unix_dos_nt_errmap[i].dos_class;
68 ecode = unix_dos_nt_errmap[i].dos_code;
69 ntstatus = unix_dos_nt_errmap[i].nt_error;
70 break;
72 i++;
76 return error_packet(outbuf,ntstatus,eclass,ecode,line,file);
80 /****************************************************************************
81 Create an error packet. Normally called using the ERROR() macro.
82 ****************************************************************************/
84 int error_packet(char *outbuf,NTSTATUS ntstatus,
85 uint8 eclass,uint32 ecode,int line, const char *file)
87 int outsize = set_message(outbuf,0,0,True);
88 extern uint32 global_client_caps;
90 if (errno != 0)
91 DEBUG(3,("error string = %s\n",strerror(errno)));
94 * We can explicitly force 32 bit error codes even when the
95 * parameter "nt status" is set to no by pre-setting the
96 * FLAGS2_32_BIT_ERROR_CODES bit in the smb_flg2 outbuf.
97 * This is to allow work arounds for client bugs that are needed
98 * when talking with clients that normally expect nt status codes. JRA.
101 if ((lp_nt_status_support() || (SVAL(outbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) && (global_client_caps & CAP_STATUS32)) {
102 if (NT_STATUS_V(ntstatus) == 0 && eclass)
103 ntstatus = dos_to_ntstatus(eclass, ecode);
104 SIVAL(outbuf,smb_rcls,NT_STATUS_V(ntstatus));
105 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)|FLAGS2_32_BIT_ERROR_CODES);
106 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) %s\n",
107 file, line,
108 (int)CVAL(outbuf,smb_com),
109 smb_fn_name(CVAL(outbuf,smb_com)),
110 get_nt_error_msg(ntstatus)));
111 return outsize;
114 if (eclass == 0 && NT_STATUS_V(ntstatus))
115 ntstatus_to_dos(ntstatus, &eclass, &ecode);
117 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2)&~FLAGS2_32_BIT_ERROR_CODES);
118 SSVAL(outbuf,smb_rcls,eclass);
119 SSVAL(outbuf,smb_err,ecode);
121 DEBUG(3,("error packet at %s(%d) cmd=%d (%s) eclass=%d ecode=%d\n",
122 file, line,
123 (int)CVAL(outbuf,smb_com),
124 smb_fn_name(CVAL(outbuf,smb_com)),
125 eclass,
126 ecode));
128 return outsize;