logread.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307 USA
  23. *
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/ipc.h>
  29. #include <sys/types.h>
  30. #include <sys/sem.h>
  31. #include <sys/shm.h>
  32. #include <signal.h>
  33. #include <setjmp.h>
  34. #include <unistd.h>
  35. #include "busybox.h"
  36. static const long KEY_ID = 0x414e4547; /*"GENA"*/
  37. static struct shbuf_ds {
  38. int size; // size of data written
  39. int head; // start of message list
  40. int tail; // end of message list
  41. char data[1]; // data/messages
  42. } *buf = NULL; // shared memory pointer
  43. // Semaphore operation structures
  44. static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
  45. static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
  46. static int log_shmid = -1; // ipc shared memory id
  47. static int log_semid = -1; // ipc semaphore id
  48. static jmp_buf jmp_env;
  49. static void error_exit(const char *str);
  50. static void interrupted(int sig);
  51. /*
  52. * sem_up - up()'s a semaphore.
  53. */
  54. static inline void sem_up(int semid)
  55. {
  56. if ( semop(semid, SMrup, 1) == -1 )
  57. error_exit("semop[SMrup]");
  58. }
  59. /*
  60. * sem_down - down()'s a semaphore
  61. */
  62. static inline void sem_down(int semid)
  63. {
  64. if ( semop(semid, SMrdn, 2) == -1 )
  65. error_exit("semop[SMrdn]");
  66. }
  67. extern int logread_main(int argc, char **argv)
  68. {
  69. int i;
  70. int follow=0;
  71. if (argc == 2 && strcmp(argv[1],"-f")==0) {
  72. follow = 1;
  73. } else {
  74. /* no options, no getopt */
  75. if (argc > 1)
  76. bb_show_usage();
  77. }
  78. // handle intrrupt signal
  79. if (setjmp(jmp_env)) goto output_end;
  80. // attempt to redefine ^C signal
  81. signal(SIGINT, interrupted);
  82. if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1)
  83. error_exit("Can't find circular buffer");
  84. // Attach shared memory to our char*
  85. if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL)
  86. error_exit("Can't get access to circular buffer from syslogd");
  87. if ( (log_semid = semget(KEY_ID, 0, 0)) == -1)
  88. error_exit("Can't get access to semaphone(s) for circular buffer from syslogd");
  89. // Suppose atomic memory move
  90. i = follow ? buf->tail : buf->head;
  91. do {
  92. #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
  93. char *buf_data;
  94. int log_len,j;
  95. #endif
  96. sem_down(log_semid);
  97. //printf("head: %i tail: %i size: %i\n",buf->head,buf->tail,buf->size);
  98. if (buf->head == buf->tail || i==buf->tail) {
  99. if (follow) {
  100. sem_up(log_semid);
  101. sleep(1); /* TODO: replace me with a sleep_on */
  102. continue;
  103. } else {
  104. printf("<empty syslog>\n");
  105. }
  106. }
  107. // Read Memory
  108. #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
  109. log_len = buf->tail - i;
  110. if (log_len < 0)
  111. log_len += buf->size;
  112. buf_data = (char *)malloc(log_len);
  113. if (!buf_data)
  114. error_exit("malloc failed");
  115. if (buf->tail < i) {
  116. memcpy(buf_data, buf->data+i, buf->size-i);
  117. memcpy(buf_data+buf->size-i, buf->data, buf->tail);
  118. } else {
  119. memcpy(buf_data, buf->data+i, buf->tail-i);
  120. }
  121. i = buf->tail;
  122. #else
  123. while ( i != buf->tail) {
  124. printf("%s", buf->data+i);
  125. i+= strlen(buf->data+i) + 1;
  126. if (i >= buf->size )
  127. i=0;
  128. }
  129. #endif
  130. // release the lock on the log chain
  131. sem_up(log_semid);
  132. #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING
  133. for (j=0; j < log_len; j+=strlen(buf_data+j)+1) {
  134. printf("%s", buf_data+j);
  135. if (follow)
  136. fflush(stdout);
  137. }
  138. free(buf_data);
  139. #endif
  140. } while (follow);
  141. output_end:
  142. if (log_shmid != -1)
  143. shmdt(buf);
  144. return EXIT_SUCCESS;
  145. }
  146. static void interrupted(int sig){
  147. signal(SIGINT, SIG_IGN);
  148. longjmp(jmp_env, 1);
  149. }
  150. static void error_exit(const char *str){
  151. perror(str);
  152. //release all acquired resources
  153. if (log_shmid != -1)
  154. shmdt(buf);
  155. exit(1);
  156. }