3
0

logread.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "busybox.h"
  12. #include <sys/ipc.h>
  13. #include <sys/sem.h>
  14. #include <sys/shm.h>
  15. #define DEBUG 0
  16. enum { KEY_ID = 0x414e4547 }; /* "GENA" */
  17. static struct shbuf_ds {
  18. int32_t size; // size of data written
  19. int32_t head; // start of message list
  20. int32_t tail; // end of message list
  21. char data[1]; // data/messages
  22. } *buf; // shared memory pointer
  23. // Semaphore operation structures
  24. static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
  25. static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
  26. static void error_exit(const char *str) ATTRIBUTE_NORETURN;
  27. static void error_exit(const char *str)
  28. {
  29. //release all acquired resources
  30. shmdt(buf);
  31. bb_perror_msg_and_die(str);
  32. }
  33. /*
  34. * sem_up - up()'s a semaphore.
  35. */
  36. static void sem_up(int semid)
  37. {
  38. if (semop(semid, SMrup, 1) == -1)
  39. error_exit("semop[SMrup]");
  40. }
  41. static void interrupted(int sig ATTRIBUTE_UNUSED)
  42. {
  43. signal(SIGINT, SIG_IGN);
  44. shmdt(buf);
  45. exit(0);
  46. }
  47. int logread_main(int argc, char **argv)
  48. {
  49. int cur;
  50. int log_semid; /* ipc semaphore id */
  51. int log_shmid; /* ipc shared memory id */
  52. smallint follow = getopt32(argc, argv, "f");
  53. log_shmid = shmget(KEY_ID, 0, 0);
  54. if (log_shmid == -1)
  55. bb_perror_msg_and_die("can't find syslogd buffer");
  56. // Attach shared memory to our char*
  57. buf = shmat(log_shmid, NULL, SHM_RDONLY);
  58. if (buf == NULL)
  59. bb_perror_msg_and_die("can't access syslogd buffer");
  60. log_semid = semget(KEY_ID, 0, 0);
  61. if (log_semid == -1)
  62. error_exit("can't get access to semaphores for syslogd buffer");
  63. // attempt to redefine ^C signal
  64. signal(SIGINT, interrupted);
  65. // Suppose atomic memory move
  66. cur = follow ? buf->tail : buf->head;
  67. do {
  68. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  69. char *buf_data;
  70. int log_len, j;
  71. #endif
  72. if (semop(log_semid, SMrdn, 2) == -1)
  73. error_exit("semop[SMrdn]");
  74. if (DEBUG)
  75. printf("head:%i cur:%d tail:%i size:%i\n",
  76. buf->head, cur, buf->tail, buf->size);
  77. if (buf->head == buf->tail || cur == buf->tail) {
  78. if (follow) {
  79. sem_up(log_semid);
  80. fflush(stdout);
  81. sleep(1); /* TODO: replace me with a sleep_on */
  82. continue;
  83. }
  84. puts("<empty syslog>");
  85. }
  86. // Read Memory
  87. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  88. log_len = buf->tail - cur;
  89. if (log_len < 0)
  90. log_len += buf->size;
  91. buf_data = xmalloc(log_len);
  92. if (buf->tail >= cur)
  93. j = log_len;
  94. else
  95. j = buf->size - cur;
  96. memcpy(buf_data, buf->data + cur, j);
  97. if (buf->tail < cur)
  98. memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
  99. cur = buf->tail;
  100. #else
  101. while (cur != buf->tail) {
  102. fputs(buf->data + cur, stdout);
  103. cur += strlen(buf->data + cur) + 1;
  104. if (cur >= buf->size)
  105. cur = 0;
  106. }
  107. #endif
  108. // release the lock on the log chain
  109. sem_up(log_semid);
  110. #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
  111. for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
  112. fputs(buf_data + j, stdout);
  113. }
  114. free(buf_data);
  115. #endif
  116. } while (follow);
  117. shmdt(buf);
  118. fflush_stdout_and_exit(EXIT_SUCCESS);
  119. }