You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.c 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* This file is part of Netsukuku
  2. * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
  3. *
  4. * This source code is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as published
  6. * by the Free Software Foundation; either version 2 of the License,
  7. * or (at your option) any later version.
  8. *
  9. * This source code is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * Please refer to the GNU Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Public License along with
  15. * this source code; if not, write to:
  16. * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * --
  19. * misc.c: some useful functions.
  20. */
  21. #include "includes.h"
  22. #include <dirent.h>
  23. #include <sys/wait.h>
  24. #include "common.h"
  25. /*
  26. * xor_int
  27. *
  28. * XORs all the bytes of the `i' integer by merging them in a single
  29. * byte. It returns the merged byte.
  30. */
  31. char
  32. xor_int(int i)
  33. {
  34. char c;
  35. c = (i & 0xff) ^ ((i & 0xff00) >> 8) ^ ((i & 0xff0000) >> 16) ^
  36. ((i & 0xff000000) >> 24);
  37. return c;
  38. }
  39. /*\
  40. *
  41. * * * * Swap functions * * * *
  42. *
  43. \*/
  44. /*
  45. * swap_array: swaps the elements of the `src' array and stores the result in
  46. * `dst'. The `src' array has `nmemb'# elements and each of them is `nmemb_sz'
  47. * big.
  48. */
  49. void
  50. swap_array(int nmemb, size_t nmemb_sz, void *src, void *dst)
  51. {
  52. int i, total_sz;
  53. total_sz = nmemb * nmemb_sz;
  54. char buf[total_sz], *z;
  55. if (src == dst)
  56. z = buf;
  57. else
  58. z = dst;
  59. for (i = nmemb - 1; i >= 0; i--)
  60. memcpy(z + (nmemb_sz * (nmemb - i - 1)),
  61. (char *) src + (nmemb_sz * i), nmemb_sz);
  62. if (src == dst)
  63. memcpy(dst, buf, total_sz);
  64. }
  65. /*
  66. * swap_ints: Swap integers.
  67. * It swaps the `x' array which has `nmemb' elements and stores the result it
  68. * in `y'.
  69. */
  70. void
  71. swap_ints(int nmemb, unsigned int *x, unsigned int *y)
  72. {
  73. swap_array(nmemb, sizeof(int), x, y);
  74. }
  75. void
  76. swap_shorts(int nmemb, unsigned short *x, unsigned short *y)
  77. {
  78. swap_array(nmemb, sizeof(short), x, y);
  79. }
  80. /*
  81. * * * * Random related functions * * * *
  82. */
  83. /*
  84. * rand_range: It returns a random number x which is _min <= x <= _max
  85. */
  86. int
  87. rand_range(int _min, int _max)
  88. {
  89. return (rand() % (_max - _min + 1)) + _min;
  90. }
  91. /*
  92. * xsrand
  93. *
  94. * It sets the random seed with a pseudo random number
  95. */
  96. void
  97. xsrand(void)
  98. {
  99. FILE *fd;
  100. int seed;
  101. if ((fd = fopen("/dev/urandom", "r"))) {
  102. fread(&seed, 4, 1, fd);
  103. fclose(fd);
  104. } else
  105. seed = getpid() ^ time(0) ^ clock();
  106. srand(seed);
  107. }
  108. /*
  109. * * * * String functions * * * *
  110. */
  111. char *
  112. last_token(char *string, char tok)
  113. {
  114. while (*string == tok)
  115. string++;
  116. return string;
  117. }
  118. /*
  119. * strip_char: Removes any occurrences of the character `char_to_strip' in the
  120. * string `string'.
  121. */
  122. void
  123. strip_char(char *string, char char_to_strip)
  124. {
  125. int i;
  126. char *p;
  127. for (i = 0; i < strlen(string); i++) {
  128. if (string[i] == char_to_strip) {
  129. p = last_token(&string[i], char_to_strip);
  130. strcpy(&string[i], p);
  131. }
  132. }
  133. }
  134. /*
  135. * * * * Search functions * * * *
  136. */
  137. /*
  138. * split_string: splits the `str' strings at maximum in `max_substrings'#
  139. * substrings using as divisor the `div_str' string.
  140. * Each substring can be at maximum of `max_substring_sz' bytes.
  141. * The array of malloced substrings is returned and in `substrings' the number
  142. * of saved substrings is stored.
  143. * On error 0 is the return value.
  144. */
  145. char **
  146. split_string(char *str, const char *div_str, int *substrings,
  147. int max_substrings, int max_substring_sz)
  148. {
  149. int i = 0, strings = 0, str_len = 0, buf_len;
  150. char *buf, **splitted = 0, *p;
  151. *substrings = 0;
  152. str_len = strlen(str);
  153. buf = str - 1;
  154. while ((buf = strstr((const char *) buf + 1, div_str)))
  155. strings++;
  156. if (!strings && !str_len)
  157. return 0;
  158. strings++;
  159. if (strings > max_substrings)
  160. strings = max_substrings;
  161. splitted = (char **) xmalloc(sizeof(char *) * strings);
  162. buf = str;
  163. for (i = 0; i < strings; i++) {
  164. p = strstr((const char *) buf, div_str);
  165. if (p)
  166. *p = 0;
  167. buf_len = strlen(buf);
  168. if (buf_len <= max_substring_sz && buf_len > 0)
  169. splitted[i] = xstrdup(buf);
  170. else {
  171. i--;
  172. strings--;
  173. buf = p + 1;
  174. }
  175. if (!p) {
  176. i++;
  177. break;
  178. }
  179. buf = p + 1;
  180. }
  181. if (i != strings)
  182. splitted = (char **) xrealloc(splitted, sizeof(char *) * i);
  183. *substrings = strings;
  184. return splitted;
  185. }
  186. /*
  187. * If `x' is present in the `ia' array, which has `nmemb' members, it returns
  188. * 1, otherwise 0.
  189. */
  190. int
  191. find_int(int x, int *ia, int nmemb)
  192. {
  193. int e;
  194. for (e = 0; e < nmemb; e++)
  195. if (ia[e] == x)
  196. return 1;
  197. return 0;
  198. }
  199. /*
  200. * * * * Time functions * * * *
  201. */
  202. /*
  203. * xtimer: It sleeps for `secs' seconds.
  204. * `steps' tells to xtimer() how many "for" cycles it must run to sleep for
  205. * `secs' secons. At each cycle it updates `counter'.
  206. */
  207. void
  208. xtimer(u_int secs, u_int steps, int *counter)
  209. {
  210. static int i, ms_sleep;
  211. static int ms_step; /* how many ms it must sleep at every step */
  212. static int s_step; /* seconds per step */
  213. static int mu_step; /* micro seconds per step */
  214. if (!steps)
  215. steps++;
  216. if (counter)
  217. *counter = 0;
  218. ms_sleep = secs * 1000;
  219. if (ms_sleep < secs) {
  220. /* We are waiting a LONG TIME, it's useless to have steps < of
  221. * one second */
  222. ms_step = 1000;
  223. steps = secs;
  224. } else {
  225. ms_step = ms_sleep / steps;
  226. if (!ms_step) {
  227. ms_step = 1;
  228. steps = ms_sleep;
  229. }
  230. }
  231. s_step = ms_step / 1000;
  232. mu_step = ms_step * 1000;
  233. for (i = 0; i < steps; i++) {
  234. if (ms_step >= 1000)
  235. sleep(s_step);
  236. else
  237. usleep(mu_step);
  238. if (counter)
  239. (*counter)++;
  240. }
  241. }
  242. /*
  243. * * * * File & Dir related functions * * * *
  244. */
  245. /*
  246. * check_and_create_dir: tries to access in the specified `dir' directory and
  247. * if doesn't exist tries to create it.
  248. * On success it returns 0
  249. */
  250. int
  251. check_and_create_dir(char *dir)
  252. {
  253. DIR *d;
  254. /* Try to open the directory */
  255. d = opendir(dir);
  256. if (!d) {
  257. if (errno == ENOENT) {
  258. /* The directory doesn't exist, try to create it */
  259. if (mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
  260. error("Cannot create the %d directory: %s", dir,
  261. strerror(errno));
  262. return -1;
  263. }
  264. } else {
  265. error("Cannot access to the %s directory: %s", dir,
  266. strerror(errno));
  267. return -1;
  268. }
  269. }
  270. closedir(d);
  271. return 0;
  272. }
  273. /*
  274. * file_exist
  275. *
  276. * returns 1 if `filename' is a valid existing file.
  277. */
  278. int
  279. file_exist(char *filename)
  280. {
  281. FILE *fd;
  282. if (!(fd = fopen(filename, "r")))
  283. return !(errno == ENOENT);
  284. fclose(fd);
  285. return 1;
  286. }
  287. /*
  288. * exec_root_script: executes `script' with the given `argv', but checks first if the
  289. * script is:
  290. * - suid
  291. * - it isn't owned by root
  292. * - it isn't writable by others than root
  293. * If one of this conditions is true, the script won't be executed.
  294. * On success 0 is returned.
  295. */
  296. int
  297. exec_root_script(char *script, char *argv)
  298. {
  299. struct stat sh_stat;
  300. int ret;
  301. char command[strlen(script) + strlen(argv) + 2];
  302. if (stat(script, &sh_stat)) {
  303. error("Couldn't stat %s: %s", strerror(errno));
  304. return -1;
  305. }
  306. if (sh_stat.st_uid != 0 || sh_stat.st_mode & S_ISUID ||
  307. sh_stat.st_mode & S_ISGID ||
  308. (sh_stat.st_gid != 0 && sh_stat.st_mode & S_IWGRP) ||
  309. sh_stat.st_mode & S_IWOTH) {
  310. error("Please adjust the permissions of %s and be sure it "
  311. "hasn't been modified.\n"
  312. " Use this command:\n"
  313. " chmod 744 %s; chown root:root %s",
  314. script, script, script);
  315. return -1;
  316. }
  317. sprintf(command, "%s %s", script, argv);
  318. loginfo("Executing \"%s\"", command);
  319. ret = system(command);
  320. if (ret == -1) {
  321. error("Couldn't execute %s: %s", script, strerror(errno));
  322. return -1;
  323. }
  324. if (!WIFEXITED(ret) || (WIFEXITED(ret) && WEXITSTATUS(ret) != 0)) {
  325. error("\"%s\" didn't terminate correctly", command);
  326. return -1;
  327. }
  328. return 0;
  329. }
  330. /* This is the most important function */
  331. void
  332. do_nothing(void)
  333. {
  334. return;
  335. }