Imported Upstream version 3.2.0
[debian/amanda] / common-src / amanda.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amanda.h,v 1.131 2006/07/25 18:27:56 martinea Exp $
28  *
29  * the central header file included by all amanda sources
30  */
31 #ifndef AMANDA_H
32 #define AMANDA_H
33
34 #ifdef HAVE_CONFIG_H
35 /* use a relative path here to avoid conflicting with Perl's config.h. */
36 #include "../config/config.h"
37 #endif
38
39 #include <glib.h>
40 #include <glib/gprintf.h>
41
42 #include "amflock.h"
43
44 /*
45  * Force large file source even if configure guesses wrong.
46  */
47 #ifndef _LARGEFILE64_SOURCE
48 #define _LARGEFILE64_SOURCE 1
49 #endif
50
51 #ifndef  _FILE_OFFSET_BITS
52 #define _FILE_OFFSET_BITS 64
53 #endif
54
55 #ifdef HAVE_SYS_TYPES_H
56 #  include <sys/types.h>
57 #endif
58
59 /* gnulib creates this header locally if the system doesn't provide it,
60  * so it uses a local ("") include */
61 #include "stdint.h"
62
63 /*
64  * I would prefer that each Amanda module include only those system headers
65  * that are locally needed, but on most Unixes the system header files are not
66  * protected against multiple inclusion, so this can lead to problems.
67  *
68  * Also, some systems put key files in different places, so by including 
69  * everything here the rest of the system is isolated from such things.
70  */
71
72 /* from the autoconf documentation */
73 #ifdef HAVE_DIRENT_H
74 #  include <dirent.h>
75 #  define NAMLEN(dirent) strlen((dirent)->d_name)
76 #else
77 #  define dirent direct
78 #  define NAMLEN(dirent) (dirent)->d_namlen
79 #  if HAVE_SYS_NDIR_H
80 #    include <sys/ndir.h>
81 #  endif
82 #  if HAVE_SYS_DIR_H
83 #    include <sys/dir.h>
84 #  endif
85 #  if HAVE_NDIR_H
86 #    include <ndir.h>
87 #  endif
88 #endif
89
90 #ifdef ENABLE_NLS
91 #  include <libintl.h>
92 #  include <locale.h>
93 #  define  plural(String1, String2, Count)                              \
94                 (((Count) == 1) ? (String1) : (String2))
95 #else
96 #  define plural(String1, String2, Count)                               \
97                 (((Count) == 1) ? (String1) : (String2))
98 #  define setlocale(Which, Locale)
99 #  define textdomain(Domain)
100 #  define bindtextdomain(Package, Directory)
101 #  define gettext(String)                       String
102 #  define dgettext(Domain, String)              String
103 #  define dcgettext(Domain, String, Catagory)   String
104 #  define ngettext(String1, String2, Count)                             \
105                 plural((String1), (String2), (Count))
106 #  define dngettext(Domain, String1, String2, Count)                    \
107                 plural((String1), (String2), (Count))
108 #  define dcngettext(Domain, String1, String2, Count, Catagory)         \
109                 plural((String1), (String2), (Count))
110 #endif
111 #define T_(String)                      String
112 #ifndef SWIG /* TODO: make this go away */
113 #define _(String)                       dgettext("amanda", (String))
114 #endif
115
116 #ifdef HAVE_FCNTL_H
117 #  include <fcntl.h>
118 #endif
119
120 #ifdef HAVE_GRP_H
121 #  include <grp.h>
122 #endif
123
124 #if defined(USE_DB_H)
125 #  include <db.h>
126 #else
127 #if defined(USE_DBM_H)
128 #  include <dbm.h>
129 #else
130 #if defined(USE_GDBM_H)
131 #  include <gdbm.h>
132 #else
133 #if defined(USE_NDBM_H)
134 #  include <ndbm.h>
135 #endif
136 #endif
137 #endif
138 #endif
139
140 #ifdef TIME_WITH_SYS_TIME
141 #  include <sys/time.h>
142 #  include <time.h>
143 #else
144 #  ifdef HAVE_SYS_TIME_H
145 #    include <sys/time.h>
146 #  else
147 #    include <time.h>
148 #  endif
149 #endif
150
151 #ifdef HAVE_LIBC_H
152 #  include <libc.h>
153 #endif
154
155 #ifdef HAVE_STDLIB_H
156 #  include <stdlib.h>
157 #endif
158
159 #ifdef HAVE_LIBGEN_H
160 #  include <libgen.h>
161 #endif
162
163 #ifdef HAVE_STRING_H
164 #  include <string.h>
165 #endif
166
167 #ifdef HAVE_STRINGS_H
168 #  include <strings.h>
169 #endif
170
171 #ifdef HAVE_SYSLOG_H
172 #  include <syslog.h>
173 #endif
174
175 #ifdef HAVE_MATH_H
176 #  include <math.h>
177 #endif
178
179 #ifdef HAVE_SYS_FILE_H
180 #  include <sys/file.h>
181 #endif
182
183 #ifdef HAVE_SYS_IOCTL_H
184 #  include <sys/ioctl.h>
185 #endif
186
187 #ifdef HAVE_LIMITS_H
188 #include <limits.h>
189 #endif
190
191 #ifdef HAVE_SYS_PARAM_H
192 #  include <sys/param.h>
193 #endif
194
195 #if defined(HAVE_SYS_IPC_H) && defined(HAVE_SYS_SHM_H)
196 #  include <sys/ipc.h>
197 #  include <sys/shm.h>
198 #else
199 #  ifdef HAVE_SYS_MMAN_H
200 #    include <sys/mman.h>
201 #  endif
202 #endif
203
204 #ifdef HAVE_SYS_SELECT_H
205 #  include <sys/select.h>
206 #endif
207
208 #ifdef HAVE_SYS_STAT_H
209 #  include <sys/stat.h>
210 #endif
211
212 #ifdef HAVE_SYS_UIO_H
213 #  include <sys/uio.h>
214 #else
215 struct iovec {
216     void *iov_base;
217     int iov_len;
218 };
219 #endif
220
221 #ifdef HAVE_WAIT_H
222 #  include <wait.h>
223 #endif
224
225 #ifdef HAVE_SYS_WAIT_H
226 #  include <sys/wait.h>
227 #endif
228
229 #ifdef HAVE_STDARG_H
230 #include <stdarg.h>
231 #endif
232
233 #ifdef WAIT_USES_INT
234   typedef int amwait_t;
235 # ifndef WEXITSTATUS
236 #  define WEXITSTATUS(stat_val) (*(unsigned*)&(stat_val) >> 8)
237 # endif
238 # ifndef WTERMSIG
239 #  define WTERMSIG(stat_val) (*(unsigned*)&(stat_val) & 0x7F)
240 # endif
241 # ifndef WIFEXITED
242 #  define WIFEXITED(stat_val) ((*(unsigned*)&(stat_val) & 255) == 0)
243 # endif
244 #else
245 # ifdef WAIT_USES_UNION
246    typedef union wait amwait_t;
247 #  ifndef WEXITSTATUS
248 #  define WEXITSTATUS(stat_val) (((amwait_t*)&(stat_val))->w_retcode)
249 #  endif
250 #  ifndef WTERMSIG
251 #   define WTERMSIG(stat_val) (((amwait_t*)&(stat_val))->w_termsig)
252 #  endif
253 #  ifndef WIFEXITED
254 #   define WIFEXITED(stat_val) (WTERMSIG(stat_val) == 0)
255 #  endif
256 # else
257    typedef int amwait_t;
258 #  ifndef WEXITSTATUS
259 #   define WEXITSTATUS(stat_val) (*(unsigned*)&(stat_val) >> 8)
260 #  endif
261 #  ifndef WTERMSIG
262 #   define WTERMSIG(stat_val) (*(unsigned*)&(stat_val) & 0x7F)
263 #  endif
264 #  ifndef WIFEXITED
265 #   define WIFEXITED(stat_val) ((*(unsigned*)&(stat_val) & 255) == 0)
266 #  endif
267 # endif
268 #endif
269
270 #ifndef WIFSIGNALED
271 # define WIFSIGNALED(stat_val)  (WTERMSIG(stat_val) != 0)
272 #endif
273
274 #ifdef HAVE_UNISTD_H
275 #  include <unistd.h>
276 #endif
277
278 #ifdef HAVE_NETINET_IN_H
279 #  include <netinet/in.h>
280 #endif
281
282 #include <ctype.h>
283 #include <errno.h>
284 #include <pwd.h>
285 #include <signal.h>
286 #include <setjmp.h>
287 #include <stdio.h>
288 #include <sys/resource.h>
289 #include <sys/socket.h>
290
291 #ifdef WORKING_IPV6
292 #define INET6
293 #endif
294
295 #ifndef INET_ADDRSTRLEN
296 #define INET_ADDRSTRLEN 16
297 #endif
298
299 #if !defined(HAVE_SIGACTION) && defined(HAVE_SIGVEC)
300 /* quick'n'dirty hack for NextStep31 */
301 #  define sa_flags sv_flags
302 #  define sa_handler sv_handler
303 #  define sa_mask sv_mask
304 #  define sigaction sigvec
305 #  define sigemptyset(mask) /* no way to clear pending signals */
306 #endif
307
308 /*
309  * Most Unixen declare errno in <errno.h>, some don't.  Some multithreaded
310  * systems have errno as a per-thread macro.  So, we have to be careful.
311  */
312 #ifndef errno
313 extern int errno;
314 #endif
315
316 /*
317  * Some compilers have int for type of sizeof() some use size_t.
318  * size_t is the one we want...
319  */
320 #define SIZEOF(x)       (size_t)sizeof(x)
321
322
323 /*
324  * Some older BSD systems don't have these FD_ macros, so if not, provide them.
325  */
326 #if !defined(FD_SET) || defined(LINT) || defined(__lint)
327 #  undef FD_SETSIZE
328 #  define FD_SETSIZE      (int)(SIZEOF(fd_set) * CHAR_BIT)
329
330 #  undef FD_SET
331 #  define FD_SET(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] |= (int)((1 << ((n) % WORD_BIT))))
332
333 #  undef FD_CLR
334 #  define FD_CLR(n, p)    (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] &= (int)(~(1 << ((n) % WORD_BIT))))
335
336 #  undef FD_ISSET
337 #  define FD_ISSET(n, p)  (((fd_set *)(p))->fds_bits[(n)/WORD_BIT] & (1 << ((n) % WORD_BIT)))
338
339 #  undef FD_ZERO
340 #  define FD_ZERO(p)      memset((p), 0, SIZEOF(*(p)))
341 #endif
342
343 #ifndef FD_COPY
344 #  define FD_COPY(p, q)   memcpy((q), (p), SIZEOF(*(p)))
345 #endif
346
347
348 /*
349  * Define MAX_HOSTNAME_LENGTH as the size of arrays to hold hostname's.
350  */
351 #undef  MAX_HOSTNAME_LENGTH
352 #define MAX_HOSTNAME_LENGTH 1025
353
354 /*
355  * If void is broken, substitute char.
356  */
357 #ifdef BROKEN_VOID
358 #  define void char
359 #endif
360
361 #define stringize(x) #x
362 #define stringconcat(x, y) x ## y
363
364 /* amanda #days calculation, with roundoff */
365
366 #define SECS_PER_DAY    (24*60*60)
367 #define days_diff(a, b) (int)(((b) - (a) + SECS_PER_DAY/2) / SECS_PER_DAY)
368
369 /* Global constants.  */
370 #ifndef AMANDA_SERVICE_NAME
371 #define AMANDA_SERVICE_NAME "amanda"
372 #endif
373 #ifndef KAMANDA_SERVICE_NAME
374 #define KAMANDA_SERVICE_NAME "kamanda"
375 #endif
376 #ifndef SERVICE_SUFFIX
377 #define SERVICE_SUFFIX ""
378 #endif
379 #ifndef AMANDA_SERVICE_DEFAULT
380 #define AMANDA_SERVICE_DEFAULT  ((in_port_t)10080)
381 #endif
382 #ifndef KAMANDA_SERVICE_DEFAULT
383 #define KAMANDA_SERVICE_DEFAULT ((in_port_t)10081)
384 #endif
385
386 #define am_round(v,u)   ((((v) + (u) - 1) / (u)) * (u))
387 #define am_floor(v,u)   (((v) / (u)) * (u))
388
389 /* Holding disk block size.  Do not even think about changint this!  :-) */
390 #define DISK_BLOCK_KB           32
391 #define DISK_BLOCK_BYTES        (DISK_BLOCK_KB * 1024)
392
393 /* Maximum length of tape label, plus one for null-terminator. */
394 #define MAX_TAPE_LABEL_LEN (10240)
395 #define MAX_TAPE_LABEL_BUF (MAX_TAPE_LABEL_LEN+1)
396 #define MAX_TAPE_LABEL_FMT "%10240s"
397
398 /* Unfortunately, the system-level sockaddr_storage definition can lead to
399  * C aliasing errors (where the optimizer doesn't notice that two operations
400  * affect the same datum).  We define our own similar type as a union.
401  */
402 typedef union sockaddr_union {
403     struct sockaddr         sa;
404     struct sockaddr_in      sin;
405 #ifdef WORKING_IPV6
406     struct sockaddr_in6     sin6;
407 #endif
408 #ifdef HAVE_SOCKADDR_STORAGE
409     struct sockaddr_storage ss; /* not used; just here to make the union full-size */
410 #endif
411 } sockaddr_union;
412
413 #include "debug.h"
414 #include "file.h"
415
416 void *debug_alloc(const char *file, int line, size_t size);
417 void *debug_newalloc(const char *file, int line, void *old, size_t size);
418 char *debug_stralloc(const char *file, int line, const char *str);
419 char *debug_newstralloc(const char *file, int line,
420                 char *oldstr, const char *newstr);
421 char *debug_vstralloc(const char *file, int line, const char *str, ...);
422 char *debug_newvstralloc(const char *file, int line,
423                 char *oldstr, const char *str, ...);
424 char *debug_vstrallocf(const char *file, int line, const char *fmt,
425                 ...) G_GNUC_PRINTF(3, 4);
426 char *debug_newvstrallocf(const char *file, int line, char *oldstr,
427                 const char *fmt, ...) G_GNUC_PRINTF(4, 5);
428
429 /* Usage: vstrextend(foo, "bar, "baz", NULL). Extends the existing 
430  * string, or allocates a brand new one. */
431 char *debug_vstrextend(const char *file, int line, char **oldstr, ...);
432
433 #define alloc(s)                debug_alloc(__FILE__, __LINE__, (s))
434 #define newalloc(p,s)           debug_newalloc(__FILE__, __LINE__, (p), (s))
435 #define stralloc(s)             debug_stralloc(__FILE__, __LINE__, (s))
436 #define newstralloc(p,s)        debug_newstralloc(__FILE__, __LINE__, (p), (s))
437 #define vstralloc(...)          debug_vstralloc(__FILE__,__LINE__,__VA_ARGS__)
438 #define newvstralloc(...)       debug_newvstralloc(__FILE__,__LINE__,__VA_ARGS__)
439 #define vstrallocf(...)         debug_vstrallocf(__FILE__,__LINE__,__VA_ARGS__)
440 #define newvstrallocf(...)      debug_newvstrallocf(__FILE__,__LINE__,__VA_ARGS__)
441 #define vstrextend(...)         debug_vstrextend(__FILE__,__LINE__,__VA_ARGS__)
442
443 #define stralloc2(s1,s2)        vstralloc((s1),(s2),NULL)
444 #define newstralloc2(p,s1,s2)   newvstralloc((p),(s1),(s2),NULL)
445
446 #define vstrallocf(...)         debug_vstrallocf(__FILE__,__LINE__,__VA_ARGS__)
447
448 /*@only@*/ /*@null@*/ char *debug_agets(const char *file, int line, FILE *f);
449 /*@only@*/ /*@null@*/ char *debug_areads(const char *file, int line, int fd);
450 #define agets(f)              debug_agets(__FILE__,__LINE__,(f))
451 #define areads(f)             debug_areads(__FILE__,__LINE__,(f))
452
453 /* return a "safe" version of the current environment; pass this to execle */
454 #define safe_env() safe_env_full(NULL)
455
456 /* like safe_env, but optionally add additional environment variables */
457 char ** safe_env_full(char **add);
458
459 time_t  unctime(char *timestr);
460
461 /*
462  * amfree(ptr) -- if allocated, release space and set ptr to NULL.
463  *
464  * In general, this should be called instead of just free(), unless
465  * the very next source line sets the pointer to a new value.
466  */
467
468 #define amfree(ptr) do {                                                \
469     if((ptr) != NULL) {                                                 \
470         int e__errno = errno;                                           \
471         free(ptr);                                                      \
472         (ptr) = NULL;                                                   \
473         errno = e__errno;                                               \
474         (void)(ptr);  /* Fix value never used warning at end of routines */ \
475     }                                                                   \
476 } while (0)
477
478 #define strappend(s1,s2) do {                                           \
479     char *t_t_t = (s1) ? stralloc2((s1),(s2)) : stralloc((s2));         \
480     amfree((s1));                                                       \
481     (s1) = t_t_t;                                                       \
482 } while(0)
483
484 /*
485  * Return the number of elements in an array.
486  */
487 #define am_countof(a)   (int)(SIZEOF(a) / SIZEOF((a)[0]))
488
489 /*
490  * min/max.  Don't do something like
491  *
492  *    x = min(y++, z);
493  *
494  * because the increment will be duplicated.
495  */
496 #undef min
497 #undef max
498 #define min(a, b)       ((a) < (b) ? (a) : (b))
499 #define max(a, b)       ((a) > (b) ? (a) : (b))
500
501 /*
502  * Utility bitmask manipulation macros.
503  */
504 #define SET(t, f)       ((t) |= (f))
505 #define CLR(t, f)       ((t) &= ~((unsigned)(f)))
506 #define ISSET(t, f)     ((t) & (f))
507
508 /*
509  * Utility string macros.  All assume a variable holds the current
510  * character and the string pointer points to the next character to
511  * be processed.  Typical setup is:
512  *
513  *  s = buffer;
514  *  ch = *s++;
515  *  skip_whitespace(s, ch);
516  *  ...
517  *
518  * If you advance the pointer "by hand" to skip over something, do
519  * it like this:
520  *
521  *  s += some_amount;
522  *  ch = s[-1];
523  *
524  * Note that ch has the character at the end of the just skipped field.
525  * It is often useful to terminate a string, make a copy, then restore
526  * the input like this:
527  *
528  *  skip_whitespace(s, ch);
529  *  fp = s-1;                   ## save the start
530  *  skip_nonwhitespace(s, ch);  ## find the end
531  *  p[-1] = '\0';               ## temporary terminate
532  *  field = stralloc(fp);       ## make a copy
533  *  p[-1] = ch;                 ## restore the input
534  *
535  * The scanning macros are:
536  *
537  *  skip_whitespace (ptr, var)
538  *    -- skip whitespace, but stops at a newline
539  *  skip_non_whitespace (ptr, var)
540  *    -- skip non whitespace
541  *  skip_non_whitespace_cs (ptr, var)
542  *    -- skip non whitespace, stop at comment
543  *  skip_integer (ptr, var)
544  *    -- skip an integer field
545  *  skip_line (ptr, var)
546  *    -- skip just past the next newline
547  *  strncmp_const (str, const_str)
548  *    -- compare str to const_str, a string constant
549  *  strncmp_const_skip (str, const_var, ptr, var)
550  *    -- like strncmp_const, but skip the string if a match is
551  *       found; this macro only tests for equality, discarding
552  *       ordering information.
553  *
554  * where:
555  *
556  *  ptr -- string pointer
557  *  var -- current character
558  *
559  * These macros copy a non-whitespace field to a new buffer, and should
560  * only be used if dynamic allocation is impossible (fixed size buffers
561  * are asking for trouble):
562  *
563  *  copy_string (ptr, var, field, len, fldptr)
564  *    -- copy a non-whitespace field
565  *  copy_string_cs (ptr, var, field, len, fldptr)
566  *    -- copy a non-whitespace field, stop at comment
567  *
568  * where:
569  *
570  *  ptr -- string pointer
571  *  var -- current character
572  *  field -- area to copy to
573  *  len -- length of area (needs room for null byte)
574  *  fldptr -- work pointer used in move
575  *            if NULL on exit, the field was too small for the input
576  */
577
578 #define STR_SIZE        4096            /* a generic string buffer size */
579 #define NUM_STR_SIZE    128             /* a generic number buffer size */
580
581 #define skip_whitespace(ptr,c) do {                                     \
582     while((c) != '\n' && g_ascii_isspace((int)c)) (c) = *(ptr)++;               \
583 } while(0)
584
585 #define skip_non_whitespace(ptr,c) do {                                 \
586     while((c) != '\0' && !g_ascii_isspace((int)c)) (c) = *(ptr)++;              \
587 } while(0)
588
589 #define skip_non_whitespace_cs(ptr,c) do {                              \
590     while((c) != '\0' && (c) != '#' && !g_ascii_isspace((int)c)) (c) = *(ptr)++;\
591 } while(0)
592
593 #define skip_non_integer(ptr,c) do {                                    \
594     while((c) != '\0' && !isdigit(c)) (c) = *(ptr)++;                   \
595 } while(0)
596
597 #define skip_integer(ptr,c) do {                                        \
598     if((c) == '+' || (c) == '-') (c) = *(ptr)++;                        \
599     while(isdigit(c)) (c) = *(ptr)++;                                   \
600 } while(0)
601
602 #define skip_quoted_string(ptr, c) do {                                 \
603     int iq = 0;                                                         \
604     while (((c) != '\0') && !((iq == 0) && g_ascii_isspace((int)c))) {          \
605         if ((c) == '"') {                                               \
606             iq = !iq;                                                   \
607         } else if ((c) == '\\') {                                       \
608             if (*ptr)   /* not last character */                        \
609                 (ptr)++;                                                \
610         }                                                               \
611         (c) = *(ptr)++;                                                 \
612     }                                                                   \
613 } while (0)
614
615 #define skip_quoted_line(ptr, c) do {                                   \
616     int iq = 0;                                                         \
617     while((c) && !((iq == 0) && ((c) == '\n'))) {                       \
618         if ((c) == '"')                                                 \
619             iq = !iq;                                                   \
620         (c) = *(ptr)++;                                                 \
621     }                                                                   \
622     if(c)                                                               \
623         (c) = *(ptr)++;                                                 \
624 } while(0)
625
626 #define skip_line(ptr,c) do {                                           \
627     while((c) && (c) != '\n')                                           \
628         (c) = *(ptr)++;                                                 \
629     if(c)                                                               \
630         (c) = *(ptr)++;                                                 \
631 } while(0)
632
633 #define copy_string(ptr,c,f,l,fp) do {                                  \
634     (fp) = (f);                                                         \
635     while((c) != '\0' && !g_ascii_isspace((int)c)) {                            \
636         if((fp) >= (f) + (l) - 1) {                                     \
637             *(fp) = '\0';                                               \
638             (fp) = NULL;                                                \
639             (void)(fp);  /* Fix value never used warning at end of routines */ \
640             break;                                                      \
641         }                                                               \
642         *(fp)++ = (c);                                                  \
643         (c) = *(ptr)++;                                                 \
644     }                                                                   \
645     if(fp)                                                              \
646         *fp = '\0';                                                     \
647 } while(0)
648
649 #define copy_string_cs(ptr,c,f,l,fp) do {                               \
650     (fp) = (f);                                                         \
651     while((c) != '\0' && (c) != '#' && !g_ascii_isspace((int)c)) {              \
652         if((fp) >= (f) + (l) - 1) {                                     \
653             *(fp) = '\0';                                               \
654             (fp) = NULL;                                                \
655             break;                                                      \
656         }                                                               \
657         *(fp)++ = (c);                                                  \
658         (c) = *(ptr)++;                                                 \
659     }                                                                   \
660     if(fp) *fp = '\0';                                                  \
661 } while(0)
662
663 #define is_dot_or_dotdot(s)                                             \
664     ((s)[0] == '.'                                                      \
665      && ((s)[1] == '\0'                                                 \
666          || ((s)[1] == '.' && (s)[2] == '\0')))
667
668 #define strncmp_const(str, cnst)                                        \
669         strncmp((str), (cnst), sizeof((cnst))-1)
670
671 /* (have to roll this up in an expression, so it can be used in if()) */
672 #define strncmp_const_skip(str, cnst, ptr, var)                         \
673         ((strncmp((str), (cnst), sizeof((cnst))-1) == 0)?               \
674                  ((ptr)+=sizeof((cnst))-1, (var)=(ptr)[-1], 0)          \
675                 :1)
676
677 /* from old bsd-security.c */
678 extern int debug;
679 extern int check_security(sockaddr_union *, char *, unsigned long, char **);
680
681 /*
682  * Handle functions which are not always declared on all systems.  This
683  * stops gcc -Wall and lint from complaining.
684  */
685
686 /* AIX #defines accept, and provides a prototype for the alternate name */
687 #if !defined(HAVE_ACCEPT_DECL) && !defined(accept)
688 extern int accept(int s, struct sockaddr *addr, socklen_t_equiv *addrlen);
689 #endif
690
691 #ifndef HAVE_ATOF_DECL
692 extern double atof(const char *ptr);
693 #endif
694
695 #ifndef HAVE_BCOPY
696 # define bcopy(from,to,n) ((void)memmove((to), (from), (n)))
697 #else
698 # ifndef HAVE_BCOPY_DECL
699 extern void bcopy(const void *s1, void *s2, size_t n);
700 # endif
701 #endif
702
703 #ifndef HAVE_BIND_DECL
704 extern int bind(int s, const struct sockaddr *name, socklen_t_equiv namelen);
705 #endif
706
707 #ifndef HAVE_BZERO
708 #define bzero(s,n) ((void)memset((s),0,(n)))
709 #else
710 # ifndef HAVE_BZERO_DECL
711 extern void bzero(void *s, size_t n);
712 # endif
713 #endif
714
715 #ifndef HAVE_CLOSELOG_DECL
716 extern void closelog(void);
717 #endif
718
719 #ifndef HAVE_CONNECT_DECL
720 extern int connect(int s, struct sockaddr *name, socklen_t_equiv namelen);
721 #endif
722
723 #ifndef HAVE_FCLOSE_DECL
724 extern int fclose(FILE *stream);
725 #endif
726
727 #ifndef HAVE_FFLUSH_DECL
728 extern int fflush(FILE *stream);
729 #endif
730
731 #ifndef HAVE_FPRINTF_DECL
732 extern int fprintf(FILE *stream, const char *format, ...);
733 #endif
734
735 #ifndef HAVE_FPUTC_DECL
736 extern int fputc(int c, FILE *stream);
737 #endif
738
739 #ifndef HAVE_FPUTS_DECL
740 extern int fputs(const char *s, FILE *stream);
741 #endif
742
743 #ifndef HAVE_FREAD_DECL
744 extern size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
745 #endif
746
747 #ifndef HAVE_FSEEK_DECL
748 extern int fseek(FILE *stream, long offset, int ptrname);
749 #endif
750
751 #ifndef HAVE_FWRITE_DECL
752 extern size_t fwrite(const void *ptr, size_t size, size_t nitems,
753                         FILE *stream);
754 #endif
755
756 #ifndef HAVE_GETHOSTNAME_DECL
757 extern int gethostname(char *name, int namelen);
758 #endif
759
760 #ifndef HAVE_GETOPT_DECL
761 extern char *optarg;
762 extern int getopt(int argc, char * const *argv, const char *optstring);
763 #endif
764
765 /* AIX #defines getpeername, and provides a prototype for the alternate name */
766 #if !defined(HAVE_GETPEERNAME_DECL) && !defined(getpeername)
767 extern int getpeername(int s, struct sockaddr *name, socklen_t_equiv *namelen);
768 #endif
769
770 /* AIX #defines getsockname, and provides a prototype for the alternate name */
771 #if !defined(HAVE_GETSOCKNAME_DECL) && !defined(getsockname)
772 extern int getsockname(int s, struct sockaddr *name, socklen_t_equiv *namelen);
773 #endif
774
775 #ifndef HAVE_GETSOCKOPT_DECL
776 extern int getsockopt(int s, int level, int optname, char *optval,
777                          socklen_t_equiv *optlen);
778 #endif
779
780 #ifndef HAVE_INITGROUPS
781 # define initgroups(name,basegid) 0
782 #else
783 # ifndef HAVE_INITGROUPS_DECL
784 extern int initgroups(const char *name, gid_t basegid);
785 # endif
786 #endif
787
788 #ifndef HAVE_IOCTL_DECL
789 extern int ioctl(int fildes, int request, ...);
790 #endif
791
792 #ifndef isnormal
793 #ifndef HAVE_ISNORMAL
794 #define isnormal(f) (((f) < 0.0) || ((f) > 0.0))
795 #endif
796 #endif
797
798 #ifndef HAVE_LISTEN_DECL
799 extern int listen(int s, int backlog);
800 #endif
801
802 #ifndef HAVE_LSTAT_DECL
803 extern int lstat(const char *path, struct stat *buf);
804 #endif
805
806 #ifndef HAVE_MALLOC_DECL
807 extern void *malloc (size_t size);
808 #endif
809
810 #ifndef HAVE_MEMMOVE_DECL
811 #ifdef HAVE_MEMMOVE
812 extern void *memmove(void *to, const void *from, size_t n);
813 #else
814 extern char *memmove(char *to, /*const*/ char *from, size_t n);
815 #endif
816 #endif
817
818 #ifndef HAVE_MEMSET_DECL
819 extern void *memset(void *s, int c, size_t n);
820 #endif
821
822 #ifndef HAVE_MKTEMP_DECL
823 extern char *mktemp(char *template);
824 #endif
825
826 #ifndef HAVE_MKSTEMP_DECL
827 extern int mkstemp(char *template);
828 #endif
829
830 #ifndef HAVE_MKTIME_DECL
831 extern time_t mktime(struct tm *timeptr);
832 #endif
833
834 #ifndef HAVE_OPENLOG_DECL
835 #ifdef LOG_AUTH
836 extern void openlog(const char *ident, int logopt, int facility);
837 #else
838 extern void openlog(const char *ident, int logopt);
839 #endif
840 #endif
841
842 #ifndef HAVE_PCLOSE_DECL
843 extern int pclose(FILE *stream);
844 #endif
845
846 #ifndef HAVE_PERROR_DECL
847 extern void perror(const char *s);
848 #endif
849
850 #ifndef HAVE_PRINTF_DECL
851 extern int printf(const char *format, ...);
852 #endif
853
854 #ifndef HAVE_PUTS_DECL
855 extern int puts(const char *s);
856 #endif
857
858 #ifndef HAVE_REALLOC_DECL
859 extern void *realloc(void *ptr, size_t size);
860 #endif
861
862 /* AIX #defines recvfrom, and provides a prototype for the alternate name */
863 #if !defined(HAVE_RECVFROM_DECL) && !defined(recvfrom)
864 extern int recvfrom(int s, char *buf, int len, int flags,
865                        struct sockaddr *from, socklen_t_equiv *fromlen);
866 #endif
867
868 #ifndef HAVE_REMOVE_DECL
869 extern int remove(const char *path);
870 #endif
871
872 #ifndef HAVE_RENAME_DECL
873 extern int rename(const char *old, const char *new);
874 #endif
875
876 #ifndef HAVE_REWIND_DECL
877 extern void rewind(FILE *stream);
878 #endif
879
880 #ifndef HAVE_RUSEROK_DECL
881 extern int ruserok(const char *rhost, int suser,
882                       const char *ruser, const char *luser);
883 #endif
884
885 #ifndef HAVE_SELECT_DECL
886 extern int select(int nfds,
887                      SELECT_ARG_TYPE *readfds,
888                      SELECT_ARG_TYPE *writefds,
889                      SELECT_ARG_TYPE *exceptfds,
890                      struct timeval *timeout);
891 #endif
892
893 #ifndef HAVE_SENDTO_DECL
894 extern int sendto(int s, const char *msg, int len, int flags,
895                      const struct sockaddr *to, int tolen);
896 #endif
897
898 #ifdef HAVE_SETRESGID
899 #define setegid(x)      setresgid((gid_t)-1,(x),(gid_t)-1)
900 #ifndef HAVE_SETRESGID_DECL
901 extern int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
902 #endif
903 #else
904 #ifndef HAVE_SETEGID_DECL
905 extern int setegid(gid_t egid);
906 #endif
907 #endif
908
909 #ifdef HAVE_SETRESUID
910 #define seteuid(x)      setresuid((uid_t)-1,(x),(uid_t)-1)
911 #ifndef HAVE_SETRESUID_DECL
912 extern int setresuid(uid_t ruid, uid_t euid, uid_t suid);
913 #endif
914 #else
915 #ifndef HAVE_SETEUID_DECL
916 extern int seteuid(uid_t euid);
917 #endif
918 #endif
919
920 #ifndef HAVE_SETPGID_DECL
921 #ifdef HAVE_SETPGID
922 extern int setpgid(pid_t pid, pid_t pgid);
923 #endif
924 #endif
925
926 #ifndef HAVE_SETPGRP_DECL
927 #ifdef SETPGRP_VOID
928 extern pid_t setpgrp(void);
929 #else
930 extern pid_t setpgrp(pid_t pgrp, pid_t pid);
931 #endif
932 #endif
933
934 #ifndef HAVE_SETSOCKOPT_DECL
935 extern int setsockopt(int s, int level, int optname,
936                          const char *optval, int optlen);
937 #endif
938
939 #ifdef HAVE_SHMGET
940 #ifndef HAVE_SHMAT_DECL
941 extern void *shmat(int shmid, const SHM_ARG_TYPE *shmaddr, int shmflg);
942 #endif
943
944 #ifndef HAVE_SHMCTL_DECL
945 extern int shmctl(int shmid, int cmd, struct shmid_ds *buf);
946 #endif
947
948 #ifndef HAVE_SHMDT_DECL
949 extern int shmdt(SHM_ARG_TYPE *shaddr);
950 #endif
951
952 #ifndef HAVE_SHMGET_DECL
953 extern int shmget(key_t key, size_t size, int shmflg);
954 #endif
955 #endif
956
957 #ifndef HAVE_SNPRINTF_DECL
958 #include "arglist.h"
959 int snprintf(char *buf, size_t len, const char *format,...)
960      G_GNUC_PRINTF(3,4);
961 #endif
962 #ifndef HAVE_VSNPRINTF_DECL
963 #include "arglist.h"
964 int vsnprintf(char *buf, size_t len, const char *format, va_list ap);
965 #endif
966
967 #ifndef HAVE_SOCKET_DECL
968 extern int socket(int domain, int type, int protocol);
969 #endif
970
971 #ifndef HAVE_SOCKETPAIR_DECL
972 extern int socketpair(int domain, int type, int protocol, int sv[2]);
973 #endif
974
975 #ifndef HAVE_SSCANF_DECL
976 extern int sscanf(const char *s, const char *format, ...);
977 #endif
978
979 #ifndef HAVE_STRCASECMP_DECL
980 extern int strcasecmp(const char *s1, const char *s2);
981 #endif
982
983 #ifndef HAVE_STRERROR_DECL
984 extern char *strerror(int errnum);
985 #endif
986
987 #ifndef HAVE_STRFTIME_DECL
988 extern size_t strftime(char *s, size_t maxsize, const char *format,
989                           const struct tm *timeptr);
990 #endif
991
992 #ifndef HAVE_STRNCASECMP_DECL
993 extern int strncasecmp(const char *s1, const char *s2, int n);
994 #endif
995
996 #ifndef HAVE_SYSLOG_DECL
997 extern void syslog(int priority, const char *logstring, ...)
998      G_GNUC_PRINTF(2,3);
999 #endif
1000
1001 #ifndef HAVE_SYSTEM_DECL
1002 extern int system(const char *string);
1003 #endif
1004
1005 #ifndef HAVE_TIME_DECL
1006 extern time_t time(time_t *tloc);
1007 #endif
1008
1009 #ifndef HAVE_TOLOWER_DECL
1010 extern int tolower(int c);
1011 #endif
1012
1013 #ifndef HAVE_TOUPPER_DECL
1014 extern int toupper(int c);
1015 #endif
1016
1017 #ifndef HAVE_UNGETC_DECL
1018 extern int ungetc(int c, FILE *stream);
1019 #endif
1020
1021 #ifndef HAVE_VFPRINTF_DECL
1022 #include "arglist.h"
1023 extern int vfprintf(FILE *stream, const char *format, va_list ap);
1024 #endif
1025
1026 #ifndef HAVE_VPRINTF_DECL
1027 #include "arglist.h"
1028 extern int vprintf(const char *format, va_list ap);
1029 #endif
1030
1031 /* these system headers are added by gnulib if they
1032  * do not exist */
1033 #include "netdb.h"
1034 #include "arpa/inet.h"
1035
1036 /* gnulib-only includes */
1037 #include "safe-read.h"
1038 #include "full-read.h"
1039 #include "full-write.h"
1040
1041 #if !defined(S_ISCHR) && defined(_S_IFCHR) && defined(_S_IFMT)
1042 #define S_ISCHR(mode) (((mode) & _S_IFMT) == _S_IFCHR)
1043 #endif
1044
1045 #if !defined(S_ISREG) && defined(_S_IFREG) && defined(_S_IFMT)
1046 #define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG)
1047 #endif
1048
1049 #ifndef HAVE_WAITPID
1050 #ifdef HAVE_WAIT4
1051 #define waitpid(pid,status,options) wait4(pid,status,options,0)
1052 #else
1053 extern pid_t waitpid(pid_t pid, amwait_t *stat_loc, int options);
1054 #endif
1055 #endif
1056
1057 #ifndef HAVE_WRITEV_DECL
1058 extern ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
1059 #endif
1060
1061 #ifndef STDIN_FILENO
1062 #define STDIN_FILENO 0
1063 #endif
1064
1065 #ifndef STDOUT_FILENO
1066 #define STDOUT_FILENO 1
1067 #endif
1068
1069 #ifndef STDERR_FILENO
1070 #define STDERR_FILENO 2
1071 #endif
1072
1073 /* S_ISDIR is not defined on Nextstep */
1074 #ifndef S_ISDIR
1075 #if defined(_S_IFMT) && defined(_S_IFDIR)
1076 #define S_ISDIR(mode)   (((mode) & (_S_IFMT)) == (_S_IFDIR))
1077 #else
1078 #error Don t know how to define S_ISDIR
1079 #endif
1080 #endif
1081
1082 #if SIZEOF_SIZE_T == SIZEOF_INT
1083 #  define        SIZE_T_ATOI    (size_t)atoi
1084 #  ifndef SIZE_MAX
1085 #    define      SIZE_MAX       G_MAXUINT
1086 #  endif
1087 #else
1088 #  define        SIZE_T_ATOI    (size_t)atol
1089 #  ifndef SIZE_MAX
1090 #    define      SIZE_MAX       ULONG_MAX
1091 #  endif
1092 #endif
1093
1094 #if SIZEOF_SSIZE_T == SIZEOF_INT
1095 #  define        SSIZE_T_ATOI   (ssize_t)atoi
1096 #  ifndef SSIZE_MAX
1097 #    define      SSIZE_MAX      INT_MAX
1098 #  endif
1099 #  ifndef SSIZE_MIN
1100 #    define      SSIZE_MIN      INT_MIN
1101 #  endif
1102 #else
1103 #  define        SSIZE_T_ATOI   (ssize_t)atol
1104 #  ifndef SSIZE_MAX
1105 #    define      SSIZE_MAX      LONG_MAX
1106 #  endif
1107 #  ifndef SSIZE_MIN
1108 #    define      SSIZE_MIN      LONG_MIN
1109 #  endif
1110 #endif
1111
1112 #if SIZEOF_TIME_T == SIZEOF_INT
1113 #  define        TIME_T_ATOI    (time_t)atoi
1114 #  ifndef TIME_MAX
1115 #    define      TIME_MAX       G_MAXUINT
1116 #  endif
1117 #else
1118 #  define        TIME_T_ATOI    (time_t)atol
1119 #  ifndef TIME_MAX
1120 #    define      TIME_MAX       ULONG_MAX
1121 #  endif
1122 #endif
1123
1124 #if SIZEOF_OFF_T > SIZEOF_LONG
1125 #  ifdef HAVE_ATOLL
1126 #    define        OFF_T_ATOI    (off_t)atoll
1127 #  else
1128 #    define        OFF_T_ATOI    (off_t)atol
1129 #  endif
1130 #  ifdef HAVE_STRTOLL
1131 #    define        OFF_T_STRTOL  (off_t)strtoll
1132 #  else
1133 #    define        OFF_T_STRTOL  (off_t)strtol
1134 #  endif
1135 #else
1136 #  if SIZEOF_OFF_T == SIZEOF_LONG
1137 #    define        OFF_T_ATOI    (off_t)atol
1138 #    define        OFF_T_STRTOL  (off_t)strtol
1139 #  else
1140 #    define        OFF_T_ATOI    (off_t)atoi
1141 #    define        OFF_T_STRTOL  (off_t)strtol
1142 #  endif
1143 #endif
1144
1145
1146 #define BIND_CYCLE_RETRIES      120             /* Total of 30 minutes */
1147
1148 #define MAX_DUMPERS 63
1149
1150 #ifndef NI_MAXHOST
1151 #define NI_MAXHOST 1025
1152 #endif
1153
1154 typedef enum {
1155     KENCRYPT_NONE,      /* krb5 encryption not enabled */
1156     KENCRYPT_WILL_DO,   /* krb5 encryption will be enabled once amanda
1157                            protocol stream is closed */
1158     KENCRYPT_YES        /* krb5 encryption enabled on all stream */
1159 } kencrypt_type;
1160
1161 #define DUMP_LEVELS    400
1162
1163 /* Constants to define the number of pre-opened pipes between amandad and
1164  * its services */
1165
1166 /* If you change these (don't!), change them in perl/Amanda/Constants.pm, too */
1167 #define DATA_FD_COUNT   3               /* number of general-use pipes */
1168 #define DATA_FD_OFFSET  50
1169
1170 #endif  /* !AMANDA_H */