sys (3554B)
1 #ifndef _SYS_ 2 #define _SYS_ 3 4 /* System-wide includes, 'cuz I'm too lazy to repeat them throughout 5 * the entire source tree. 6 */ 7 8 // C 9 #include <errno.h> 10 #include <fcntl.h> 11 #ifdef HAVE_GETOPT_H 12 #include <getopt.h> 13 #endif 14 #include <netdb.h> 15 #include <regex.h> 16 #include <signal.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 #include <pthread.h> 21 #include <unistd.h> 22 #include <arpa/inet.h> 23 #include <netinet/in.h> 24 #include <sys/resource.h> 25 #include <sys/select.h> 26 #include <sys/socket.h> 27 #include <sys/time.h> 28 #include <sys/types.h> 29 #include <sys/wait.h> 30 31 #ifdef INADDR_NONE 32 # define HAVE_INADDR_NONE 33 #else 34 # define INADDR_NONE 0xffffffff 35 #endif 36 37 // C++ 38 #include <exception> 39 #include <iostream> 40 #include <map> 41 #include <sstream> 42 #include <string> 43 #include <vector> 44 #include <queue> 45 46 /* When the system goes out of open files, we take a nap for X secs. Set 47 * to 0 to suppress this. */ 48 #define EMFILE_SLEEP 3 49 50 /* Profiling support on/off */ 51 #ifdef PROFILER 52 # define PROFILE(x) Profiler local_prof(x) 53 #else 54 # define PROFILE(x) 55 #endif 56 57 /* Memory debugging on/off */ 58 #ifdef MEMDEBUG 59 # define MEM(x) x 60 #else 61 # define MEM(x) 62 #endif 63 64 /* If you fear that your malloc() / realloc() may have threading problems, 65 * uncomment the following. It will cause mutex locks around the calls. */ 66 // #define MISTRUST_MALLOC_THREADSAFE 67 68 /* If you fear racing conditions in thread_create() then uncomment this. 69 * BTW it would be really weird if thread_create() weren't thread-safe., so 70 * defining this is very likely not necessary. */ 71 // #define MISTRUST_THREAD_CREATE_THREADSAFE 72 73 using namespace std; 74 75 // This is for the Servertype of serversocket() 76 #include "servertype/servertype" 77 78 79 /* Generic funtions */ 80 string inet2string(struct in_addr in); 81 bool ipmatch (struct in_addr addr, struct in_addr mask); 82 vector<string> str2parts (string const &s, char sep); 83 void mt_srand(unsigned long s); 84 unsigned long mt_rand(void); 85 bool check_acl(string const &ipstr, struct in_addr ipaddr); 86 int sysrun (string const &s); 87 int maxtimeout(int a, int b); 88 string base64_decode(string const &s); 89 90 void mutex_lock(void *obj); 91 void mutex_unlock(void *obj); 92 bool mutex_lock_cout(); 93 void mutex_unlock_cout(); 94 bool mutex_lock_cerr(); 95 void mutex_unlock_cerr(); 96 97 #ifndef HAVE_INET_ATON 98 int inet_aton (char const *name, struct in_addr *addr); 99 #endif 100 101 #ifndef HAVE_STRNSTR 102 char *strnstr (char const *s, char const *find, size_t slen); 103 #endif 104 105 /* Messaging. Conditionals are defined as a macro to speed things up. */ 106 #define msg(x) \ 107 if (config.verbose() && mutex_lock_cout()) { \ 108 if (config.prefixtimestamp()) { \ 109 Timestamp tm; \ 110 cout << tm.desc() << ' '; \ 111 } \ 112 cout << pthread_self() << " INFO: " << x; \ 113 cout.flush(); \ 114 mutex_unlock_cout(); \ 115 } 116 #define debugmsg(x) \ 117 if (config.debug() && mutex_lock_cout()) { \ 118 if (config.prefixtimestamp()) { \ 119 Timestamp tm; \ 120 cout << tm.desc() << ' '; \ 121 } \ 122 cout << pthread_self() << " DEBUG: " << x; \ 123 cout.flush(); \ 124 mutex_unlock_cout(); \ 125 } 126 #define reportmsg(x) \ 127 if (mutex_lock_cerr()) { \ 128 if (config.prefixtimestamp()) { \ 129 Timestamp tm; \ 130 cerr << tm.desc() << ' '; \ 131 } \ 132 cerr << pthread_self() << " REPORT " << x; \ 133 mutex_unlock_cerr(); \ 134 } 135 #define warnmsg(x) \ 136 if (mutex_lock_cerr()) { \ 137 if (config.prefixtimestamp()) { \ 138 Timestamp tm; \ 139 cerr << tm.desc() << ' '; \ 140 } \ 141 cerr << pthread_self() << " WARNING " << x; \ 142 mutex_unlock_cerr(); \ 143 } 144 145 #endif 146