logread.c 6.1 KB

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