PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/amanda/tags/amanda260p2/common-src/amanda.h

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