1 #ifndef NUT_COMMON_H 2 #define NUT_COMMON_H 3 4 /* common.h - prototypes for the common useful functions 5 6 Copyright (C) 2000 Russell Kroll <rkroll@exploits.org> 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 #include "config.h" /* must be the first header */ 24 25 /* Need this on AIX when using xlc to get alloca */ 26 #ifdef _AIX 27 #pragma alloca 28 #endif /* _AIX */ 29 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <stdio.h> 33 #include <stdarg.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <signal.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <syslog.h> 40 #include <unistd.h> 41 #include <assert.h> 42 43 #include "timehead.h" 44 #include "attribute.h" 45 #include "proto.h" 46 #include "str.h" 47 48 #ifdef __cplusplus 49 /* *INDENT-OFF* */ 50 extern "C" { 51 /* *INDENT-ON* */ 52 #endif 53 54 extern const char *UPS_VERSION; 55 56 /* get the syslog ready for us */ 57 void open_syslog(const char *progname); 58 59 /* close ttys and become a daemon */ 60 void background(void); 61 62 /* do this here to keep pwd/grp stuff out of the main files */ 63 struct passwd *get_user_pwent(const char *name); 64 65 /* change to the user defined in the struct */ 66 void become_user(struct passwd *pw); 67 68 /* drop down into a directory and throw away pointers to the old path */ 69 void chroot_start(const char *path); 70 71 /* write a pid file - <name> is a full pathname *or* just the program name */ 72 void writepid(const char *name); 73 74 /* send a signal to another running process */ 75 int sendsignal(const char *progname, int sig); 76 77 int snprintfcat(char *dst, size_t size, const char *fmt, ...) 78 __attribute__ ((__format__ (__printf__, 3, 4))); 79 80 /* open <pidfn>, get the pid, then send it <sig> */ 81 int sendsignalfn(const char *pidfn, int sig); 82 83 const char *xbasename(const char *file); 84 85 /* enable writing upslog_with_errno() and upslogx() type messages to 86 the syslog */ 87 void syslogbit_set(void); 88 89 /* Return the default path for the directory containing configuration files */ 90 const char * confpath(void); 91 92 /* Return the default path for the directory containing state files */ 93 const char * dflt_statepath(void); 94 95 /* Return the alternate path for pid files */ 96 const char * altpidpath(void); 97 98 void upslog_with_errno(int priority, const char *fmt, ...) 99 __attribute__ ((__format__ (__printf__, 2, 3))); 100 void upslogx(int priority, const char *fmt, ...) 101 __attribute__ ((__format__ (__printf__, 2, 3))); 102 void upsdebug_with_errno(int level, const char *fmt, ...) 103 __attribute__ ((__format__ (__printf__, 2, 3))); 104 void upsdebugx(int level, const char *fmt, ...) 105 __attribute__ ((__format__ (__printf__, 2, 3))); 106 void upsdebug_hex(int level, const char *msg, const void *buf, int len); 107 void upsdebug_ascii(int level, const char *msg, const void *buf, int len); 108 109 void fatal_with_errno(int status, const char *fmt, ...) 110 __attribute__ ((__format__ (__printf__, 2, 3))) __attribute__((noreturn)); 111 void fatalx(int status, const char *fmt, ...) 112 __attribute__ ((__format__ (__printf__, 2, 3))) __attribute__((noreturn)); 113 114 extern int nut_debug_level; 115 extern int nut_log_level; 116 117 void *xmalloc(size_t size); 118 void *xcalloc(size_t number, size_t size); 119 void *xrealloc(void *ptr, size_t size); 120 char *xstrdup(const char *string); 121 122 int select_read(const int fd, void *buf, const size_t buflen, const long d_sec, const long d_usec); 123 int select_write(const int fd, const void *buf, const size_t buflen, const long d_sec, const long d_usec); 124 125 /* Buffer sizes used for various functions */ 126 #define SMALLBUF 512 127 #define LARGEBUF 1024 128 129 /* Provide declarations for getopt() global variables */ 130 131 #ifdef NEED_GETOPT_H 132 #include <getopt.h> 133 #else 134 #ifdef NEED_GETOPT_DECLS 135 extern char *optarg; 136 extern int optind; 137 #endif /* NEED_GETOPT_DECLS */ 138 #endif /* HAVE_GETOPT_H */ 139 140 /* logging flags: bitmask! */ 141 142 #define UPSLOG_STDERR 0x0001 143 #define UPSLOG_SYSLOG 0x0002 144 #define UPSLOG_STDERR_ON_FATAL 0x0004 145 #define UPSLOG_SYSLOG_ON_FATAL 0x0008 146 147 #ifndef HAVE_SETEUID 148 # define seteuid(x) setresuid(-1,x,-1) /* Works for HP-UX 10.20 */ 149 # define setegid(x) setresgid(-1,x,-1) /* Works for HP-UX 10.20 */ 150 #endif 151 152 #ifdef __cplusplus 153 /* *INDENT-OFF* */ 154 } 155 /* *INDENT-ON* */ 156 #endif 157 158 #endif /* NUT_COMMON_H */ 159