logread.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * circular buffer syslog implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
  6. *
  7. * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  10. */
  11. #include "libbb.h"
  12. #include <sys/ipc.h>
  13. #include <sys/sem.h>
  14. #include <sys/shm.h>
  15. #define DEBUG 0
  16. /* our shared key (syslogd.c and logread.c must be in sync) */
  17. enum { KEY_ID = 0x414e4547 }; /* "GENA" */
  18. struct shbuf_ds {
  19. int32_t size; // size of data - 1
  20. int32_t tail; // end of message list
  21. char data[1]; // messages
  22. };
  23. static const struct sembuf init_sem[3] = {
  24. {0, -1, IPC_NOWAIT | SEM_UNDO},
  25. {1, 0}, {0, +1, SEM_UNDO}
  26. };
  27. struct globals {
  28. struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
  29. struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
  30. struct shbuf_ds *shbuf;
  31. };
  32. #define G (*(struct globals*)&bb_common_bufsiz1)
  33. #define SMrup (G.SMrup)
  34. #define SMrdn (G.SMrdn)
  35. #define shbuf (G.shbuf)
  36. #define INIT_G() do { \
  37. memcpy(SMrup, init_sem, sizeof(init_sem)); \
  38. } while (0)
  39. static void error_exit(const char *str) NORETURN;
  40. static void error_exit(const char *str)
  41. {
  42. //release all acquired resources
  43. shmdt(shbuf);
  44. bb_perror_msg_and_die(str);
  45. }
  46. /*
  47. * sem_up - up()'s a semaphore.
  48. */
  49. static void sem_up(int semid)
  50. {
  51. if (semop(semid, SMrup, 1) == -1)
  52. error_exit("semop[SMrup]");
  53. }
  54. static void interrupted(int sig UNUSED_PARAM)
  55. {
  56. signal(SIGINT, SIG_IGN);
  57. shmdt(shbuf);
  58. exit(EXIT_SUCCESS);
  59. }
  60. int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  61. int logread_main(int argc UNUSED_PARAM, char **argv)
  62. {
  63. unsigned cur;
  64. int log_semid; /* ipc semaphore id */
  65. int log_shmid; /* ipc shared memory id */
  66. smallint follow = getopt32(argv, "f");
  67. INIT_G();
  68. log_shmid = shmget(KEY_ID, 0, 0);
  69. if (log_shmid == -1)
  70. bb_perror_msg_and_die("can't find syslogd buffer");
  71. /* Attach shared memory to our char* */
  72. shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
  73. if (shbuf == NULL)
  74. bb_perror_msg_and_die("can't access syslogd buffer");
  75. log_semid = semget(KEY_ID, 0, 0);
  76. if (log_semid == -1)
  77. error_exit("can't get access to semaphores for syslogd buffer");
  78. signal(SIGINT, interrupted);
  79. /* Suppose atomic memory read */
  80. /* Max possible value for tail is shbuf->size - 1 */
  81. cur = shbuf->tail;
  82. /* Loop for logread -f, one pass if there was no -f */
  83. do {
  84. unsigned shbuf_size;
  85. unsigned shbuf_tail;
  86. const char *shbuf_data;
  87. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  88. int i;
  89. int len_first_part;
  90. int len_total = len_total; /* for gcc */
  91. char *copy = copy; /* for gcc */
  92. #endif
  93. if (semop(log_semid, SMrdn, 2) == -1)
  94. error_exit("semop[SMrdn]");
  95. /* Copy the info, helps gcc to realize that it doesn't change */
  96. shbuf_size = shbuf->size;
  97. shbuf_tail = shbuf->tail;
  98. shbuf_data = shbuf->data; /* pointer! */
  99. if (DEBUG)
  100. printf("cur:%d tail:%i size:%i\n",
  101. cur, shbuf_tail, shbuf_size);
  102. if (!follow) {
  103. /* advance to oldest complete message */
  104. /* find NUL */
  105. cur += strlen(shbuf_data + cur);
  106. if (cur >= shbuf_size) { /* last byte in buffer? */
  107. cur = strnlen(shbuf_data, shbuf_tail);
  108. if (cur == shbuf_tail)
  109. goto unlock; /* no complete messages */
  110. }
  111. /* advance to first byte of the message */
  112. cur++;
  113. if (cur >= shbuf_size) /* last byte in buffer? */
  114. cur = 0;
  115. } else { /* logread -f */
  116. if (cur == shbuf_tail) {
  117. sem_up(log_semid);
  118. fflush(stdout);
  119. sleep(1); /* TODO: replace me with a sleep_on */
  120. continue;
  121. }
  122. }
  123. /* Read from cur to tail */
  124. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  125. len_first_part = len_total = shbuf_tail - cur;
  126. if (len_total < 0) {
  127. /* message wraps: */
  128. /* [SECOND PART.........FIRST PART] */
  129. /* ^data ^tail ^cur ^size */
  130. len_total += shbuf_size;
  131. }
  132. copy = xmalloc(len_total + 1);
  133. if (len_first_part < 0) {
  134. /* message wraps (see above) */
  135. len_first_part = shbuf_size - cur;
  136. memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
  137. }
  138. memcpy(copy, shbuf_data + cur, len_first_part);
  139. copy[len_total] = '\0';
  140. cur = shbuf_tail;
  141. #else
  142. while (cur != shbuf_tail) {
  143. fputs(shbuf_data + cur, stdout);
  144. cur += strlen(shbuf_data + cur) + 1;
  145. if (cur >= shbuf_size)
  146. cur = 0;
  147. }
  148. #endif
  149. unlock:
  150. /* release the lock on the log chain */
  151. sem_up(log_semid);
  152. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  153. for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
  154. fputs(copy + i, stdout);
  155. }
  156. free(copy);
  157. #endif
  158. } while (follow);
  159. shmdt(shbuf);
  160. fflush_stdout_and_exit(EXIT_SUCCESS);
  161. }