rotater.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * This file is part of the zlog Library.
  3. *
  4. * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING in base directory.
  7. */
  8. #include <string.h>
  9. #include <glob.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include <pthread.h>
  18. #include "zc_defs.h"
  19. #include "rotater.h"
  20. #define ROLLING 1 /* aa.02->aa.03, aa.01->aa.02, aa->aa.01 */
  21. #define SEQUENCE 2 /* aa->aa.03 */
  22. typedef struct {
  23. int index;
  24. char path[MAXLEN_PATH + 1];
  25. } zlog_file_t;
  26. void zlog_rotater_profile(zlog_rotater_t * a_rotater, int flag)
  27. {
  28. zc_assert(a_rotater,);
  29. zc_profile(flag, "--rotater[%p][%p,%s,%d][%s,%s,%s,%ld,%ld,%d,%d,%d]--",
  30. a_rotater,
  31. &(a_rotater->lock_mutex),
  32. a_rotater->lock_file,
  33. a_rotater->lock_fd,
  34. a_rotater->base_path,
  35. a_rotater->archive_path,
  36. a_rotater->glob_path,
  37. (long)a_rotater->num_start_len,
  38. (long)a_rotater->num_end_len,
  39. a_rotater->num_width,
  40. a_rotater->mv_type,
  41. a_rotater->max_count
  42. );
  43. if (a_rotater->files) {
  44. int i;
  45. zlog_file_t *a_file;
  46. zc_arraylist_foreach(a_rotater->files, i, a_file) {
  47. zc_profile(flag, "[%s,%d]->", a_file->path, a_file->index);
  48. }
  49. }
  50. return;
  51. }
  52. /*******************************************************************************/
  53. void zlog_rotater_del(zlog_rotater_t *a_rotater)
  54. {
  55. zc_assert(a_rotater,);
  56. if (a_rotater->lock_fd) {
  57. if (close(a_rotater->lock_fd)) {
  58. zc_error("close fail, errno[%d]", errno);
  59. }
  60. }
  61. if (pthread_mutex_destroy(&(a_rotater->lock_mutex))) {
  62. zc_error("pthread_mutex_destroy fail, errno[%d]", errno);
  63. }
  64. zc_debug("zlog_rotater_del[%p]", a_rotater);
  65. free(a_rotater);
  66. return;
  67. }
  68. zlog_rotater_t *zlog_rotater_new(char *lock_file)
  69. {
  70. int fd = 0;
  71. zlog_rotater_t *a_rotater;
  72. zc_assert(lock_file, NULL);
  73. a_rotater = calloc(1, sizeof(zlog_rotater_t));
  74. if (!a_rotater) {
  75. zc_error("calloc fail, errno[%d]", errno);
  76. return NULL;
  77. }
  78. if (pthread_mutex_init(&(a_rotater->lock_mutex), NULL)) {
  79. zc_error("pthread_mutex_init fail, errno[%d]", errno);
  80. free(a_rotater);
  81. return NULL;
  82. }
  83. /* depends on umask of the user here
  84. * if user A create /tmp/zlog.lock 0600
  85. * user B is unable to read /tmp/zlog.lock
  86. * B has to choose another lock file except /tmp/zlog.lock
  87. */
  88. fd = open(lock_file, O_RDWR | O_CREAT,
  89. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  90. if (fd < 0) {
  91. zc_error("open file[%s] fail, errno[%d]", lock_file, errno);
  92. goto err;
  93. }
  94. a_rotater->lock_fd = fd;
  95. a_rotater->lock_file = lock_file;
  96. //zlog_rotater_profile(a_rotater, ZC_DEBUG);
  97. return a_rotater;
  98. err:
  99. zlog_rotater_del(a_rotater);
  100. return NULL;
  101. }
  102. /*******************************************************************************/
  103. static void zlog_file_del(zlog_file_t * a_file)
  104. {
  105. zc_debug("del onefile[%p]", a_file);
  106. zc_debug("a_file->path[%s]", a_file->path);
  107. free(a_file);
  108. }
  109. static zlog_file_t *zlog_file_check_new(zlog_rotater_t * a_rotater, const char *path)
  110. {
  111. int nwrite;
  112. int nread;
  113. zlog_file_t *a_file;
  114. /* base_path will not be in list */
  115. if (STRCMP(a_rotater->base_path, ==, path)) {
  116. return NULL;
  117. }
  118. /* omit dirs */
  119. if ((path)[strlen(path) - 1] == '/') {
  120. return NULL;
  121. }
  122. a_file = calloc(1, sizeof(zlog_file_t));
  123. if (!a_file) {
  124. zc_error("calloc fail, errno[%d]", errno);
  125. return NULL;
  126. }
  127. nwrite = snprintf(a_file->path, sizeof(a_file->path), "%s", path);
  128. if (nwrite < 0 || nwrite >= sizeof(a_file->path)) {
  129. zc_error("snprintf fail or overflow, nwrite=[%d], errno[%d]", nwrite, errno);
  130. goto err;
  131. }
  132. nread = 0;
  133. sscanf(a_file->path + a_rotater->num_start_len, "%d%n", &(a_file->index), &(nread));
  134. if (a_rotater->num_width != 0) {
  135. if (nread < a_rotater->num_width) {
  136. zc_warn("aa.1.log is not expect, need aa.01.log");
  137. goto err;
  138. }
  139. } /* else all file is ok */
  140. return a_file;
  141. err:
  142. free(a_file);
  143. return NULL;
  144. }
  145. static int zlog_file_cmp(zlog_file_t * a_file_1, zlog_file_t * a_file_2)
  146. {
  147. return (a_file_1->index > a_file_2->index);
  148. }
  149. static int zlog_rotater_add_archive_files(zlog_rotater_t * a_rotater)
  150. {
  151. int rc = 0;
  152. glob_t glob_buf;
  153. size_t pathc;
  154. char **pathv;
  155. zlog_file_t *a_file;
  156. a_rotater->files = zc_arraylist_new((zc_arraylist_del_fn)zlog_file_del);
  157. if (!a_rotater->files) {
  158. zc_error("zc_arraylist_new fail");
  159. return -1;
  160. }
  161. /* scan file which is aa.*.log and aa */
  162. rc = glob(a_rotater->glob_path, GLOB_ERR | GLOB_MARK | GLOB_NOSORT, NULL, &glob_buf);
  163. if (rc == GLOB_NOMATCH) {
  164. goto exit;
  165. } else if (rc) {
  166. zc_error("glob err, rc=[%d], errno[%d]", rc, errno);
  167. return -1;
  168. }
  169. pathv = glob_buf.gl_pathv;
  170. pathc = glob_buf.gl_pathc;
  171. /* check and find match aa.[0-9]*.log, depend on num_width */
  172. for (; pathc-- > 0; pathv++) {
  173. a_file = zlog_file_check_new(a_rotater, *pathv);
  174. if (!a_file) {
  175. zc_warn("not the expect pattern file");
  176. continue;
  177. }
  178. /* file in list aa.00, aa.01, aa.02... */
  179. rc = zc_arraylist_sortadd(a_rotater->files,
  180. (zc_arraylist_cmp_fn)zlog_file_cmp, a_file);
  181. if (rc) {
  182. zc_error("zc_arraylist_sortadd fail");
  183. goto err;
  184. }
  185. }
  186. exit:
  187. globfree(&glob_buf);
  188. return 0;
  189. err:
  190. globfree(&glob_buf);
  191. return -1;
  192. }
  193. static int zlog_rotater_seq_files(zlog_rotater_t * a_rotater)
  194. {
  195. int rc = 0;
  196. int nwrite = 0;
  197. int i, j;
  198. zlog_file_t *a_file;
  199. char new_path[MAXLEN_PATH + 1];
  200. zc_arraylist_foreach(a_rotater->files, i, a_file) {
  201. if (a_rotater->max_count > 0
  202. && i < zc_arraylist_len(a_rotater->files) - a_rotater->max_count) {
  203. /* unlink aa.0 aa.1 .. aa.(n-c) */
  204. rc = unlink(a_file->path);
  205. if (rc) {
  206. zc_error("unlink[%s] fail, errno[%d]",a_file->path , errno);
  207. return -1;
  208. }
  209. continue;
  210. }
  211. }
  212. if (zc_arraylist_len(a_rotater->files) > 0) { /* list is not empty */
  213. a_file = zc_arraylist_get(a_rotater->files, zc_arraylist_len(a_rotater->files)-1);
  214. if (!a_file) {
  215. zc_error("zc_arraylist_get fail");
  216. return -1;
  217. }
  218. j = zc_max(zc_arraylist_len(a_rotater->files)-1, a_file->index) + 1;
  219. } else {
  220. j = 0;
  221. }
  222. /* do the base_path mv */
  223. memset(new_path, 0x00, sizeof(new_path));
  224. nwrite = snprintf(new_path, sizeof(new_path), "%.*s%0*d%s",
  225. (int) a_rotater->num_start_len, a_rotater->glob_path,
  226. a_rotater->num_width, j,
  227. a_rotater->glob_path + a_rotater->num_end_len);
  228. if (nwrite < 0 || nwrite >= sizeof(new_path)) {
  229. zc_error("nwirte[%d], overflow or errno[%d]", nwrite, errno);
  230. return -1;
  231. }
  232. if (rename(a_rotater->base_path, new_path)) {
  233. zc_error("rename[%s]->[%s] fail, errno[%d]", a_rotater->base_path, new_path, errno);
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. static int zlog_rotater_roll_files(zlog_rotater_t * a_rotater)
  239. {
  240. int i;
  241. int rc = 0;
  242. int nwrite;
  243. char new_path[MAXLEN_PATH + 1];
  244. zlog_file_t *a_file;
  245. /* now in the list, aa.0 aa.1 aa.2 aa.02... */
  246. for (i = zc_arraylist_len(a_rotater->files) - 1; i > -1; i--) {
  247. a_file = zc_arraylist_get(a_rotater->files, i);
  248. if (!a_file) {
  249. zc_error("zc_arraylist_get fail");
  250. return -1;
  251. }
  252. if (a_rotater->max_count > 0 && i >= a_rotater->max_count - 1) {
  253. /* remove file.3 >= 3*/
  254. rc = unlink(a_file->path);
  255. if (rc) {
  256. zc_error("unlink[%s] fail, errno[%d]",a_file->path , errno);
  257. return -1;
  258. }
  259. continue;
  260. }
  261. /* begin rename aa.01.log -> aa.02.log , using i, as index in list maybe repeat */
  262. memset(new_path, 0x00, sizeof(new_path));
  263. nwrite = snprintf(new_path, sizeof(new_path), "%.*s%0*d%s",
  264. (int) a_rotater->num_start_len, a_rotater->glob_path,
  265. a_rotater->num_width, i + 1,
  266. a_rotater->glob_path + a_rotater->num_end_len);
  267. if (nwrite < 0 || nwrite >= sizeof(new_path)) {
  268. zc_error("nwirte[%d], overflow or errno[%d]", nwrite, errno);
  269. return -1;
  270. }
  271. if (rename(a_file->path, new_path)) {
  272. zc_error("rename[%s]->[%s] fail, errno[%d]", a_file->path, new_path, errno);
  273. return -1;
  274. }
  275. }
  276. /* do the base_path mv */
  277. memset(new_path, 0x00, sizeof(new_path));
  278. nwrite = snprintf(new_path, sizeof(new_path), "%.*s%0*d%s",
  279. (int) a_rotater->num_start_len, a_rotater->glob_path,
  280. a_rotater->num_width, 0,
  281. a_rotater->glob_path + a_rotater->num_end_len);
  282. if (nwrite < 0 || nwrite >= sizeof(new_path)) {
  283. zc_error("nwirte[%d], overflow or errno[%d]", nwrite, errno);
  284. return -1;
  285. }
  286. if (rename(a_rotater->base_path, new_path)) {
  287. zc_error("rename[%s]->[%s] fail, errno[%d]", a_rotater->base_path, new_path, errno);
  288. return -1;
  289. }
  290. return 0;
  291. }
  292. static int zlog_rotater_parse_archive_path(zlog_rotater_t * a_rotater)
  293. {
  294. int nwrite;
  295. int nread;
  296. char *p;
  297. size_t len;
  298. /* no archive path is set */
  299. if (a_rotater->archive_path[0] == '\0') {
  300. nwrite = snprintf(a_rotater->glob_path, sizeof(a_rotater->glob_path),
  301. "%s.*", a_rotater->base_path);
  302. if (nwrite < 0 || nwrite > sizeof(a_rotater->glob_path)) {
  303. zc_error("nwirte[%d], overflow or errno[%d]", nwrite, errno);
  304. return -1;
  305. }
  306. a_rotater->mv_type = ROLLING;
  307. a_rotater->num_width = 0;
  308. a_rotater->num_start_len = strlen(a_rotater->base_path) + 1;
  309. a_rotater->num_end_len = strlen(a_rotater->base_path) + 2;
  310. return 0;
  311. } else {
  312. /* find the 1st # */
  313. p = strchr(a_rotater->archive_path, '#');
  314. if (!p) {
  315. zc_error("no # in archive_path[%s]", a_rotater->archive_path);
  316. return -1;
  317. }
  318. nread = 0;
  319. sscanf(p, "#%d%n", &(a_rotater->num_width), &nread);
  320. if (nread == 0) nread = 1;
  321. if (*(p+nread) == 'r') {
  322. a_rotater->mv_type = ROLLING;
  323. } else if (*(p+nread) == 's') {
  324. a_rotater->mv_type = SEQUENCE;
  325. } else {
  326. zc_error("#r or #s not found");
  327. return -1;
  328. }
  329. /* copy and substitue #i to * in glob_path*/
  330. len = p - a_rotater->archive_path;
  331. if (len > sizeof(a_rotater->glob_path) - 1) {
  332. zc_error("sizeof glob_path not enough,len[%ld]", (long) len);
  333. return -1;
  334. }
  335. memcpy(a_rotater->glob_path, a_rotater->archive_path, len);
  336. nwrite = snprintf(a_rotater->glob_path + len, sizeof(a_rotater->glob_path) - len,
  337. "*%s", p + nread + 1);
  338. if (nwrite < 0 || nwrite > sizeof(a_rotater->glob_path) - len) {
  339. zc_error("nwirte[%d], overflow or errno[%d]", nwrite, errno);
  340. return -1;
  341. }
  342. a_rotater->num_start_len = len;
  343. a_rotater->num_end_len = len + 1;
  344. }
  345. return 0;
  346. }
  347. static void zlog_rotater_clean(zlog_rotater_t *a_rotater)
  348. {
  349. a_rotater->base_path = NULL;
  350. a_rotater->archive_path = NULL;
  351. a_rotater->max_count = 0;
  352. a_rotater->mv_type = 0;
  353. a_rotater->num_width = 0;
  354. a_rotater->num_start_len = 0;
  355. a_rotater->num_end_len = 0;
  356. memset(a_rotater->glob_path, 0x00, sizeof(a_rotater->glob_path));
  357. if (a_rotater->files) zc_arraylist_del(a_rotater->files);
  358. a_rotater->files = NULL;
  359. }
  360. static int zlog_rotater_lsmv(zlog_rotater_t *a_rotater,
  361. char *base_path, char *archive_path, int archive_max_count)
  362. {
  363. int rc = 0;
  364. a_rotater->base_path = base_path;
  365. a_rotater->archive_path = archive_path;
  366. a_rotater->max_count = archive_max_count;
  367. rc = zlog_rotater_parse_archive_path(a_rotater);
  368. if (rc) {
  369. zc_error("zlog_rotater_parse_archive_path fail");
  370. goto err;
  371. }
  372. rc = zlog_rotater_add_archive_files(a_rotater);
  373. if (rc) {
  374. zc_error("zlog_rotater_add_archive_files fail");
  375. goto err;
  376. }
  377. if (a_rotater->mv_type == ROLLING) {
  378. rc = zlog_rotater_roll_files(a_rotater);
  379. if (rc) {
  380. zc_error("zlog_rotater_roll_files fail");
  381. goto err;
  382. }
  383. } else if (a_rotater->mv_type == SEQUENCE) {
  384. rc = zlog_rotater_seq_files(a_rotater);
  385. if (rc) {
  386. zc_error("zlog_rotater_seq_files fail");
  387. goto err;
  388. }
  389. }
  390. zlog_rotater_clean(a_rotater);
  391. return 0;
  392. err:
  393. zlog_rotater_clean(a_rotater);
  394. return -1;
  395. }
  396. /*******************************************************************************/
  397. static int zlog_rotater_trylock(zlog_rotater_t *a_rotater)
  398. {
  399. int rc;
  400. struct flock fl;
  401. fl.l_type = F_WRLCK;
  402. fl.l_start = 0;
  403. fl.l_whence = SEEK_SET;
  404. fl.l_len = 0;
  405. rc = pthread_mutex_trylock(&(a_rotater->lock_mutex));
  406. if (rc == EBUSY) {
  407. zc_warn("pthread_mutex_trylock fail, as lock_mutex is locked by other threads");
  408. return -1;
  409. } else if (rc != 0) {
  410. zc_error("pthread_mutex_trylock fail, rc[%d]", rc);
  411. return -1;
  412. }
  413. if (fcntl(a_rotater->lock_fd, F_SETLK, &fl)) {
  414. if (errno == EAGAIN || errno == EACCES) {
  415. /* lock by other process, that's right, go on */
  416. /* EAGAIN on linux */
  417. /* EACCES on AIX */
  418. zc_warn("fcntl lock fail, as file is lock by other process");
  419. } else {
  420. zc_error("lock fd[%d] fail, errno[%d]", a_rotater->lock_fd, errno);
  421. }
  422. if (pthread_mutex_unlock(&(a_rotater->lock_mutex))) {
  423. zc_error("pthread_mutex_unlock fail, errno[%d]", errno);
  424. }
  425. return -1;
  426. }
  427. return 0;
  428. }
  429. static int zlog_rotater_unlock(zlog_rotater_t *a_rotater)
  430. {
  431. int rc = 0;
  432. struct flock fl;
  433. fl.l_type = F_UNLCK;
  434. fl.l_start = 0;
  435. fl.l_whence = SEEK_SET;
  436. fl.l_len = 0;
  437. if (fcntl(a_rotater->lock_fd, F_SETLK, &fl)) {
  438. rc = -1;
  439. zc_error("unlock fd[%s] fail, errno[%d]", a_rotater->lock_fd, errno);
  440. }
  441. if (pthread_mutex_unlock(&(a_rotater->lock_mutex))) {
  442. rc = -1;
  443. zc_error("pthread_mutext_unlock fail, errno[%d]", errno);
  444. }
  445. return rc;
  446. }
  447. int zlog_rotater_rotate(zlog_rotater_t *a_rotater,
  448. char *base_path, size_t msg_len,
  449. char *archive_path, long archive_max_size, int archive_max_count)
  450. {
  451. int rc = 0;
  452. struct zlog_stat info;
  453. zc_assert(base_path, -1);
  454. if (zlog_rotater_trylock(a_rotater)) {
  455. zc_warn("zlog_rotater_trylock fail, maybe lock by other process or threads");
  456. return 0;
  457. }
  458. if (stat(base_path, &info)) {
  459. rc = -1;
  460. zc_error("stat [%s] fail, errno[%d]", base_path, errno);
  461. goto exit;
  462. }
  463. if (info.st_size + msg_len <= archive_max_size) {
  464. /* file not so big,
  465. * may alread rotate by oth process or thread,
  466. * return */
  467. rc = 0;
  468. goto exit;
  469. }
  470. /* begin list and move files */
  471. rc = zlog_rotater_lsmv(a_rotater, base_path, archive_path, archive_max_count);
  472. if (rc) {
  473. zc_error("zlog_rotater_lsmv [%s] fail, return", base_path);
  474. rc = -1;
  475. } /* else if (rc == 0) */
  476. //zc_debug("zlog_rotater_file_ls_mv success");
  477. exit:
  478. /* unlock file */
  479. if (zlog_rotater_unlock(a_rotater)) {
  480. zc_error("zlog_rotater_unlock fail");
  481. }
  482. return rc;
  483. }
  484. /*******************************************************************************/