1 /* common.h - prototypes for the common useful functions 2 3 Copyright (C) 2000 Russell Kroll <rkroll@exploits.org> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 */ 19 20 #ifndef NUT_COMMON_H_SEEN 21 #define NUT_COMMON_H_SEEN 1 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 /* Use in code to notify the developers and quiesce the compiler that 57 * (for this codepath) the argument or variable is unused intentionally. 58 * void f(int x) { 59 * NUT_UNUSED_VARIABLE(x); 60 * ... 61 * } 62 * 63 * Note that solutions which mark up function arguments or employ this or 64 * that __attribute__ proved not portable enough for wherever NUT builds. 65 */ 66 #define NUT_UNUSED_VARIABLE(x) (void)(x) 67 68 /** @brief Default timeout (in seconds) for network operations, as used by `upsclient` and `nut-scanner`. */ 69 #define DEFAULT_NETWORK_TIMEOUT 5 70 71 /** @brief Default timeout (in seconds) for retrieving the result of a `TRACKING`-enabled operation (e.g. `INSTCMD`, `SET VAR`). */ 72 #define DEFAULT_TRACKING_TIMEOUT 10 73 74 /* get the syslog ready for us */ 75 void open_syslog(const char *progname); 76 77 /* close ttys and become a daemon */ 78 void background(void); 79 80 /* do this here to keep pwd/grp stuff out of the main files */ 81 struct passwd *get_user_pwent(const char *name); 82 83 /* change to the user defined in the struct */ 84 void become_user(struct passwd *pw); 85 86 /* drop down into a directory and throw away pointers to the old path */ 87 void chroot_start(const char *path); 88 89 /* write a pid file - <name> is a full pathname *or* just the program name */ 90 void writepid(const char *name); 91 92 /* send a signal to another running process */ 93 int sendsignal(const char *progname, int sig); 94 95 int snprintfcat(char *dst, size_t size, const char *fmt, ...) 96 __attribute__ ((__format__ (__printf__, 3, 4))); 97 98 /* Report maximum platform value for the pid_t */ 99 pid_t get_max_pid_t(void); 100 101 /* open <pidfn>, get the pid, then send it <sig> */ 102 int sendsignalfn(const char *pidfn, int sig); 103 104 const char *xbasename(const char *file); 105 106 /* enable writing upslog_with_errno() and upslogx() type messages to 107 the syslog */ 108 void syslogbit_set(void); 109 110 /* Return the default path for the directory containing configuration files */ 111 const char * confpath(void); 112 113 /* Return the default path for the directory containing state files */ 114 const char * dflt_statepath(void); 115 116 /* Return the alternate path for pid files */ 117 const char * altpidpath(void); 118 119 /* upslog*() messages are sent to syslog always; 120 * their life after that is out of NUT's control */ 121 void upslog_with_errno(int priority, const char *fmt, ...) 122 __attribute__ ((__format__ (__printf__, 2, 3))); 123 void upslogx(int priority, const char *fmt, ...) 124 __attribute__ ((__format__ (__printf__, 2, 3))); 125 126 /* upsdebug*() messages are only logged if debugging 127 * level is high enough. To speed up a bit (minimize 128 * passing of ultimately ignored data trough the stack) 129 * these are "hidden" implementations wrapped into 130 * macros for earlier routine names spread around the 131 * codebase, they would check debug level first and 132 * only if logging should happen - call the routine 133 * and pass around pointers and other data. 134 */ 135 void s_upsdebug_with_errno(int level, const char *fmt, ...) 136 __attribute__ ((__format__ (__printf__, 2, 3))); 137 void s_upsdebugx(int level, const char *fmt, ...) 138 __attribute__ ((__format__ (__printf__, 2, 3))); 139 void s_upsdebug_hex(int level, const char *msg, const void *buf, size_t len); 140 void s_upsdebug_ascii(int level, const char *msg, const void *buf, size_t len); 141 /* These macros should help avoid run-time overheads 142 * passing data for messages nobody would ever see. 143 * 144 * Also NOTE: the "level" may be specified by callers in various ways, 145 * e.g. as a "X ? Y : Z" style expression; to catch those expansions 146 * transparently we hide them into parentheses as "(label)". 147 * 148 * For stricter C99 compatibility, all parameters specified to a macro 149 * must be populated by caller (so we do not handle "fmt, args..." where 150 * the args part may be skipped by caller because fmt is a fixed string). 151 * Note it is then up to the caller (and compiler checks) that at least 152 * one argument is provided, the format string (maybe fixed) -- as would 153 * be required by the actual s_upsdebugx*() method after macro evaluation. 154 */ 155 #define upsdebug_with_errno(level, ...) \ 156 do { if (nut_debug_level >= (level)) { s_upsdebug_with_errno((level), __VA_ARGS__); } } while(0) 157 #define upsdebugx(level, ...) \ 158 do { if (nut_debug_level >= (level)) { s_upsdebugx((level), __VA_ARGS__); } } while(0) 159 #define upsdebug_hex(level, msg, buf, len) \ 160 do { if (nut_debug_level >= (level)) { s_upsdebug_hex((level), msg, buf, len); } } while(0) 161 #define upsdebug_ascii(level, msg, buf, len) \ 162 do { if (nut_debug_level >= (level)) { s_upsdebug_ascii((level), msg, buf, len); } } while(0) 163 164 void fatal_with_errno(int status, const char *fmt, ...) 165 __attribute__ ((__format__ (__printf__, 2, 3))) __attribute__((noreturn)); 166 void fatalx(int status, const char *fmt, ...) 167 __attribute__ ((__format__ (__printf__, 2, 3))) __attribute__((noreturn)); 168 169 extern int nut_debug_level; 170 extern int nut_log_level; 171 172 void *xmalloc(size_t size); 173 void *xcalloc(size_t number, size_t size); 174 void *xrealloc(void *ptr, size_t size); 175 char *xstrdup(const char *string); 176 177 ssize_t select_read(const int fd, void *buf, const size_t buflen, const time_t d_sec, const suseconds_t d_usec); 178 ssize_t select_write(const int fd, const void *buf, const size_t buflen, const time_t d_sec, const suseconds_t d_usec); 179 180 char * get_libname(const char* base_libname); 181 182 /* Buffer sizes used for various functions */ 183 #define SMALLBUF 512 184 #define LARGEBUF 1024 185 186 /** @brief (Minimum) Size that a string must have to hold a UUID4 (i.e. UUID4 length + the terminating null character). */ 187 #define UUID4_LEN 37 188 189 /* Provide declarations for getopt() global variables */ 190 191 #ifdef NEED_GETOPT_H 192 #include <getopt.h> 193 #else 194 #ifdef NEED_GETOPT_DECLS 195 extern char *optarg; 196 extern int optind; 197 #endif /* NEED_GETOPT_DECLS */ 198 #endif /* HAVE_GETOPT_H */ 199 200 /* logging flags: bitmask! */ 201 202 #define UPSLOG_STDERR 0x0001 203 #define UPSLOG_SYSLOG 0x0002 204 #define UPSLOG_STDERR_ON_FATAL 0x0004 205 #define UPSLOG_SYSLOG_ON_FATAL 0x0008 206 207 #ifndef HAVE_SETEUID 208 # define seteuid(x) setresuid(-1,x,-1) /* Works for HP-UX 10.20 */ 209 # define setegid(x) setresgid(-1,x,-1) /* Works for HP-UX 10.20 */ 210 #endif 211 212 #ifdef __cplusplus 213 /* *INDENT-OFF* */ 214 } 215 /* *INDENT-ON* */ 216 #endif 217 218 #endif /* NUT_COMMON_H_SEEN */ 219