logread.c 4.7 KB

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