CUBRID Engine  latest
broker_error.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * broker_error.c - Error code and messages.
22  * This implements functions to manipulate error codes and
23  * messages.
24  */
25 
26 #ident "$Id$"
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 
32 #include "broker_error.h"
33 #include "broker_filename.h"
34 
35 #if defined(WINDOWS) && defined(DISPATCHER)
36 #include "reg_get.h"
37 #endif
38 
39 static int get_error_msg (int err_code, char *msg_buf);
40 
42 static int cur_os_errno = 0;
43 
44 /*
45  * name: uw_set_error_code - set an error code
46  *
47  * arguments: file_name - file name of the error source
48  * line_no - line number of the error source
49  * error_code - error code to set
50  * os_errno - kernel error code
51  *
52  * returns/side-effects:
53  * void
54  *
55  * description: set the error_code to the current error code
56  *
57  * note: UniWeb maintains only one error code for the most recent
58  * error status. That is, this function will ignore the possible
59  * previous error condition.
60  */
61 void
62 uw_set_error_code (const char *file_name, int line_no, int error_code, int os_errno)
63 {
64  cur_error_code = error_code;
65  cur_os_errno = os_errno;
66 }
67 
68 /*
69  * name: uw_get_error_code - get the current error code
70  *
71  * arguments: void
72  *
73  * returns/side-effects:
74  * returns the current error code
75  *
76  * description: returns the current error code
77  *
78  * note: UniWeb maintains only one error code for the most recent
79  * error status. That is, this function will not give any infor
80  * on possible previous error condition.
81  */
82 int
84 {
85  return (cur_error_code);
86 }
87 
88 int
90 {
91  return (cur_os_errno);
92 }
93 
94 /*
95  * name: uw_get_error_message - get the current error message
96  *
97  * arguments: void
98  *
99  * returns/side-effects:
100  * returns the current error message
101  *
102  * description: returns the current error message
103  *
104  * note: UniWeb maintains only one error code for the most recent
105  * error status. That is, this function will not give any infor
106  * on possible previous error condition.
107  */
108 const char *
109 uw_get_error_message (int error_code, int os_error_code)
110 {
111  static char err_msg_buf[2048];
112  char *p;
113  char err_msg[1024];
114 
115  if (error_code > 0 || error_code < UW_MAX_ERROR_CODE)
116  return ("No such error code.");
117  if (os_error_code == 0)
118  {
119  get_error_msg (error_code, err_msg_buf);
120  return (err_msg_buf);
121  }
122  p = strerror (os_error_code);
123  if (p == NULL)
124  {
125  get_error_msg (error_code, err_msg_buf);
126  return (err_msg_buf);
127  }
128  get_error_msg (error_code, err_msg);
129  snprintf (err_msg_buf, sizeof (err_msg_buf) - 1, "%s (OS error code = %d, %s)", err_msg, os_error_code, p);
130  return (err_msg_buf);
131 }
132 
133 #if defined (ENABLE_UNUSED_FUNCTION)
134 const char *
135 uw_error_message (int error_code)
136 {
137  static char err_msg[1024];
138 
139  if (error_code < 0 || error_code > UW_MAX_ERROR_CODE)
140  return ("No such error code.");
141  else
142  get_error_msg (error_code, err_msg);
143  return (err_msg);
144 }
145 
146 void
147 uw_error_message_r (int error_code, char *err_msg)
148 {
149  if (error_code < 0 || error_code > UW_MAX_ERROR_CODE)
150  strcpy (err_msg, "No such error code.");
151  else
152  get_error_msg (error_code, err_msg);
153 }
154 
155 void
156 uw_os_err_msg (int os_err_code, char *err_msg)
157 {
158  if (os_err_code > 0)
159  strcpy (err_msg, strerror (os_err_code));
160  else
161  err_msg[0] = '\0';
162 }
163 #endif /* ENABLE_UNUSED_FUNCTION */
164 
165 static int
166 get_error_msg (int err_code, char *msg_buf)
167 {
168  FILE *fp;
169  char *p;
170  int i;
171  char buf[1024];
172  char default_err_msg_file[BROKER_PATH_MAX];
173 
174  msg_buf[0] = '\0';
175  /* temporarily calculation */
176  err_code = UW_MIN_ERROR_CODE - err_code;
177 
178 #ifdef DISPATCHER
179  strcpy (default_err_msg_file, ERROR_MSG_FILE);
180 #else
181  get_cubrid_file (FID_UV_ERR_MSG, default_err_msg_file, BROKER_PATH_MAX);
182 #endif
183 
184 #if defined(WINDOWS) && defined(DISPATCHER)
185  if (reg_get (REG_ERR_MSG, buf, sizeof (buf)) < 0)
186  return -1;
187  fp = fopen (buf, "r");
188  if (fp == NULL)
189  return -1;
190 #else
191  p = getenv ("UW_ER_MSG");
192  if (p == NULL)
193  {
194  fp = fopen (default_err_msg_file, "r");
195  }
196  else
197  {
198  fp = fopen (p, "r");
199  if (fp == NULL)
200  {
201  fp = fopen (default_err_msg_file, "r");
202  }
203  }
204  if (fp == NULL)
205  {
206  return -1;
207  }
208 #endif
209 
210  for (i = 0; i < err_code; i++)
211  {
212  if (fgets (buf, sizeof (buf), fp) == NULL)
213  {
214  fclose (fp);
215  return -1;
216  }
217  }
218 
219  if (fgets (buf, sizeof (buf), fp) == NULL)
220  {
221  fclose (fp);
222  return -1;
223  }
224 
225  p = strchr (buf, ':');
226  if ((p == NULL) || (atoi (buf) != err_code))
227  {
228  fclose (fp);
229  return -1;
230  }
231  if (*(p + 1) == ' ')
232  p++;
233  strcpy (msg_buf, p + 1);
234  fclose (fp);
235  return 0;
236 }
int uw_get_error_code(void)
Definition: broker_error.c:83
#define BROKER_PATH_MAX
Definition: broker_config.h:91
int uw_get_os_error_code(void)
Definition: broker_error.c:89
void uw_set_error_code(const char *file_name, int line_no, int error_code, int os_errno)
Definition: broker_error.c:62
static int cur_os_errno
Definition: broker_error.c:42
char * get_cubrid_file(T_CUBRID_FILE_ID fid, char *buf, size_t len)
#define UW_MAX_ERROR_CODE
Definition: broker_error.h:85
const char * uw_get_error_message(int error_code, int os_error_code)
Definition: broker_error.c:109
#define NULL
Definition: freelistheap.h:34
#define UW_ER_NO_ERROR
Definition: broker_error.h:31
int i
Definition: dynamic_load.c:954
#define UW_MIN_ERROR_CODE
Definition: broker_error.h:84
static int cur_error_code
Definition: broker_error.c:41
static int get_error_msg(int err_code, char *msg_buf)
Definition: broker_error.c:166
const char ** p
Definition: dynamic_load.c:945