ut.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * various general purpose functions
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. */
  19. /** Kamailio core :: various general purpose/utility functions.
  20. * @file ut.c
  21. * @ingroup core
  22. * Module: core
  23. */
  24. #include <sys/types.h>
  25. #include <pwd.h>
  26. #include <grp.h>
  27. #include <stdlib.h>
  28. #include <time.h>
  29. #include <sys/utsname.h> /* uname() */
  30. #include <libgen.h>
  31. #include "ut.h"
  32. #include "mem/mem.h"
  33. #include "globals.h"
  34. /* global buffer for ut.h int2str() */
  35. char ut_buf_int2str[INT2STR_MAX_LEN];
  36. /* converts a username into uid:gid,
  37. * returns -1 on error & 0 on success */
  38. int user2uid(int* uid, int* gid, char* user)
  39. {
  40. char* tmp;
  41. struct passwd *pw_entry;
  42. if (user){
  43. *uid=strtol(user, &tmp, 10);
  44. if ((tmp==0) ||(*tmp)){
  45. /* maybe it's a string */
  46. pw_entry=getpwnam(user);
  47. if (pw_entry==0){
  48. goto error;
  49. }
  50. *uid=pw_entry->pw_uid;
  51. if (gid) *gid=pw_entry->pw_gid;
  52. }
  53. return 0;
  54. }
  55. error:
  56. return -1;
  57. }
  58. /* converts a group name into a gid
  59. * returns -1 on error, 0 on success */
  60. int group2gid(int* gid, char* group)
  61. {
  62. char* tmp;
  63. struct group *gr_entry;
  64. if (group){
  65. *gid=strtol(group, &tmp, 10);
  66. if ((tmp==0) ||(*tmp)){
  67. /* maybe it's a string */
  68. gr_entry=getgrnam(group);
  69. if (gr_entry==0){
  70. goto error;
  71. }
  72. *gid=gr_entry->gr_gid;
  73. }
  74. return 0;
  75. }
  76. error:
  77. return -1;
  78. }
  79. /*
  80. * Replacement of timegm (does not exists on all platforms
  81. * Taken from
  82. * http://lists.samba.org/archive/samba-technical/2002-November/025737.html
  83. */
  84. time_t _timegm(struct tm* t)
  85. {
  86. time_t tl, tb;
  87. struct tm* tg;
  88. t->tm_isdst = 0;
  89. tl = mktime(t);
  90. if (tl == -1) {
  91. t->tm_hour--;
  92. tl = mktime (t);
  93. if (tl == -1) {
  94. return -1; /* can't deal with output from strptime */
  95. }
  96. tl += 3600;
  97. }
  98. tg = gmtime(&tl);
  99. tg->tm_isdst = 0;
  100. tb = mktime(tg);
  101. if (tb == -1) {
  102. tg->tm_hour--;
  103. tb = mktime (tg);
  104. if (tb == -1) {
  105. return -1; /* can't deal with output from gmtime */
  106. }
  107. tb += 3600;
  108. }
  109. return (tl - (tb - tl));
  110. }
  111. /* Convert time_t value that is relative to local timezone to UTC */
  112. time_t local2utc(time_t in)
  113. {
  114. struct tm* tt;
  115. tt = gmtime(&in);
  116. tt->tm_isdst = -1;
  117. return mktime(tt);
  118. }
  119. /* Convert time_t value in UTC to to value relative to local time zone */
  120. time_t utc2local(time_t in)
  121. {
  122. struct tm* tt;
  123. tt = localtime(&in);
  124. #ifdef HAVE_TIMEGM
  125. return timegm(tt);
  126. #else
  127. return _timegm(tt);
  128. #endif
  129. }
  130. /*
  131. * Return str as zero terminated string allocated
  132. * using pkg_malloc
  133. */
  134. char* as_asciiz(str* s)
  135. {
  136. char* r;
  137. r = (char*)pkg_malloc(s->len + 1);
  138. if (!r) {
  139. ERR("Out of memory\n");
  140. return 0;
  141. }
  142. memcpy(r, s->s, s->len);
  143. r[s->len] = '\0';
  144. return r;
  145. }
  146. /* return system version (major.minor.minor2) as
  147. * (major<<16)|(minor)<<8|(minor2)
  148. * (if some of them are missing, they are set to 0)
  149. * if the parameters are not null they are set to the coresp. part
  150. */
  151. unsigned int get_sys_version(int* major, int* minor, int* minor2)
  152. {
  153. struct utsname un;
  154. int m1;
  155. int m2;
  156. int m3;
  157. char* p;
  158. memset (&un, 0, sizeof(un));
  159. m1=m2=m3=0;
  160. /* get sys version */
  161. uname(&un);
  162. m1=strtol(un.release, &p, 10);
  163. if (*p=='.'){
  164. p++;
  165. m2=strtol(p, &p, 10);
  166. if (*p=='.'){
  167. p++;
  168. m3=strtol(p, &p, 10);
  169. }
  170. }
  171. if (major) *major=m1;
  172. if (minor) *minor=m2;
  173. if (minor2) *minor2=m3;
  174. return ((m1<<16)|(m2<<8)|(m3));
  175. }
  176. /** transform a relative pathname into an absolute one.
  177. * @param base - base file, used to extract the absolute path prefix.
  178. * Might be NULL, in which case the path of the ser.cfg is
  179. * used.
  180. * @param file - file path to be transformed. If it's already absolute
  181. * (starts with '/') is left alone. If not the result will
  182. * be `dirname base`/file.
  183. * @return pkg allocated asciiz string or 0 on error.
  184. */
  185. char* get_abs_pathname(str* base, str* file)
  186. {
  187. str ser_cfg;
  188. char* buf, *dir, *res;
  189. int len;
  190. if (base == NULL) {
  191. ser_cfg.s = cfg_file;
  192. ser_cfg.len = strlen(cfg_file);
  193. base = &ser_cfg;
  194. }
  195. if (!base->s || base->len <= 0 || base->s[0] != '/') {
  196. BUG("get_abs_pathname: Base file must be absolute pathname: "
  197. "'%.*s'\n", STR_FMT(base));
  198. return NULL;
  199. }
  200. if (!file || !file->s || file->len <= 0) {
  201. BUG("get_abs_pathname: Invalid 'file' parameter\n");
  202. return NULL;
  203. }
  204. if (file->s[0] == '/') {
  205. /* This is an absolute pathname, make a zero terminated
  206. * copy and use it as it is */
  207. if ((res = pkg_malloc(file->len+1)) == NULL) {
  208. ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
  209. return NULL;
  210. }
  211. memcpy(res, file->s, file->len);
  212. res[file->len]=0;
  213. } else {
  214. /* This is not an absolute pathname, make it relative
  215. * to the location of the base file
  216. */
  217. /* Make a copy, function dirname may modify the string */
  218. if ((buf = pkg_malloc(base->len+1)) == NULL) {
  219. ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
  220. return NULL;
  221. }
  222. memcpy(buf, base->s, base->len);
  223. buf[base->len]=0;
  224. dir = dirname(buf);
  225. len = strlen(dir);
  226. if ((res = pkg_malloc(len + 1 + file->len + 1)) == NULL) {
  227. ERR("get_abs_pathname: No memory left (pkg_malloc failed)\n");
  228. pkg_free(buf);
  229. return NULL;
  230. }
  231. memcpy(res, dir, len);
  232. res[len] = '/';
  233. memcpy(res + len + 1, file->s, file->len);
  234. res[len + 1 + file->len] = '\0';
  235. pkg_free(buf);
  236. }
  237. return res;
  238. }
  239. /**
  240. * @brief search for occurence of needle in text
  241. * @return pointer to start of needle in text or NULL if the needle
  242. * is not found
  243. */
  244. char *str_search(str *text, str *needle)
  245. {
  246. char *p;
  247. if(text==NULL || text->s==NULL || needle==NULL || needle->s==NULL
  248. || text->len<needle->len)
  249. return NULL;
  250. for (p = text->s; p <= text->s + text->len - needle->len; p++) {
  251. if (*p == *needle->s && memcmp(p, needle->s, needle->len)==0) {
  252. return p;
  253. }
  254. }
  255. return NULL;
  256. }
  257. /*
  258. * ser_memmem() returns the location of the first occurrence of data
  259. * pattern b2 of size len2 in memory block b1 of size len1 or
  260. * NULL if none is found. Obtained from NetBSD.
  261. */
  262. void * ser_memmem(const void *b1, const void *b2, size_t len1, size_t len2)
  263. {
  264. /* Initialize search pointer */
  265. char *sp = (char *) b1;
  266. /* Initialize pattern pointer */
  267. char *pp = (char *) b2;
  268. /* Initialize end of search address space pointer */
  269. char *eos = sp + len1 - len2;
  270. /* Sanity check */
  271. if(!(b1 && b2 && len1 && len2))
  272. return NULL;
  273. while (sp <= eos) {
  274. if (*sp == *pp)
  275. if (memcmp(sp, pp, len2) == 0)
  276. return sp;
  277. sp++;
  278. }
  279. return NULL;
  280. }
  281. /*
  282. * ser_memrmem() returns the location of the last occurrence of data
  283. * pattern b2 of size len2 in memory block b1 of size len1 or
  284. * NULL if none is found.
  285. */
  286. void * ser_memrmem(const void *b1, const void *b2, size_t len1, size_t len2)
  287. {
  288. /* Initialize search pointer */
  289. char *sp = (char *) b1 + len1 - len2;
  290. /* Initialize pattern pointer */
  291. char *pp = (char *) b2;
  292. /* Initialize end of search address space pointer */
  293. char *eos = (char *) b1;
  294. /* Sanity check */
  295. if(!(b1 && b2 && len1 && len2))
  296. return NULL;
  297. while (sp >= eos) {
  298. if (*sp == *pp)
  299. if (memcmp(sp, pp, len2) == 0)
  300. return sp;
  301. sp--;
  302. }
  303. return NULL;
  304. }