Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / base / FBase_NativeError.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FBaseNativeError.cpp
19  * @brief               This is the implementation file of functions for native error handling.
20  */
21 #include <string.h>
22 #include <errno.h>
23 #include <system_info.h>
24 #include <power.h>
25 #include <alarm.h>
26 #include <FBaseSysLog.h>
27 #include "FBase_NativeError.h"
28 #include "device.h"
29 #include "sqlite3.h"
30
31 using namespace Tizen::Base;
32
33 static int nativeErrorToResultTable[] =
34 {
35 E_SUCCESS,                  // SUCCESS           0      /* Success, No error */
36 E_INVALID_OPERATION,        // EPERM             1      /* Operation not permitted */
37 E_FILE_NOT_FOUND,           // ENOENT            2      /* No such file or directory */
38 E_UNKNOWN,                  // ESRCH             3      /* No such process */
39 E_INTERRUPTED,              // EINTR             4      /* Interrupted system call */
40 E_IO,                       // EIO                   5  /* I/O error */
41 E_OBJ_NOT_FOUND,            // ENXIO             6      /* No such device or address */
42 E_OVERFLOW,                 // E2BIG             7      /* Argument list too long */
43 E_UNKNOWN,                  // ENOEXEC           8      /* Exec format error */
44 E_FILE_NOT_FOUND,           // EBADF             9      /* Bad file number */
45 E_UNKNOWN,                  // ECHILD           10      /* No child processes */
46 E_UNKNOWN,                  // EAGAIN           11      /* Try again */
47 E_OUT_OF_MEMORY,            // ENOMEM           12      /* Out of memory */
48 E_ILLEGAL_ACCESS,           // EACCES           13      /* Permission denied */
49 E_UNKNOWN,                  // EFAULT           14      /* Bad address */
50 E_UNKNOWN,                  // ENOTBLK          15      /* Block device required */
51 E_DEVICE_BUSY,              // EBUSY            16      /* Device or resource busy */
52 E_FILE_ALREADY_EXIST,       // EEXIST           17      /* File exists */
53 E_UNKNOWN,                  // EXDEV            18      /* Cross-device link */
54 E_IO,                       // ENODEV           19      /* No such device */
55 E_INVALID_ARG,              // ENOTDIR          20      /* Not a directory */
56 E_INVALID_ARG,              // EISDIR           21      /* Is a directory */
57 E_INVALID_ARG,              // EINVAL           22      /* Invalid argument */
58 E_MAX_EXCEEDED,             // ENFILE           23      /* File table overflow */
59 E_MAX_EXCEEDED,             // EMFILE           24      /* Too many open files */
60 E_UNKNOWN,                  // ENOTTY           25      /* Not a typewriter */
61 E_UNKNOWN,                  // ETXTBSY          26      /* Text file busy */
62 E_IO,                       // EFBIG            27      /* File too large */
63 E_STORAGE_FULL,             // ENOSPC           28      /* No space left on device */
64 E_ILLEGAL_ACCESS,           // ESPIPE           29      /* Illegal seek */
65 E_ILLEGAL_ACCESS,           // EROFS            30      /* Read-only file system */
66 E_UNKNOWN,                  // EMLINK           31      /* Too many links */
67 E_UNKNOWN,                  // EPIPE            32      /* Broken pipe */
68 E_INVALID_DOMAIN,           // EDOM                     33      /* Math argument out of domain of func */
69 E_OUT_OF_RANGE,             // ERANGE           34      /* Math result not representable ;
70 E_UNKNOWN,                  // EDEADLK          35      /* Resource deadlock would occur */
71 E_INVALID_ARG,              // ENAMETOOLONG     36      /* File name too long */
72 E_UNKNOWN,                  // ENOLCK           37      /* No record locks available */
73 E_UNKNOWN,                  // ENOSYS           38      /* Function not implemented */
74 E_UNKNOWN,                  // ENOTEMPTY        39      /* Directory not empty */
75 E_INVALID_ARG,              // ELOOP            40      /* Too many symbolic links encountered */
76 E_WOULD_BLOCK,              // EWOULDBLOCK      EAGAIN  /* Operation would block */
77 E_UNKNOWN,                  // ENOMSG           42      /* No message of desired type */
78 E_UNKNOWN,                  // EIDRM            43      /* Identifier removed */
79 E_UNKNOWN,                  // ECHRNG           44      /* Channel number out of range */
80 E_UNKNOWN,                  // EL2NSYNC         45      /* Level 2 not synchronized */
81 E_UNKNOWN,                  // EL3HLT           46      /* Level 3 halted */
82 E_UNKNOWN,                  // EL3RST           47      /* Level 3 reset */
83 E_UNKNOWN,                  // ELNRNG           48      /* Link number out of range */
84 E_UNKNOWN,                  // EUNATCH          49      /* Protocol driver not attached */
85 E_UNKNOWN,                  // ENOCSI           50      /* No CSI structure available */
86 E_UNKNOWN,                  // EL2HLT           51      /* Level 2 halted */
87 E_UNKNOWN,                  // EBADE            52      /* Invalid exchange */
88 E_UNKNOWN,                  // EBADR            53      /* Invalid request descriptor */
89 E_UNKNOWN,                  // EXFULL           54      /* Exchange full */
90 E_UNKNOWN,                  // ENOANO           55      /* No anode */
91 E_UNKNOWN,                  // EBADRQC          56      /* Invalid request code */
92 E_UNKNOWN,                  // EBADSLT          57      /* Invalid slot */
93
94 E_UNKNOWN,                  // EDEADLOCK        EDEADLK
95
96 E_UNKNOWN,                  // EBFONT           59      /* Bad font file format */
97 E_UNKNOWN,                  // ENOSTR           60      /* Device not a stream */
98 E_UNKNOWN,                  // ENODATA          61      /* No data available */
99 E_UNKNOWN,                  // ETIME            62      /* Timer expired */
100 E_UNKNOWN,                  // ENOSR            63      /* Out of streams resources */
101 E_UNKNOWN,                  // ENONET           64      /* Machine is not on the network */
102 E_UNKNOWN,                  // ENOPKG           65      /* Package not installed */
103 E_UNKNOWN,                  // EREMOTE          66      /* Object is remote */
104 E_UNKNOWN,                  // ENOLINK          67      /* Link has been severed */
105 E_UNKNOWN,                  // EADV                     68      /* Advertise error */
106 E_UNKNOWN,                  // ESRMNT           69      /* Srmount error */
107 E_UNKNOWN,                  // ECOMM            70      /* Communication error on send */
108 E_UNKNOWN,                  // EPROTO           71      /* Protocol error */
109 E_UNKNOWN,                  // EMULTIHOP        72      /* Multihop attempted */
110 E_UNKNOWN,                  // EDOTDOT          73      /* RFS specific error */
111 E_UNKNOWN,                  // EBADMSG          74      /* Not a data message */
112 E_IO,                       // EOVERFLOW        75      /* Value too large for defined data type */
113 E_UNKNOWN,                  // ENOTUNIQ         76      /* Name not unique on network */
114 E_UNKNOWN,                  // EBADFD           77      /* File descriptor in bad state */
115 E_UNKNOWN,                  // EREMCHG          78      /* Remote address changed */
116 E_UNKNOWN,                  // ELIBACC          79      /* Can not access a needed shared library */
117 E_UNKNOWN,                  // ELIBBAD          80      /* Accessing a corrupted shared library */
118 E_UNKNOWN,                  // ELIBSCN          81      /* .lib section in a.out corrupted */
119 E_UNKNOWN,                  // ELIBMAX          82      /* Attempting to link in too many shared libraries */
120 E_UNKNOWN,                  // ELIBEXEC         83      /* Cannot exec a shared library directly */
121 E_INVALID_ENCODING_RANGE,       // EILSEQ               84      /* Illegal byte sequence */
122 E_UNKNOWN,                  // ERESTART         85      /* Interrupted system call should be restarted */
123 E_UNKNOWN,                  // ESTRPIPE         86      /* Streams pipe error */
124 E_UNKNOWN,                  // EUSERS           87      /* Too many users */
125 E_UNKNOWN,                  // ENOTSOCK         88      /* Socket operation on non-socket */
126 E_UNKNOWN,                  // EDESTADDRREQ     89      /* Destination address required */
127 E_UNKNOWN,                  // EMSGSIZE         90      /* Message too long */
128 E_UNKNOWN,                  // EPROTOTYPE       91      /* Protocol wrong type for socket */
129 E_UNKNOWN,                  // ENOPROTOOPT      92      /* Protocol not available */
130 E_UNSUPPORTED_PROTOCOL,     // EPROTONOSUPPORT  93      /* Protocol not supported */
131 E_UNKNOWN,                  // ESOCKTNOSUPPORT  94      /* Socket type not supported */
132 E_UNSUPPORTED_OPERATION,    // EOPNOTSUPP       95      /* Operation not supported on transport endpoint */
133 E_UNKNOWN,                  // EPFNOSUPPORT     96      /* Protocol family not supported */
134 E_UNKNOWN,                  // EAFNOSUPPORT     97      /* Address family not supported by protocol */
135 E_UNKNOWN,                  // EADDRINUSE       98      /* Address already in use */
136 E_UNKNOWN,                  // EADDRNOTAVAIL    99      /* Cannot assign requested address */
137 E_UNKNOWN,                  // ENETDOWN         100     /* Network is down */
138 E_UNKNOWN,                  // ENETUNREACH      101     /* Network is unreachable */
139 E_UNKNOWN,                  // ENETRESET        102     /* Network dropped connection because of reset */
140 E_UNKNOWN,                  // ECONNABORTED     103     /* Software caused connection abort */
141 E_UNKNOWN,                  // ECONNRESET       104     /* Connection reset by peer */
142 E_UNKNOWN,                  // ENOBUFS          105     /* No buffer space available */
143 E_UNKNOWN,                  // EISCONN          106     /* Transport endpoint is already connected */
144 E_UNKNOWN,                  // ENOTCONN         107     /* Transport endpoint is not connected */
145 E_UNKNOWN,                  // ESHUTDOWN        108     /* Cannot send after transport endpoint shutdown */
146 E_UNKNOWN,                  // ETOOMANYREFS     109     /* Too many references: cannot splice */
147 E_UNKNOWN,                  // ETIMEDOUT        110     /* Connection timed out */
148 E_UNKNOWN,                  // ECONNREFUSED     111     /* Connection refused */
149 E_UNKNOWN,                  // EHOSTDOWN        112     /* Host is down */
150 E_HOST_UNREACHABLE,         // EHOSTUNREACH     113     /* No route to host */
151 E_UNKNOWN,                  // EALREADY         114     /* Operation already in progress */
152 E_UNKNOWN,                  // EINPROGRESS      115     /* Operation now in progress */
153 E_UNKNOWN,                  // ESTALE           116     /* Stale NFS file handle */
154 E_UNKNOWN,                  // EUCLEAN          117     /* Structure needs cleaning */
155 E_UNKNOWN,                  // ENOTNAM          118     /* Not a XENIX named type file */
156 E_UNKNOWN,                  // ENAVAIL          119     /* No XENIX semaphores available */
157 E_UNKNOWN,                  // EISNAM           120     /* Is a named type file */
158 E_UNKNOWN,                  // EREMOTEIO        121     /* Remote I/O error */
159 E_UNKNOWN,                  // EDQUOT           122     /* Quota exceeded */
160
161 E_UNKNOWN,                  // ENOMEDIUM        123     /* No medium found */
162 E_UNKNOWN,                  // EMEDIUMTYPE      124     /* Wrong medium type */
163 E_UNKNOWN,                  // ECANCELED        125     /* Operation Canceled */
164 E_UNKNOWN,                  // ENOKEY           126     /* Required key not available */
165 E_UNKNOWN,                  // EKEYEXPIRED      127     /* Key has expired */
166 E_UNKNOWN,                  // EKEYREVOKED      128     /* Key has been revoked */
167 E_UNKNOWN,                  // EKEYREJECTED     129     /* Key was rejected by service */
168
169 /* for robust mutexes */
170 E_UNKNOWN,                  // EOWNERDEAD       130     /* Owner died */
171 E_UNKNOWN,                  // ENOTRECOVERABLE  131     /* State not recoverable */
172 };
173
174 static int sqlErrorToResultTable[] =
175 {
176         E_SUCCESS,              // SQLITE_OK           0   /* Successful result */
177         E_DATABASE,             // SQLITE_ERROR        1   /* SQL error or missing database */
178         E_DATABASE,             // SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
179         E_DATABASE,             // SQLITE_PERM         3   /* Access permission denied */
180         E_DATABASE,             // SQLITE_ABORT        4   /* Callback routine requested an abort */
181         E_SERVICE_BUSY,         // SQLITE_BUSY         5   /* The database file is locked */
182         E_DATABASE,             // SQLITE_LOCKED       6   /* A table in the database is locked */
183         E_DATABASE,             // SQLITE_NOMEM        7   /* A malloc() failed */
184         E_DATABASE,             // SQLITE_READONLY     8   /* Attempt to write a readonly database */
185         E_DATABASE,             // SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
186         E_DATABASE,             // SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
187         E_DATABASE,             // SQLITE_CORRUPT     11   /* The database disk image is malformed */
188         E_DATABASE,             // SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
189         E_DATABASE,             // SQLITE_FULL        13   /* Insertion failed because database is full */
190         E_FILE_NOT_FOUND,       // SQLITE_CANTOPEN    14   /* Unable to open the database file */
191         E_DATABASE,             // SQLITE_PROTOCOL    15   /* Database lock protocol error */
192         E_DATABASE,             // SQLITE_EMPTY       16   /* Database is empty */
193         E_DATABASE,             // SQLITE_SCHEMA      17   /* The database schema changed */
194         E_INVALID_ARG,          // SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
195         E_DATABASE,             // SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
196         E_DATABASE,             // SQLITE_MISMATCH    20   /* Data type mismatch */
197         E_DATABASE,             // SQLITE_MISUSE      21   /* Library used incorrectly */
198         E_DATABASE,             // SQLITE_NOLFS       22   /* Uses OS features not supported on host */
199         E_DATABASE,             // SQLITE_AUTH        23   /* Authorization denied */
200         E_DATABASE,             // SQLITE_FORMAT      24   /* Auxiliary database format error */
201         E_INVALID_ARG,          // SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
202         E_DATABASE,             // SQLITE_NOTADB      26   /* File opened that is not a database file */
203 };
204
205 static int sqlErrorToDetailResultTable[] =
206 {
207         E_SUCCESS,              // SQLITE_OK           0   /* Successful result */
208         E_INVALID_ARG,          // SQLITE_ERROR        1   /* SQL error or missing database */
209         E_SYSTEM,               // SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
210         E_ILLEGAL_ACCESS,       // SQLITE_PERM         3   /* Access permission denied */
211         E_UNKNOWN,              // SQLITE_ABORT        4   /* Callback routine requested an abort */
212         E_OBJECT_LOCKED,        // SQLITE_BUSY         5   /* The database file is locked */
213         E_OBJECT_LOCKED,        // SQLITE_LOCKED       6   /* A table in the database is locked */
214         E_OUT_OF_MEMORY,        // SQLITE_NOMEM        7   /* A malloc() failed */
215         E_ILLEGAL_ACCESS,       // SQLITE_READONLY     8   /* Attempt to write a readonly database */
216         E_UNKNOWN,              // SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
217         E_IO,                   // SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
218         E_INVALID_FORMAT,       // SQLITE_CORRUPT     11   /* The database disk image is malformed */
219         E_SYSTEM,               // SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
220         E_STORAGE_FULL,         // SQLITE_FULL        13   /* Insertion failed because database is full */
221         E_FILE_NOT_FOUND,       // SQLITE_CANTOPEN    14   /* Unable to open the database file */
222         E_UNKNOWN,              // SQLITE_PROTOCOL    15   /* Database lock protocol error */
223         E_UNKNOWN,              // SQLITE_EMPTY       16   /* Database is empty */
224         E_INVALID_ARG,          // SQLITE_SCHEMA      17   /* The database schema changed */
225         E_INVALID_ARG,          // SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
226         E_INVALID_ARG,          // SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
227         E_INVALID_ARG,          // SQLITE_MISMATCH    20   /* Data type mismatch */
228         E_INVALID_OPERATION,    // SQLITE_MISUSE      21   /* Library used incorrectly */
229         E_SYSTEM,               // SQLITE_NOLFS       22   /* Uses OS features not supported on host */
230         E_ILLEGAL_ACCESS,       // SQLITE_AUTH        23   /* Authorization denied */
231         E_INVALID_FORMAT,       // SQLITE_FORMAT      24   /* Auxiliary database format error */
232         E_INVALID_ARG,          // SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
233         E_INVALID_FORMAT,       // SQLITE_NOTADB      26   /* File opened that is not a database file */
234 };
235
236 result
237 _NativeError::ConvertNativeErrorToResult(int errNo, const char* pFile, int line, const char* pFunction, bool logNativeErr)
238 {
239         result r = E_UNKNOWN;
240
241         if ((errNo >= 0) && (errNo < (int) (sizeof(nativeErrorToResultTable) / sizeof(nativeErrorToResultTable[0]))))
242         {
243                 r = nativeErrorToResultTable[errNo];
244         }
245
246         if (logNativeErr)
247         {
248                 SysLog(NID_BASE, "A native error (%d) occurred at (%s:%d).", errNo, pFile, line);
249         }
250
251         if (r == E_UNKNOWN)
252         {
253                 SysLog(NID_BASE, "[E_SYSTEM] Failed to convert native error (%d, %s) to result.", errNo, strerror(errNo));
254                 return E_SYSTEM;
255         }
256
257         return r;
258 }
259
260 result
261 _NativeError::ConvertNativeErrorToResult(int errNo, bool logNativeErr)
262 {
263         result r = E_UNKNOWN;
264
265         if ((errNo >= 0) && (errNo < (int) (sizeof(nativeErrorToResultTable) / sizeof(nativeErrorToResultTable[0]))))
266         {
267                 r = nativeErrorToResultTable[errNo];
268         }
269
270         if (logNativeErr)
271         {
272                 SysLog(NID_BASE, "A native error (%d) occurred.", errNo);
273         }
274
275         if (r == E_UNKNOWN)
276         {
277                 SysLog(NID_BASE, "[E_SYSTEM] Failed to convert native error (%d, %s) to result.", errNo, strerror(errNo));
278                 return E_SYSTEM;
279         }
280
281         return r;
282 }
283
284 const char*
285 _NativeError::ConvertNativeErrorToMessage(int errNo, const char* pFile, int line, const char* pFunction)
286 {
287         return GetErrorMessage(ConvertNativeErrorToResult(errNo, pFile, line, pFunction));
288 }
289
290 const char*
291 _NativeError::ConvertNativeErrorToMessage(int errNo)
292 {
293         return GetErrorMessage(__ConvertNativeErrorToResult(errNo));
294 }
295
296 result
297 _NativeError::ConvertNativeSystemErrorToResult(int err)
298 {
299         switch (err)
300         {
301         case SYSTEM_INFO_ERROR_INVALID_PARAMETER:
302                 return E_INVALID_ARG;
303         }
304
305         return E_UNKNOWN;
306 }
307
308 result
309 _NativeError::ConvertNativeDeviceErrorToResult(int err)
310 {
311         switch (err)
312         {
313         case DEVICE_ERROR_INVALID_PARAMETER:
314                 return E_INVALID_ARG;
315         }
316
317         return E_UNKNOWN;
318 }
319
320 result
321 _NativeError::ConvertNativePowerErrorToResult(int err)
322 {
323         switch (err)
324         {
325         case POWER_ERROR_INVALID_PARAMETER:
326                 return E_INVALID_ARG;
327         }
328
329         return E_UNKNOWN;
330 }
331
332 result
333 _NativeError::ConvertNativeAlarmErrorToResult(int err)
334 {
335         switch (err)
336         {
337         case ERR_ALARM_INVALID_PARAM:
338                 return E_INVALID_ARG;
339
340         case ERR_ALARM_INVALID_ID:
341                 return E_INVALID_ARG;
342
343         case ERR_ALARM_INVALID_REPEAT:
344                 return E_INVALID_ARG;
345
346         case ERR_ALARM_INVALID_TIME:
347                 return E_INVALID_ARG;
348
349         case ERR_ALARM_INVALID_DATE:
350                 return E_INVALID_ARG;
351
352         case ERR_ALARM_NO_SERVICE_NAME:
353                 return E_INVALID_ARG;
354
355         case ERR_ALARM_SYSTEM_FAIL:
356                 return E_SYSTEM;
357         }
358
359         return E_UNKNOWN;
360 }
361
362 result
363 _NativeError::ConvertNativeHapticErrorToResult(int err)
364 {
365         return E_UNKNOWN;
366 }
367
368 result
369 _NativeError::ConvertNativeTelephonyErrorToResult(int err)
370 {
371         return E_UNKNOWN;
372 }
373
374 result
375 _NativeError::ConvertNativeSoundManagerErrorToResult(int err)
376 {
377         return E_UNKNOWN;
378 }
379
380 result
381 _NativeError::ConvertNativeSqliteErrorToResult(int err)
382 {
383         result r = E_UNKNOWN;
384
385         if ((err >= 0) && (err < (int) (sizeof(sqlErrorToResultTable) / sizeof(sqlErrorToResultTable[0]))))
386         {
387                 r = sqlErrorToResultTable[err];
388         }
389
390         if (r == E_UNKNOWN)
391         {
392                 SysLog(NID_BASE, "[E_SYSTEM] Failed to convert native error (%d) to result.", err);
393                 return E_SYSTEM;
394         }
395
396         return r;
397 }
398
399 result
400 _NativeError::ConvertNativeSqliteErrorToDetailResult(int err)
401 {
402         result r = E_UNKNOWN;
403
404         if ((err >= 0) && (err < (int) (sizeof(sqlErrorToDetailResultTable) / sizeof(sqlErrorToDetailResultTable[0]))))
405         {
406                 r = sqlErrorToDetailResultTable[err];
407         }
408
409         if (r == E_UNKNOWN)
410         {
411                 SysLog(NID_BASE, "[E_SYSTEM] Failed to convert native error (%d) to result.", err);
412                 return E_SYSTEM;
413         }
414
415         return r;
416 }