para_play: simplify play_pcm(), add E_PLAY_OVERRUN, E_PREMATURE_END
[paraslash.git] / error.h
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file error.h list of error messages for all subsystems */
20
21 /** \cond list of all subsystems that support the shiny new error facility */
22 enum para_subsystem {
23         SS_RECV,
24         SS_NET,
25         SS_ORTP_RECV,
26         SS_AUDIOD,
27         SS_EXEC,
28         SS_CLOSE_ON_FORK,
29         SS_SIGNAL,
30         SS_STRING,
31         SS_DAEMON,
32         SS_STAT,
33         SS_TIME,
34         SS_GRAB_CLIENT,
35         SS_HTTP_RECV,
36         SS_RECV_COMMON,
37         SS_FILTER_CHAIN,
38         SS_WAV,
39         SS_COMPRESS,
40         SS_OGGDEC,
41         SS_FILTER,
42         SS_COMMAND,
43         SS_RANDOM_SELECTOR,
44         SS_PLAYLIST_SELECTOR,
45         SS_CRYPT,
46         SS_HTTP_SEND,
47         SS_ORTP_SEND,
48         SS_DB,
49         SS_OGG,
50         SS_MP3,
51         SS_MP3DEC,
52         SS_SERVER,
53         SS_AFS,
54         SS_MYSQL_SELECTOR,
55         SS_IPC,
56         SS_DCCP,
57         SS_DCCP_RECV,
58         SS_DCCP_SEND,
59         SS_FD,
60         SS_GUI,
61         SS_PLAY,
62         SS_ALSA,
63         SS_RINGBUFFER};
64
65 #define NUM_SS (SS_RINGBUFFER + 1)
66 extern const char **para_errlist[];
67 /** \endcond */
68
69 #define NET_ERRORS \
70         PARA_ERROR(SEND, "send error"), \
71         PARA_ERROR(RECV, "receive error"), \
72         PARA_ERROR(SOCKET, "socket error"), \
73         PARA_ERROR(CONNECT, "connect error"), \
74         PARA_ERROR(ACCEPT, "accept error"), \
75         PARA_ERROR(SETSOCKOPT, "failed to set socket options"), \
76         PARA_ERROR(BIND, "bind error"), \
77         PARA_ERROR(NAME_TOO_LONG, "name too long for struct sockaddr_un"), \
78         PARA_ERROR(CHMOD, "failed to set socket mode"), \
79         PARA_ERROR(SENDMSG, "sendmsg() failed"), \
80         PARA_ERROR(RECVMSG, "recvmsg() failed"), \
81         PARA_ERROR(SCM_CREDENTIALS, "did not receive SCM credentials"), \
82         PARA_ERROR(LISTEN, "listen error"), \
83         PARA_ERROR(RECV_PATTERN, "did not receive expected pattern"), \
84         PARA_ERROR(HOST_INFO, "gethostbyname() failed"), \
85
86
87 #define ORTP_RECV_ERRORS \
88         PARA_ERROR(MSG_TO_BUF, "failed to extract rtp packet"), \
89         PARA_ERROR(ORTP_SYNTAX, "ottp syntax error"), \
90         PARA_ERROR(TOO_MANY_BAD_CHUNKS, "too many consecutive bad chunks"), \
91         PARA_ERROR(INVALID_HEADER, "invalid header packet"), \
92         PARA_ERROR(OVERRUN, "outout buffer overrun"), \
93
94
95 #define HTTP_RECV_ERRORS \
96         PARA_ERROR(SEND_HTTP_REQUEST, "failed to send http request"), \
97         PARA_ERROR(MISSING_OK, "did not receive OK message from peer"), \
98         PARA_ERROR(HTTP_RECV_BUF, "did not receive buffer")
99
100
101 #define RECV_ERRORS \
102         PARA_ERROR(RECV_SELECT, "recv select error"), \
103         PARA_ERROR(WRITE_STDOUT, "stdout write error"), \
104
105
106 #define RECV_COMMON_ERRORS \
107         PARA_ERROR(RECV_SYNTAX, "recv syntax error"), \
108
109
110 #define AUDIOD_ERRORS \
111         PARA_ERROR(WRITE_AUDIO_DATA, "failed to write audio data"), \
112         PARA_ERROR(NO_MORE_SLOTS, "no more empty slots"), \
113         PARA_ERROR(MISSING_COLON, "syntax error: missing colon"), \
114         PARA_ERROR(UNSUPPORTED_AUDIO_FORMAT, "given audio format not supported"), \
115         PARA_ERROR(CLIENT_WRITE, "client write error"), \
116         PARA_ERROR(UCRED_PERM, "permission denied"), \
117         PARA_ERROR(INVALID_AUDIOD_CMD, "invalid command"), \
118
119
120 #define FILTER_CHAIN_ERRORS \
121         PARA_ERROR(UNSUPPORTED_FILTER, "given filter not supported"), \
122         PARA_ERROR(BAD_FILTER_OPTIONS, "invalid filter option given"), \
123
124
125 #define STAT_ERRORS \
126         PARA_ERROR(TOO_MANY_CLIENTS, "maximal number of stat clients exceeded"), \
127         PARA_ERROR(UNKNOWN_STAT_ITEM, "status item not recognized"), \
128
129
130 #define OGGDEC_ERRORS \
131         PARA_ERROR(OGGDEC_READ, "read from media returned an error"), \
132         PARA_ERROR(OGGDEC_NOTVORBIS, "bitstream is not vorbis data"), \
133         PARA_ERROR(OGGDEC_VERSION, "vorbis version mismatch"), \
134         PARA_ERROR(OGGDEC_BADHEADER, "invalid vorbis bitstream header"), \
135         PARA_ERROR(OGGDEC_FAULT, "bug or heap/stack corruption"), \
136         PARA_ERROR(OGGDEC_BADLINK, "invalid stream section or requested link corrupt"), \
137
138
139 #define GRAB_CLIENT_ERRORS \
140         PARA_ERROR(PEDANTIC_GRAB, "fd not ready and pedantic grab requested"), \
141         PARA_ERROR(GC_WRITE, "grab client write error"), \
142         PARA_ERROR(BAD_GC_SLOT, "invalid slot requested by grab client"), \
143         PARA_ERROR(BAD_GC_FILTER_NUM, "invalid filter number given"), \
144         PARA_ERROR(GC_SYNTAX, "grab client syntax error"), \
145         PARA_ERROR(GC_HELP_GIVEN, ""), /* not really an error */ \
146         PARA_ERROR(GC_VERSION_GIVEN, ""), /* not really an error */ \
147
148
149 #define MP3DEC_ERRORS \
150         PARA_ERROR(MAD_FRAME_DECODE, "mad frame decode error"), \
151         PARA_ERROR(MP3DEC_OVERRUN, "mp3 output buffer overrun"), \
152
153
154 #define FILTER_ERRORS \
155         PARA_ERROR(NO_FILTERS, "at least one filter must be given"), \
156         PARA_ERROR(FILTER_SYNTAX, "syntax error"), \
157
158
159 #define SIGNAL_ERRORS \
160         PARA_ERROR(SIGNAL_SIG_ERR, "signal() retured SIG_ERR"), \
161         PARA_ERROR(SIGNAL_READ, "read error from signal pipe"), \
162         PARA_ERROR(WAITPID, "waitpid error"), \
163         PARA_ERROR(SIGNAL_PIPE, "failed to setup signal pipe"), \
164
165
166 #define STRING_ERRORS \
167         PARA_ERROR(MKSTEMP, "mkstemp error: unable to create tmp file"), \
168         PARA_ERROR(FCHMOD, "fchmod error: can not set mode"), \
169
170
171 #define EXEC_ERRORS \
172         PARA_ERROR(DUP_PIPE, "exec error: can not create pipe"), \
173         PARA_ERROR(NULL_OPEN, "can not open /dev/null"), \
174
175
176 #define MP3_ERRORS \
177         PARA_ERROR(FREAD, "fread error"), \
178         PARA_ERROR(FSEEK, "fseek error"), \
179         PARA_ERROR(FRAME, "invalid mp3 frame"), \
180         PARA_ERROR(FRAME_LENGTH, "invalid frame length"), \
181         PARA_ERROR(MP3_NO_FILE, "invalid mp3 file pointer"), \
182         PARA_ERROR(MP3_INFO, "could not read mp3 info"), \
183         PARA_ERROR(MP3_REPOS, "mp3 repositioning error"), \
184         PARA_ERROR(HEADER_FREQ, "invalid header frequency"), \
185         PARA_ERROR(HEADER_BITRATE, "invalid header bitrate"), \
186
187
188 #define OGG_ERRORS \
189         PARA_ERROR(OGG_READ, "ogg read error"), \
190         PARA_ERROR(SYNC_PAGEOUT, "ogg sync page-out error (no ogg file?)"), \
191         PARA_ERROR(STREAM_PAGEIN, "ogg stream page-in error (first page)"), \
192         PARA_ERROR(STREAM_PACKETOUT, "ogg stream packet-out error (first packet)"), \
193         PARA_ERROR(VORBIS, "vorbis synthesis header-in error (not vorbis?)"), \
194         PARA_ERROR(OGG_NO_FILE, "invalid ogg file pointer"), \
195         PARA_ERROR(OGG_OPEN, "ov_open error"), \
196         PARA_ERROR(OGG_INFO, "ov_info error"), \
197         PARA_ERROR(OGG_REPOS, "ogg repositioning error"), \
198
199
200 #define AFS_ERRORS \
201         PARA_ERROR(AUDIO_FORMAT, "audio format not recognized"), \
202         PARA_ERROR(FSTAT, "failed to fstat() audio file"), \
203
204
205 #define DB_ERRORS \
206         PARA_ERROR(GETCWD, "can not get current working directory"), \
207         PARA_ERROR(CHDIR, "can not change directory"), \
208         PARA_ERROR(OPENDIR, "can not open directory"), \
209         PARA_ERROR(LSTAT, "lstat error"), \
210
211
212 #define CRYPT_ERRORS \
213         PARA_ERROR(PRIVATE_KEY, "can not read private key"), \
214         PARA_ERROR(PUBLIC_KEY, "can not read public key"), \
215         PARA_ERROR(RSA, "RSA error"), \
216         PARA_ERROR(ENCRYPT, "encrypt error"), \
217         PARA_ERROR(DECRYPT, "decrypt error"), \
218         PARA_ERROR(CHALLENGE, "failed to read challenge"), \
219
220
221 #define HTTP_SEND_ERRORS \
222         PARA_ERROR(QUEUE, "packet queue overrun"), \
223         PARA_ERROR(WRITE_OK, "can not check whether fd is writable"), \
224
225
226 #define RANDOM_SELECTOR_ERRORS \
227         PARA_ERROR(FILE_COUNT, "audio file count exceeded"), \
228         PARA_ERROR(NOTHING_FOUND, "no audio files found"), \
229
230
231 #define MYSQL_SELECTOR_ERRORS \
232         PARA_ERROR(MYSQL_SYNTAX, "mysql syntax error"), \
233         PARA_ERROR(NOTCONN, "not connected to mysql server"), \
234         PARA_ERROR(TOOBIG, "mysql: file too large"), \
235         PARA_ERROR(NAMETOOLONG, "mysql: name too long"), \
236         PARA_ERROR(QFAILED, "mysql query failed"), \
237         PARA_ERROR(NOROW, "row is NULL"), \
238         PARA_ERROR(NOATTS, "can not get attributes from mysql table"), \
239         PARA_ERROR(NORESULT, "error while fetching mysql result"), \
240         PARA_ERROR(EMPTY_RESULT, "result is empty"), \
241         PARA_ERROR(ESCAPE, "can not escape string"), \
242         PARA_ERROR(GET_AUDIO_FILE, "can not get current audio file"), \
243         PARA_ERROR(GET_STREAM, "can not get current stream"), \
244         PARA_ERROR(NO_STREAM, "no such stream"), \
245         PARA_ERROR(AUDIO_FILE, "no such audio file"), \
246         PARA_ERROR(GET_QUERY, "can not get query for specified stream"), \
247         PARA_ERROR(TMPFILE, "error while writing temporary file"), \
248         PARA_ERROR(META, "can not get meta data"), \
249         PARA_ERROR(MYSQL_INIT, "can not initialize mysql connection"), \
250         PARA_ERROR(NO_MYSQL_PASSWD, "fatal: no mysql passord given"), \
251         PARA_ERROR(NO_AF_DIR, "fatal: audio file directory not given"), \
252
253
254 #define COMMAND_ERRORS \
255         PARA_ERROR(COMMAND_SYNTAX, "syntax error in command"), \
256         PARA_ERROR(AUTH, "did not receive auth request"), \
257         PARA_ERROR(BAD_SELECTOR, "no such audio file selector"), \
258         PARA_ERROR(NO_AUDIO_FILE, "no audio file"), \
259         PARA_ERROR(BAD_CMD, "invalid command"), \
260         PARA_ERROR(PERM, "permission denied"), \
261         PARA_ERROR(USERLIST, "failed to open user list file"), \
262         PARA_ERROR(BAD_USER, "you don't exist. Go away."), \
263         PARA_ERROR(LOCK, "lock error"), \
264         PARA_ERROR(SENDER_CMD, "command not supported by this sender"), \
265
266 #define PLAYLIST_SELECTOR_ERRORS \
267         PARA_ERROR(LOAD_PLAYLIST, "failed to load playlist"), \
268
269
270 #define IPC_ERRORS \
271         PARA_ERROR(SEM_GET, "failed to create semaphore"), \
272         PARA_ERROR(SEM_REMOVE, "can not remove semaphore"), \
273         PARA_ERROR(SHM_GET, "failed to allocate shared memory area"), \
274         PARA_ERROR(SHM_DESTROY, "failed to destroy shared memory area"), \
275         PARA_ERROR(SHM_ATTACH, "can not attach shared memory area"), \
276         PARA_ERROR(SHM_DETACH, "can not detach shared memory area"), \
277
278
279 #define DCCP_ERRORS \
280         PARA_ERROR(DCCP_SOCKET, "can not create dccp socket"), \
281         PARA_ERROR(DCCP_PACKET_SIZE, "failed to set dccp packet size"), \
282         PARA_ERROR(DCCP_SERVICE, "could not get service code"), \
283
284
285 #define DCCP_RECV_ERRORS \
286         PARA_ERROR(ADDR_INFO, "getaddrinfo error"), \
287         PARA_ERROR(DCCP_OVERRUN, "dccp output buffer buffer overrun"), \
288         PARA_ERROR(DCCP_CONNECT, "dccp connect error"), \
289
290 #define DCCP_SEND_ERRORS \
291         PARA_ERROR(DCCP_BIND, "dccp bind error"), \
292         PARA_ERROR(DCCP_LISTEN, "dccp listen error"), \
293         PARA_ERROR(DCCP_WRITE, "dccp write error"), \
294
295 #define FD_ERRORS \
296         PARA_ERROR(F_GETFL, "failed to get fd flags"), \
297         PARA_ERROR(F_SETFL, "failed to set fd flags"), \
298
299
300 #define PLAY_ERRORS \
301         PARA_ERROR(READ_HDR, "failed to read audio file header"), \
302         PARA_ERROR(READ_STDIN, "failed to read from stdin"), \
303         PARA_ERROR(PLAY_SYNTAX, "syntax error"), \
304         PARA_ERROR(PLAY_OVERRUN, "buffer overrun"), \
305         PARA_ERROR(PREMATURE_END, "premature end of audio file"), \
306         PARA_ERROR(BROKEN_CONF, "Broken alsa configuration"), \
307         PARA_ERROR(ACCESS_TYPE, "alsa access type not available"), \
308         PARA_ERROR(SAMPLE_FORMAT, "sample format not available"), \
309         PARA_ERROR(CHANNEL_COUNT, "channels count not available"), \
310         PARA_ERROR(HW_PARAMS, "unable to install hw params"), \
311         PARA_ERROR(SW_PARAMS, "unable to install sw params"), \
312         PARA_ERROR(BAD_PERIOD, "can not use period equal to buffer size"), \
313         PARA_ERROR(GET_XFER, "unable to obtain xfer align"), \
314         PARA_ERROR(SET_XFER, "snd_pcm_sw_params_set_xfer_align() failed"), \
315         PARA_ERROR(ALSA_WRITE, "alsa write error"), \
316         PARA_ERROR(PCM_OPEN, "unable to open pcm"), \
317         PARA_ERROR(SND_PCM_INFO, "pcm info error"), \
318         PARA_ERROR(GET_BUFFER_TIME, "snd_pcm_hw_params_get_buffer_time_max() failed"), \
319         PARA_ERROR(SET_BUFFER_TIME, "snd_pcm_hw_params_set_buffer_time_near() failed"), \
320         PARA_ERROR(SET_RATE, "snd_pcm_hw_params_set_rate_near failed"), \
321         PARA_ERROR(START_THRESHOLD, "snd_pcm_sw_params_set_start_threshold() failed"), \
322         PARA_ERROR(STOP_THRESHOLD, "snd_pcm_sw_params_set_stop_threshold() failed"), \
323         PARA_ERROR(ALSA_LOG, "snd_output_stdio_attach() failed"), \
324
325
326
327 /* these do not need error handling (yet) */
328 #define ALSA_ERRORS
329 #define SERVER_ERRORS
330 #define WAV_ERRORS
331 #define COMPRESS_ERRORS
332 #define TIME_ERRORS
333 #define CLOSE_ON_FORK_ERRORS
334 #define DAEMON_ERRORS
335 #define ORTP_SEND_ERRORS
336 #define GUI_ERRORS
337 #define RINGBUFFER_ERRORS
338
339
340 /**
341  * the subsystem shift
342  *
343  * 255 error codes ought to be enough for every subsystem. Use the higher bits
344  * of the return value to encode the subsystem number
345  */
346 #define SS_SHIFT 8
347
348 /**
349  * compute the subsystem offset
350  *
351  * It is given by x * 2**8 where \a x is the subsystem number
352  */
353 #define SS_OFFSET(ss) (SS_ ## ss << SS_SHIFT)
354
355 /**
356  * make the enum of all errors of one subsystem
357  *
358  * As zero should not be an error, we define a dummy enum entry with value
359  * 2**ss. That lets the real errors start at 2**ss + 1.
360  */
361 #define SS_ENUM(ss) enum {\
362         E_ ## ss ## _DUMMY = SS_OFFSET(ss), \
363         ss ## _ERRORS}
364
365 /**
366  * determine the subsystem number from the error number
367  *
368  * Easy, it's just \a num / 2**8.
369  */
370 #define ERRNUM_TO_SS(num) ((num) >> SS_SHIFT)
371
372 /**
373  * determine the index of an error number
374  *
375  * Also easy: It's the lower 8 bits of num - 1.
376  */
377 #define ERRNUM_TO_INDEX(num) (((1 << SS_SHIFT) - 1) & ((num) - 1))
378
379 /**
380  * paraslash's version of strerror(3)
381  *
382  * expands to the error text of \a num (a string constant).
383  */
384 #define PARA_STRERROR(num) para_errlist[ERRNUM_TO_SS(num)] [ERRNUM_TO_INDEX(num)]
385
386 /**
387  * define the error list for one subsystem
388  *
389  * Used by macros in config.h (generated by configure)
390  */
391 #define DEFINE_ERRLIST(ss) const char * ss ## _ERRLIST[] = {ss ## _ERRORS}
392
393 /**
394  * activate errors for one subsystem.
395  *
396  * Each executable needs only the error lists of those subssystems it is actually
397  * linked with. We always reserve zeroed-out space for NUM_SS char ** pointers, but
398  * only init those of the needed subsystems. This macro is used by macros in config.h
399  * (generated by configure).
400  */
401 #define PARA_ERRLIST(ss) [SS_ ## ss] = ss ## _ERRLIST
402
403 /**
404  * This is temporarily defined to expand to its second argument (prefixed by
405  * 'E_') and gets later redefined to expand to the error text only
406  */
407 #define PARA_ERROR(err, msg) E_ ## err
408
409 /** \cond popcorn time */
410 SS_ENUM(ORTP_RECV);
411 SS_ENUM(NET);
412 SS_ENUM(RECV);
413 SS_ENUM(AUDIOD);
414 SS_ENUM(EXEC);
415 SS_ENUM(CLOSE_ON_FORK);
416 SS_ENUM(SIGNAL);
417 SS_ENUM(STRING);
418 SS_ENUM(DAEMON);
419 SS_ENUM(STAT);
420 SS_ENUM(TIME);
421 SS_ENUM(GRAB_CLIENT);
422 SS_ENUM(HTTP_RECV);
423 SS_ENUM(RECV_COMMON);
424 SS_ENUM(FILTER_CHAIN);
425 SS_ENUM(WAV);
426 SS_ENUM(COMPRESS);
427 SS_ENUM(OGGDEC);
428 SS_ENUM(MP3DEC);
429 SS_ENUM(FILTER);
430 SS_ENUM(MP3);
431 SS_ENUM(OGG);
432 SS_ENUM(SERVER);
433 SS_ENUM(AFS);
434 SS_ENUM(COMMAND);
435 SS_ENUM(RANDOM_SELECTOR);
436 SS_ENUM(PLAYLIST_SELECTOR);
437 SS_ENUM(CRYPT);
438 SS_ENUM(HTTP_SEND);
439 SS_ENUM(ORTP_SEND);
440 SS_ENUM(DB);
441 SS_ENUM(MYSQL_SELECTOR);
442 SS_ENUM(IPC);
443 SS_ENUM(DCCP);
444 SS_ENUM(DCCP_RECV);
445 SS_ENUM(DCCP_SEND);
446 SS_ENUM(FD);
447 SS_ENUM(GUI);
448 SS_ENUM(PLAY);
449 SS_ENUM(ALSA);
450 SS_ENUM(RINGBUFFER);
451 /** \endcond */
452 #undef PARA_ERROR
453 /* rest of the world only sees the error text */
454 #define PARA_ERROR(err, msg) msg