source: roaraudio/libroar/error.c @ 4912:25498423dcc9

Last change on this file since 4912:25498423dcc9 was 4912:25498423dcc9, checked in by phi, 13 years ago

added some new error codes

File size: 14.6 KB
Line 
1//error.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38// 'no error' value for errno.
39// zero is true for GNU/Linux.
40// don't know about other systems.
41#define CLEAN_ERRNO 0
42
43int roar_errno = ROAR_ERROR_NONE;
44
45int    roar_err_int(struct roar_error_frame * frame) {
46 if ( frame == NULL )
47  return -1;
48
49 memset(frame, 0, sizeof(struct roar_error_frame));
50
51 frame->cmd         = -1;
52 frame->ra_errno    = -1;
53 frame->ra_suberrno = -1;
54 frame->p_errno     = -1;
55
56 return 0;
57}
58
59void * roar_err_buildmsg(struct roar_message * mes, struct roar_error_frame * frame) {
60 int16_t * d;
61
62 if ( mes == NULL || frame == NULL )
63  return NULL;
64
65 memset(mes,  0, sizeof(struct roar_message));
66
67 d = (int16_t*)mes->data;
68
69 mes->datalen = 8 + frame->datalen;
70 frame->data  = &(mes->data[8]);
71
72 mes->data[0]    = 0; // version.
73 mes->data[1]    = frame->cmd;
74 mes->data[2]    = frame->ra_errno;
75 mes->data[3]    = frame->ra_suberrno;
76 d[2]            = ROAR_HOST2NET16(frame->p_errno);
77 d[3]            = ROAR_HOST2NET16(frame->flags);
78
79 return frame->data;
80}
81
82int    roar_err_parsemsg(struct roar_message * mes, struct roar_error_frame * frame) {
83 int16_t * d;
84
85 if ( mes == NULL || frame == NULL )
86  return -1;
87
88 d = (int16_t*)mes->data;
89
90 if ( mes->datalen < 8 )
91  return -1;
92
93 if ( mes->data[0] != 0 )
94  return -1;
95
96 frame->cmd         = mes->data[1];
97 frame->ra_errno    = mes->data[2];
98 frame->ra_suberrno = mes->data[3];
99 frame->p_errno     = ROAR_NET2HOST16(d[2]);
100 frame->flags       = ROAR_NET2HOST16(d[3]);
101
102 frame->datalen     = mes->datalen - 8;
103 frame->data        = &(mes->data[8]);
104
105 return 0;
106}
107
108int *  roar_errno2(void) {
109 return &roar_errno;
110}
111
112void   roar_err_clear(void) {
113 *roar_errno2() = ROAR_ERROR_NONE;
114}
115
116void   roar_err_clear_errno(void) {
117 errno = CLEAN_ERRNO;
118}
119
120void   roar_err_clear_all(void) {
121 roar_err_clear();
122 roar_err_clear_errno();
123}
124
125void   roar_err_update(void) {
126 int * err = roar_errno2();
127
128 // NOTE: _NEVER_ call ROAR_{DBG,INFO,WARN,ERRO}() in here! (will result in endless loop)
129 //printf("*err=%i, errno=%i\n", *err, (int)errno);
130
131 if ( *err != ROAR_ERROR_NONE ) {
132  roar_err_to_errno();
133 } else if ( !roar_err_is_errno_clean() ) {
134  roar_err_from_errno();
135 }
136}
137
138int    roar_err_is_errno_clean(void) {
139 return errno == CLEAN_ERRNO ? 1 : 0;
140}
141
142void   roar_err_set(const int error) {
143 *roar_errno2() = error;
144}
145
146void   roar_err_from_errno(void) {
147 int _roar_errno = ROAR_ERROR_NONE;
148
149 switch (errno) {
150#ifdef EACCES
151  case EACCES:       _roar_errno = ROAR_ERROR_PERM; break;
152#endif
153#ifdef EPERM
154  case EPERM:        _roar_errno = ROAR_ERROR_PERM; break;
155#endif
156#ifdef ENOENT
157  case ENOENT:       _roar_errno = ROAR_ERROR_NOENT; break;
158#endif
159#ifdef EBADMSG
160  case EBADMSG:      _roar_errno = ROAR_ERROR_BADMSG; break;
161#endif
162#ifdef EBUSY
163  case EBUSY:        _roar_errno = ROAR_ERROR_BUSY; break;
164#endif
165#ifdef ECONNREFUSED
166  case ECONNREFUSED: _roar_errno = ROAR_ERROR_CONNREFUSED; break;
167#endif
168#ifdef ENOSYS
169  case ENOSYS:       _roar_errno = ROAR_ERROR_NOSYS; break;
170#endif
171#ifdef ENOTSUP
172  case ENOTSUP:      _roar_errno = ROAR_ERROR_NOTSUP; break;
173#endif
174#ifdef EPIPE
175  case EPIPE:        _roar_errno = ROAR_ERROR_PIPE; break;
176#endif
177#ifdef EPROTO
178  case EPROTO:       _roar_errno = ROAR_ERROR_PROTO; break;
179#endif
180#ifdef ERANGE
181  case ERANGE:       _roar_errno = ROAR_ERROR_RANGE; break;
182#endif
183#ifdef EMSGSIZE
184  case EMSGSIZE:     _roar_errno = ROAR_ERROR_MSGSIZE; break;
185#endif
186#ifdef ENOMEM
187  case ENOMEM:       _roar_errno = ROAR_ERROR_NOMEM; break;
188#endif
189#ifdef EINVAL
190  case EINVAL:       _roar_errno = ROAR_ERROR_INVAL; break;
191#endif
192#ifdef EALREADY
193  case EALREADY:     _roar_errno = ROAR_ERROR_ALREADY; break;
194#endif
195#ifdef EBADRQC
196  case EBADRQC:      _roar_errno = ROAR_ERROR_BADRQC; break;
197#endif
198#ifdef EDOM
199  case EDOM:         _roar_errno = ROAR_ERROR_DOM; break;
200#endif
201#ifdef EEXIST
202  case EEXIST:       _roar_errno = ROAR_ERROR_EXIST; break;
203#endif
204#ifdef EFAULT
205  case EFAULT:       _roar_errno = ROAR_ERROR_FAULT; break;
206#endif
207#ifdef EIO
208  case EIO:          _roar_errno = ROAR_ERROR_IO; break;
209#endif
210#ifdef EREMOTEIO
211  case EREMOTEIO:    _roar_errno = ROAR_ERROR_RIO; break;
212#endif
213#ifdef EKEYEXPIRED
214  case EKEYEXPIRED:  _roar_errno = ROAR_ERROR_KEYEXPIRED; break;
215#endif
216#ifdef EKEYREJECTED
217  case EKEYREJECTED: _roar_errno = ROAR_ERROR_KEYREJECTED; break;
218#endif
219#ifdef ELOOP
220  case ELOOP:        _roar_errno = ROAR_ERROR_LOOP; break;
221#endif
222#ifdef EMFILE
223  case EMFILE:       _roar_errno = ROAR_ERROR_MFILE; break;
224#endif
225#ifdef ENAMETOOLONG
226  case ENAMETOOLONG: _roar_errno = ROAR_ERROR_NAMETOOLONG; break;
227#endif
228#ifdef ENODATA
229  case ENODATA:      _roar_errno = ROAR_ERROR_NODATA; break;
230#endif
231#ifdef ENODEV
232  case ENODEV:       _roar_errno = ROAR_ERROR_NODEV; break;
233#endif
234#ifdef ENOSPC
235  case ENOSPC:       _roar_errno = ROAR_ERROR_NOSPC; break;
236#endif
237#ifdef ENOTCONN
238  case ENOTCONN:     _roar_errno = ROAR_ERROR_NOTCONN; break;
239#endif
240#ifdef EPROTONOSUPPORT
241  case EPROTONOSUPPORT: _roar_errno = ROAR_ERROR_PROTONOSUP; break;
242#endif
243#ifdef EROFS
244  case EROFS:        _roar_errno = ROAR_ERROR_RO; break;
245#endif
246#ifdef ETIMEDOUT
247  case ETIMEDOUT:    _roar_errno = ROAR_ERROR_TIMEDOUT; break;
248#endif
249#ifdef EAGAIN
250  case EAGAIN:       _roar_errno = ROAR_ERROR_AGAIN; break;
251#endif
252#ifdef ENETDOWN
253  case ENETDOWN:     _roar_errno = ROAR_ERROR_LINKDOWN; break;
254#endif
255#ifdef EINTR
256  case EINTR:        _roar_errno = ROAR_ERROR_INTERRUPTED; break;
257#endif
258#ifdef EDQUOT
259  case EDQUOT:       _roar_errno = ROAR_ERROR_QUOTA; break;
260#endif
261#ifdef ELIBBAD
262  case ELIBBAD:      _roar_errno = ROAR_ERROR_BADLIB; break;
263#endif
264#ifdef ENOMEDIUM
265  case ENOMEDIUM:    _roar_errno = ROAR_ERROR_NOMEDIUM; break;
266#endif
267#ifdef ENOTUNIQ
268  case ENOTUNIQ:     _roar_errno = ROAR_ERROR_NOTUNIQ; break;
269#endif
270#ifdef EILSEQ
271  case EILSEQ:       _roar_errno = ROAR_ERROR_ILLSEQ; break;
272#endif
273#ifdef EADDRINUSE
274  case EADDRINUSE:   _roar_errno = ROAR_ERROR_ADDRINUSE; break;
275#endif
276#ifdef ESPIPE
277  case ESPIPE:       _roar_errno = ROAR_ERROR_BADSEEK; break;
278#endif
279  default:
280    _roar_errno = ROAR_ERROR_UNKNOWN;
281   break;
282 }
283
284 roar_err_set(_roar_errno);
285}
286
287void   roar_err_to_errno(void) {
288 int * err = roar_errno2();
289 switch (*err) {
290  case ROAR_ERROR_NONE:
291    roar_err_clear_errno();
292   break;
293#ifdef EPERM
294  case ROAR_ERROR_PERM:
295    errno = EPERM;
296   break;
297#endif
298#ifdef ENOENT
299  case ROAR_ERROR_NOENT:
300    errno = ENOENT;
301   break;
302#endif
303#ifdef EBADMSG
304  case ROAR_ERROR_BADMSG:
305    errno = EBADMSG;
306   break;
307#endif
308#ifdef EBUSY
309  case ROAR_ERROR_BUSY:
310    errno = EBUSY;
311   break;
312#endif
313#ifdef ECONNREFUSED
314  case ROAR_ERROR_CONNREFUSED:
315    errno = ECONNREFUSED;
316   break;
317#endif
318#ifdef ENOSYS
319  case ROAR_ERROR_NOSYS:
320    errno = ENOSYS;
321   break;
322#endif
323#ifdef ENOTSUP
324  case ROAR_ERROR_NOTSUP:
325    errno = ENOTSUP;
326   break;
327#endif
328#ifdef EPIPE
329  case ROAR_ERROR_PIPE:
330    errno = EPIPE;
331   break;
332#endif
333#ifdef EPROTO
334  case ROAR_ERROR_PROTO:
335    errno = EPROTO;
336   break;
337#endif
338#ifdef ERANGE
339  case ROAR_ERROR_RANGE:
340    errno = ERANGE;
341   break;
342#endif
343#ifdef EMSGSIZE
344  case ROAR_ERROR_MSGSIZE:
345    errno = EMSGSIZE;
346   break;
347#endif
348#ifdef ENOMEM
349  case ROAR_ERROR_NOMEM:
350    errno = ENOMEM;
351   break;
352#endif
353#ifdef EINVAL
354  case ROAR_ERROR_INVAL:
355    errno = EINVAL;
356   break;
357#endif
358#ifdef EALREADY
359  case ROAR_ERROR_ALREADY:
360    errno = EALREADY;
361   break;
362#endif
363#ifdef EBADRQC
364  case ROAR_ERROR_BADRQC:
365    errno = EBADRQC;
366   break;
367#endif
368#ifdef EDOM
369  case ROAR_ERROR_DOM:
370    errno = EDOM;
371   break;
372#endif
373#ifdef EEXIST
374  case ROAR_ERROR_EXIST:
375    errno = EEXIST;
376   break;
377#endif
378#ifdef EFAULT
379  case ROAR_ERROR_FAULT:
380    errno = EFAULT;
381   break;
382#endif
383#if defined(EREMOTEIO) || defined(EIO)
384  case ROAR_ERROR_RIO:
385#ifdef EREMOTEIO
386    errno = EREMOTEIO;
387#else
388    errno = EIO;
389#endif
390   break;
391#endif
392#ifdef EIO
393  case ROAR_ERROR_IO:
394  case ROAR_ERROR_HOLE:
395  case ROAR_ERROR_BADCKSUM:
396  case ROAR_ERROR_LOSTSYNC:
397  case ROAR_ERROR_NOHORSE:
398    errno = EIO;
399   break;
400#endif
401#ifdef EKEYEXPIRED
402  case ROAR_ERROR_KEYEXPIRED:
403    errno = EKEYEXPIRED;
404   break;
405#endif
406#ifdef EKEYREJECTED
407  case ROAR_ERROR_KEYREJECTED:
408    errno = EKEYREJECTED;
409   break;
410#endif
411#ifdef ELOOP
412  case ROAR_ERROR_LOOP:
413    errno = ELOOP;
414   break;
415#endif
416#ifdef EMFILE
417  case ROAR_ERROR_MFILE:
418    errno = EMFILE;
419   break;
420#endif
421#ifdef ENAMETOOLONG
422  case ROAR_ERROR_NAMETOOLONG:
423    errno = ENAMETOOLONG;
424   break;
425#endif
426#ifdef ENODATA
427  case ROAR_ERROR_NODATA:
428    errno = ENODATA;
429   break;
430#endif
431#ifdef ENODEV
432  case ROAR_ERROR_NODEV:
433  case ROAR_ERROR_NODRV:
434    errno = ENODEV;
435   break;
436#endif
437#ifdef ENOSPC
438  case ROAR_ERROR_NOSPC:
439    errno = ENOSPC;
440   break;
441#endif
442#ifdef EINVAL
443  case ROAR_ERROR_TYPEMM:
444    errno = EINVAL;
445   break;
446#endif
447#ifdef ENOSYS
448  case ROAR_ERROR_NORSYS:
449    errno = ENOSYS;
450   break;
451#endif
452#ifdef ENOTCONN
453  case ROAR_ERROR_NOTCONN:
454    errno = ENOTCONN;
455   break;
456#endif
457#ifdef EPROTONOSUPPORT
458  case ROAR_ERROR_PROTONOSUP:
459    errno = EPROTONOSUPPORT;
460   break;
461#endif
462#ifdef EROFS
463  case ROAR_ERROR_RO:
464    errno = EROFS;
465   break;
466#endif
467#ifdef ETIMEDOUT
468  case ROAR_ERROR_TIMEDOUT:
469    errno = ETIMEDOUT;
470   break;
471#endif
472#ifdef EAGAIN
473  case ROAR_ERROR_AGAIN:
474    errno = EAGAIN;
475   break;
476#endif
477#ifdef ENETDOWN
478  case ROAR_ERROR_LINKDOWN:
479    errno = ENETDOWN;
480   break;
481#endif
482#ifdef EINTR
483  case ROAR_ERROR_INTERRUPTED:
484    errno = EINTR;
485   break;
486#endif
487#ifdef EDQUOT
488  case ROAR_ERROR_QUOTA:
489    errno = EDQUOT;
490   break;
491#endif
492#ifdef ELIBBAD
493  case ROAR_ERROR_BADLIB:
494    errno = ELIBBAD;
495   break;
496#endif
497#ifdef ENOMEDIUM
498  case ROAR_ERROR_NOMEDIUM:
499    errno = ENOMEDIUM;
500   break;
501#endif
502#ifdef ENOTUNIQ
503  case ROAR_ERROR_NOTUNIQ:
504    errno = ENOTUNIQ;
505   break;
506#endif
507#ifdef EILSEQ
508  case ROAR_ERROR_ILLSEQ:
509    errno = EILSEQ;
510   break;
511#endif
512#ifdef EADDRINUSE
513  case ROAR_ERROR_ADDRINUSE:
514    errno = EADDRINUSE;
515   break;
516#endif
517#ifdef ESPIPE
518  case ROAR_ERROR_BADSEEK:
519  case ROAR_ERROR_NOSEEK:
520    errno = ESPIPE;
521   break;
522#endif
523  default:
524#ifdef EINVAL
525    errno = EINVAL;
526#else
527    errno = -1; // just guess
528#endif
529   break;
530 }
531}
532
533// phi@ph7:roaraudio $ grep '^#define ROAR_ERROR_' error.h  | tr -d /\* | while read d c d t; do printf "  {%-23s \"%s\"},\n" $c, "$t"; done
534
535const char * roar_error2str(const int error) {
536 const struct {
537  const int    err;
538  const char * msg;
539 } msgs[] = {
540  {ROAR_ERROR_NONE,        "No error"},
541  {ROAR_ERROR_PERM,        "Operation not permitted"},
542  {ROAR_ERROR_NOENT,       "No such object, file or directory"},
543  {ROAR_ERROR_BADMSG,      "Bad message"},
544  {ROAR_ERROR_BUSY,        "Device or resource busy"},
545  {ROAR_ERROR_CONNREFUSED, "Connection refused"},
546  {ROAR_ERROR_NOSYS,       "Function not implemented"},
547  {ROAR_ERROR_NOTSUP,      "Operation not supported"},
548  {ROAR_ERROR_PIPE,        "Broken pipe"},
549  {ROAR_ERROR_PROTO,       "Protocol error"},
550  {ROAR_ERROR_RANGE,       "Result too large or parameter out of range"},
551  {ROAR_ERROR_MSGSIZE,     "Message too long"},
552  {ROAR_ERROR_NOMEM,       "Not enough space"},
553  {ROAR_ERROR_INVAL,       "Invalid argument"},
554  {ROAR_ERROR_ALREADY,     "Connection already in progress"},
555  {ROAR_ERROR_BADRQC,      "Invalid request code"},
556  {ROAR_ERROR_DOM,         "Mathematics argument out of domain of function"},
557  {ROAR_ERROR_EXIST,       "File or object exists"},
558  {ROAR_ERROR_FAULT,       "Bad address"},
559  {ROAR_ERROR_IO,          "I/O-Error"},
560  {ROAR_ERROR_KEYEXPIRED,  "Key has expired"},
561  {ROAR_ERROR_KEYREJECTED, "Key was rejected by service"},
562  {ROAR_ERROR_LOOP,        "Too many recursions"},
563  {ROAR_ERROR_MFILE,       "Too many open files or objects"},
564  {ROAR_ERROR_NAMETOOLONG, "File or object name too long"},
565  {ROAR_ERROR_NODATA,      "No message is available on the read queue"},
566  {ROAR_ERROR_NODEV,       "No such device"},
567  {ROAR_ERROR_NODRV,       "No such driver"},
568  {ROAR_ERROR_NOSPC,       "No space left on device"},
569  {ROAR_ERROR_TYPEMM,      "Type missmatch. Object of diffrent type required"},
570  {ROAR_ERROR_NORSYS,      "Feature not implemented by remote end"},
571  {ROAR_ERROR_NOTCONN,     "Socket or object not connected"},
572  {ROAR_ERROR_PROTONOSUP,  "Protocol not supported"},
573  {ROAR_ERROR_RIO,         "Remote I/O Error"},
574  {ROAR_ERROR_RO,          "File or object is read only"},
575  {ROAR_ERROR_TIMEDOUT,    "Connection timed out"},
576  {ROAR_ERROR_AGAIN,       "Resource temporarily unavailable"},
577  {ROAR_ERROR_NOISE,       "Line too noisy"},
578  {ROAR_ERROR_LINKDOWN,    "Physical or logical link down"},
579  {ROAR_ERROR_INTERRUPTED, "Operation was interruped"},
580  {ROAR_ERROR_CAUSALITY,   "Causality error"},
581  {ROAR_ERROR_QUOTA,       "Quota exceeded"},
582  {ROAR_ERROR_BADLIB,      "Accessing a corrupted shared library"},
583  {ROAR_ERROR_NOMEDIUM,    "No medium found"},
584  {ROAR_ERROR_NOTUNIQ,     "Name not unique"},
585  {ROAR_ERROR_ILLSEQ,      "Illegal byte sequence"},
586  {ROAR_ERROR_ADDRINUSE,   "Address in use"},
587  {ROAR_ERROR_HOLE,        "Hole in data"},
588  {ROAR_ERROR_BADVERSION,  "Bad version"},
589  {ROAR_ERROR_NSVERSION,   "Not supported version"},
590  {ROAR_ERROR_BADMAGIC,    "Bad magic number"},
591  {ROAR_ERROR_LOSTSYNC,    "Lost synchronization"},
592  {ROAR_ERROR_BADSEEK,     "Can not seek to destination position"},
593  {ROAR_ERROR_NOSEEK,      "Seeking not supported on resource"},
594  {ROAR_ERROR_BADCKSUM,    "Data integrity error"},
595  {ROAR_ERROR_NOHORSE,     "Mount failed"},
596  {ROAR_ERROR_CHERNOBYL,   "Fatal device error"},
597  {ROAR_ERROR_NOHUG,       "Device needs love"},
598  {-1, NULL}
599 };
600 int i;
601
602 for (i = 0; msgs[i].msg != NULL; i++)
603  if ( msgs[i].err == error )
604   return msgs[i].msg;
605
606 return NULL;
607}
608
609//ll
Note: See TracBrowser for help on using the repository browser.