3
0

logread.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #if 0
  45. static void error_exit(const char *str) NORETURN;
  46. static void error_exit(const char *str)
  47. {
  48. /* Release all acquired resources */
  49. shmdt(shbuf);
  50. bb_perror_msg_and_die(str);
  51. }
  52. #else
  53. /* On Linux, shmdt is not mandatory on exit */
  54. # define error_exit(str) bb_perror_msg_and_die(str)
  55. #endif
  56. /*
  57. * sem_up - up()'s a semaphore.
  58. */
  59. static void sem_up(int semid)
  60. {
  61. if (semop(semid, SMrup, 1) == -1)
  62. error_exit("semop[SMrup]");
  63. }
  64. static void interrupted(int sig)
  65. {
  66. /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
  67. kill_myself_with_sig(sig);
  68. }
  69. int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  70. int logread_main(int argc UNUSED_PARAM, char **argv)
  71. {
  72. unsigned cur;
  73. int log_semid; /* ipc semaphore id */
  74. int log_shmid; /* ipc shared memory id */
  75. smallint follow = getopt32(argv, "f");
  76. INIT_G();
  77. log_shmid = shmget(KEY_ID, 0, 0);
  78. if (log_shmid == -1)
  79. bb_perror_msg_and_die("can't %s syslogd buffer", "find");
  80. /* Attach shared memory to our char* */
  81. shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
  82. if (shbuf == NULL)
  83. bb_perror_msg_and_die("can't %s syslogd buffer", "access");
  84. log_semid = semget(KEY_ID, 0, 0);
  85. if (log_semid == -1)
  86. error_exit("can't get access to semaphores for syslogd buffer");
  87. bb_signals(BB_FATAL_SIGS, interrupted);
  88. /* Suppose atomic memory read */
  89. /* Max possible value for tail is shbuf->size - 1 */
  90. cur = shbuf->tail;
  91. /* Loop for logread -f, one pass if there was no -f */
  92. do {
  93. unsigned shbuf_size;
  94. unsigned shbuf_tail;
  95. const char *shbuf_data;
  96. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  97. int i;
  98. int len_first_part;
  99. int len_total = len_total; /* for gcc */
  100. char *copy = copy; /* for gcc */
  101. #endif
  102. if (semop(log_semid, SMrdn, 2) == -1)
  103. error_exit("semop[SMrdn]");
  104. /* Copy the info, helps gcc to realize that it doesn't change */
  105. shbuf_size = shbuf->size;
  106. shbuf_tail = shbuf->tail;
  107. shbuf_data = shbuf->data; /* pointer! */
  108. if (DEBUG)
  109. printf("cur:%u tail:%u size:%u\n",
  110. cur, shbuf_tail, shbuf_size);
  111. if (!follow) {
  112. /* advance to oldest complete message */
  113. /* find NUL */
  114. cur += strlen(shbuf_data + cur);
  115. if (cur >= shbuf_size) { /* last byte in buffer? */
  116. cur = strnlen(shbuf_data, shbuf_tail);
  117. if (cur == shbuf_tail)
  118. goto unlock; /* no complete messages */
  119. }
  120. /* advance to first byte of the message */
  121. cur++;
  122. if (cur >= shbuf_size) /* last byte in buffer? */
  123. cur = 0;
  124. } else { /* logread -f */
  125. if (cur == shbuf_tail) {
  126. sem_up(log_semid);
  127. fflush_all();
  128. sleep(1); /* TODO: replace me with a sleep_on */
  129. continue;
  130. }
  131. }
  132. /* Read from cur to tail */
  133. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  134. len_first_part = len_total = shbuf_tail - cur;
  135. if (len_total < 0) {
  136. /* message wraps: */
  137. /* [SECOND PART.........FIRST PART] */
  138. /* ^data ^tail ^cur ^size */
  139. len_total += shbuf_size;
  140. }
  141. copy = xmalloc(len_total + 1);
  142. if (len_first_part < 0) {
  143. /* message wraps (see above) */
  144. len_first_part = shbuf_size - cur;
  145. memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
  146. }
  147. memcpy(copy, shbuf_data + cur, len_first_part);
  148. copy[len_total] = '\0';
  149. cur = shbuf_tail;
  150. #else
  151. while (cur != shbuf_tail) {
  152. fputs(shbuf_data + cur, stdout);
  153. cur += strlen(shbuf_data + cur) + 1;
  154. if (cur >= shbuf_size)
  155. cur = 0;
  156. }
  157. #endif
  158. unlock:
  159. /* release the lock on the log chain */
  160. sem_up(log_semid);
  161. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  162. for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
  163. fputs(copy + i, stdout);
  164. }
  165. free(copy);
  166. #endif
  167. fflush_all();
  168. } while (follow);
  169. /* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
  170. fflush_stdout_and_exit(EXIT_SUCCESS);
  171. }