logread.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. //config:config LOGREAD
  12. //config: bool "logread"
  13. //config: default y
  14. //config: depends on FEATURE_IPC_SYSLOG
  15. //config: help
  16. //config: If you enabled Circular Buffer support, you almost
  17. //config: certainly want to enable this feature as well. This
  18. //config: utility will allow you to read the messages that are
  19. //config: stored in the syslogd circular buffer.
  20. //config:
  21. //config:config FEATURE_LOGREAD_REDUCED_LOCKING
  22. //config: bool "Double buffering"
  23. //config: default y
  24. //config: depends on LOGREAD
  25. //config: help
  26. //config: 'logread' ouput to slow serial terminals can have
  27. //config: side effects on syslog because of the semaphore.
  28. //config: This option make logread to double buffer copy
  29. //config: from circular buffer, minimizing semaphore
  30. //config: contention at some minor memory expense.
  31. //config:
  32. //applet:IF_LOGREAD(APPLET(logread, BB_DIR_SBIN, BB_SUID_DROP))
  33. //kbuild:lib-$(CONFIG_LOGREAD) += logread.o
  34. //usage:#define logread_trivial_usage
  35. //usage: "[-fF]"
  36. //usage:#define logread_full_usage "\n\n"
  37. //usage: "Show messages in syslogd's circular buffer\n"
  38. //usage: "\n -f Output data as log grows"
  39. //usage: "\n -F Same as -f, but dump buffer first"
  40. #include "libbb.h"
  41. #include <sys/ipc.h>
  42. #include <sys/sem.h>
  43. #include <sys/shm.h>
  44. #define DEBUG 0
  45. /* our shared key (syslogd.c and logread.c must be in sync) */
  46. enum { KEY_ID = 0x414e4547 }; /* "GENA" */
  47. struct shbuf_ds {
  48. int32_t size; // size of data - 1
  49. int32_t tail; // end of message list
  50. char data[1]; // messages
  51. };
  52. static const struct sembuf init_sem[3] = {
  53. {0, -1, IPC_NOWAIT | SEM_UNDO},
  54. {1, 0}, {0, +1, SEM_UNDO}
  55. };
  56. struct globals {
  57. struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
  58. struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
  59. struct shbuf_ds *shbuf;
  60. } FIX_ALIASING;
  61. #define G (*(struct globals*)&bb_common_bufsiz1)
  62. #define SMrup (G.SMrup)
  63. #define SMrdn (G.SMrdn)
  64. #define shbuf (G.shbuf)
  65. #define INIT_G() do { \
  66. memcpy(SMrup, init_sem, sizeof(init_sem)); \
  67. } while (0)
  68. #if 0
  69. static void error_exit(const char *str) NORETURN;
  70. static void error_exit(const char *str)
  71. {
  72. /* Release all acquired resources */
  73. shmdt(shbuf);
  74. bb_perror_msg_and_die(str);
  75. }
  76. #else
  77. /* On Linux, shmdt is not mandatory on exit */
  78. # define error_exit(str) bb_perror_msg_and_die(str)
  79. #endif
  80. /*
  81. * sem_up - up()'s a semaphore.
  82. */
  83. static void sem_up(int semid)
  84. {
  85. if (semop(semid, SMrup, 1) == -1)
  86. error_exit("semop[SMrup]");
  87. }
  88. static void interrupted(int sig)
  89. {
  90. /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
  91. kill_myself_with_sig(sig);
  92. }
  93. int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  94. int logread_main(int argc UNUSED_PARAM, char **argv)
  95. {
  96. unsigned cur;
  97. int log_semid; /* ipc semaphore id */
  98. int log_shmid; /* ipc shared memory id */
  99. int follow = getopt32(argv, "fF");
  100. INIT_G();
  101. log_shmid = shmget(KEY_ID, 0, 0);
  102. if (log_shmid == -1)
  103. bb_perror_msg_and_die("can't %s syslogd buffer", "find");
  104. /* Attach shared memory to our char* */
  105. shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
  106. if (shbuf == NULL)
  107. bb_perror_msg_and_die("can't %s syslogd buffer", "access");
  108. log_semid = semget(KEY_ID, 0, 0);
  109. if (log_semid == -1)
  110. error_exit("can't get access to semaphores for syslogd buffer");
  111. bb_signals(BB_FATAL_SIGS, interrupted);
  112. /* Suppose atomic memory read */
  113. /* Max possible value for tail is shbuf->size - 1 */
  114. cur = shbuf->tail;
  115. /* Loop for -f or -F, one pass otherwise */
  116. do {
  117. unsigned shbuf_size;
  118. unsigned shbuf_tail;
  119. const char *shbuf_data;
  120. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  121. int i;
  122. int len_first_part;
  123. int len_total = len_total; /* for gcc */
  124. char *copy = copy; /* for gcc */
  125. #endif
  126. if (semop(log_semid, SMrdn, 2) == -1)
  127. error_exit("semop[SMrdn]");
  128. /* Copy the info, helps gcc to realize that it doesn't change */
  129. shbuf_size = shbuf->size;
  130. shbuf_tail = shbuf->tail;
  131. shbuf_data = shbuf->data; /* pointer! */
  132. if (DEBUG)
  133. printf("cur:%u tail:%u size:%u\n",
  134. cur, shbuf_tail, shbuf_size);
  135. if (!(follow & 1)) { /* not -f */
  136. /* if -F, "convert" it to -f, so that we dont
  137. * dump the entire buffer on each iteration
  138. */
  139. follow >>= 1;
  140. /* advance to oldest complete message */
  141. /* find NUL */
  142. cur += strlen(shbuf_data + cur);
  143. if (cur >= shbuf_size) { /* last byte in buffer? */
  144. cur = strnlen(shbuf_data, shbuf_tail);
  145. if (cur == shbuf_tail)
  146. goto unlock; /* no complete messages */
  147. }
  148. /* advance to first byte of the message */
  149. cur++;
  150. if (cur >= shbuf_size) /* last byte in buffer? */
  151. cur = 0;
  152. } else { /* -f */
  153. if (cur == shbuf_tail) {
  154. sem_up(log_semid);
  155. fflush_all();
  156. sleep(1); /* TODO: replace me with a sleep_on */
  157. continue;
  158. }
  159. }
  160. /* Read from cur to tail */
  161. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  162. len_first_part = len_total = shbuf_tail - cur;
  163. if (len_total < 0) {
  164. /* message wraps: */
  165. /* [SECOND PART.........FIRST PART] */
  166. /* ^data ^tail ^cur ^size */
  167. len_total += shbuf_size;
  168. }
  169. copy = xmalloc(len_total + 1);
  170. if (len_first_part < 0) {
  171. /* message wraps (see above) */
  172. len_first_part = shbuf_size - cur;
  173. memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
  174. }
  175. memcpy(copy, shbuf_data + cur, len_first_part);
  176. copy[len_total] = '\0';
  177. cur = shbuf_tail;
  178. #else
  179. while (cur != shbuf_tail) {
  180. fputs(shbuf_data + cur, stdout);
  181. cur += strlen(shbuf_data + cur) + 1;
  182. if (cur >= shbuf_size)
  183. cur = 0;
  184. }
  185. #endif
  186. unlock:
  187. /* release the lock on the log chain */
  188. sem_up(log_semid);
  189. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  190. for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
  191. fputs(copy + i, stdout);
  192. }
  193. free(copy);
  194. #endif
  195. fflush_all();
  196. } while (follow);
  197. /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
  198. fflush_stdout_and_exit(EXIT_SUCCESS);
  199. }