diff options
-rw-r--r-- | sysklogd/logread.c | 80 |
1 files changed, 31 insertions, 49 deletions
diff --git a/sysklogd/logread.c b/sysklogd/logread.c index 4427cf37e..d092056bf 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c | |||
@@ -11,37 +11,30 @@ | |||
11 | 11 | ||
12 | #include "busybox.h" | 12 | #include "busybox.h" |
13 | #include <sys/ipc.h> | 13 | #include <sys/ipc.h> |
14 | //#include <sys/types.h> | ||
15 | #include <sys/sem.h> | 14 | #include <sys/sem.h> |
16 | #include <sys/shm.h> | 15 | #include <sys/shm.h> |
17 | //#include <signal.h> | ||
18 | //#include <setjmp.h> | ||
19 | 16 | ||
20 | #define DEBUG 0 | 17 | #define DEBUG 0 |
21 | 18 | ||
22 | static const long KEY_ID = 0x414e4547; /* "GENA" */ | 19 | enum { KEY_ID = 0x414e4547 }; /* "GENA" */ |
23 | 20 | ||
24 | static struct shbuf_ds { | 21 | static struct shbuf_ds { |
25 | int32_t size; // size of data written | 22 | int32_t size; // size of data written |
26 | int32_t head; // start of message list | 23 | int32_t head; // start of message list |
27 | int32_t tail; // end of message list | 24 | int32_t tail; // end of message list |
28 | char data[1]; // data/messages | 25 | char data[1]; // data/messages |
29 | } *buf; // shared memory pointer | 26 | } *buf; // shared memory pointer |
30 | |||
31 | 27 | ||
32 | // Semaphore operation structures | 28 | // Semaphore operation structures |
33 | static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup | 29 | static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup |
34 | static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn | 30 | static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn |
35 | 31 | ||
36 | static int log_shmid = -1; // ipc shared memory id | ||
37 | static int log_semid = -1; // ipc semaphore id | ||
38 | 32 | ||
39 | static void error_exit(const char *str) ATTRIBUTE_NORETURN; | 33 | static void error_exit(const char *str) ATTRIBUTE_NORETURN; |
40 | static void error_exit(const char *str) | 34 | static void error_exit(const char *str) |
41 | { | 35 | { |
42 | //release all acquired resources | 36 | //release all acquired resources |
43 | if (log_shmid != -1) | 37 | shmdt(buf); |
44 | shmdt(buf); | ||
45 | bb_perror_msg_and_die(str); | 38 | bb_perror_msg_and_die(str); |
46 | } | 39 | } |
47 | 40 | ||
@@ -54,16 +47,7 @@ static void sem_up(int semid) | |||
54 | error_exit("semop[SMrup]"); | 47 | error_exit("semop[SMrup]"); |
55 | } | 48 | } |
56 | 49 | ||
57 | /* | 50 | static void interrupted(int sig ATTRIBUTE_UNUSED) |
58 | * sem_down - down()'s a semaphore | ||
59 | */ | ||
60 | static void sem_down(int semid) | ||
61 | { | ||
62 | if (semop(semid, SMrdn, 2) == -1) | ||
63 | error_exit("semop[SMrdn]"); | ||
64 | } | ||
65 | |||
66 | static void interrupted(int sig) | ||
67 | { | 51 | { |
68 | signal(SIGINT, SIG_IGN); | 52 | signal(SIGINT, SIG_IGN); |
69 | shmdt(buf); | 53 | shmdt(buf); |
@@ -73,23 +57,18 @@ static void interrupted(int sig) | |||
73 | int logread_main(int argc, char **argv) | 57 | int logread_main(int argc, char **argv) |
74 | { | 58 | { |
75 | int cur; | 59 | int cur; |
76 | int follow = 1; | 60 | int log_semid; /* ipc semaphore id */ |
77 | 61 | int log_shmid; /* ipc shared memory id */ | |
78 | if (argc != 2 || argv[1][0]!='-' || argv[1][1]!='f' ) { | 62 | smallint follow = getopt32(argc, argv, "f"); |
79 | follow = 0; | ||
80 | /* no options, no getopt */ | ||
81 | if (argc > 1) | ||
82 | bb_show_usage(); | ||
83 | } | ||
84 | 63 | ||
85 | log_shmid = shmget(KEY_ID, 0, 0); | 64 | log_shmid = shmget(KEY_ID, 0, 0); |
86 | if (log_shmid == -1) | 65 | if (log_shmid == -1) |
87 | error_exit("can't find circular buffer"); | 66 | bb_perror_msg_and_die("can't find syslogd buffer"); |
88 | 67 | ||
89 | // Attach shared memory to our char* | 68 | // Attach shared memory to our char* |
90 | buf = shmat(log_shmid, NULL, SHM_RDONLY); | 69 | buf = shmat(log_shmid, NULL, SHM_RDONLY); |
91 | if (buf == NULL) | 70 | if (buf == NULL) |
92 | error_exit("can't get access to syslogd buffer"); | 71 | bb_perror_msg_and_die("can't access syslogd buffer"); |
93 | 72 | ||
94 | log_semid = semget(KEY_ID, 0, 0); | 73 | log_semid = semget(KEY_ID, 0, 0); |
95 | if (log_semid == -1) | 74 | if (log_semid == -1) |
@@ -102,38 +81,42 @@ int logread_main(int argc, char **argv) | |||
102 | cur = follow ? buf->tail : buf->head; | 81 | cur = follow ? buf->tail : buf->head; |
103 | 82 | ||
104 | do { | 83 | do { |
105 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING | 84 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
106 | char *buf_data; | 85 | char *buf_data; |
107 | int log_len, j; | 86 | int log_len, j; |
108 | #endif | 87 | #endif |
109 | sem_down(log_semid); | 88 | if (semop(log_semid, SMrdn, 2) == -1) |
89 | error_exit("semop[SMrdn]"); | ||
110 | 90 | ||
111 | if (DEBUG) | 91 | if (DEBUG) |
112 | printf("head:%i cur:%d tail:%i size:%i\n", buf->head, cur, buf->tail, buf->size); | 92 | printf("head:%i cur:%d tail:%i size:%i\n", |
93 | buf->head, cur, buf->tail, buf->size); | ||
113 | 94 | ||
114 | if (buf->head == buf->tail || cur == buf->tail) { | 95 | if (buf->head == buf->tail || cur == buf->tail) { |
115 | if (follow) { | 96 | if (follow) { |
116 | sem_up(log_semid); | 97 | sem_up(log_semid); |
117 | sleep(1); /* TODO: replace me with a sleep_on */ | 98 | fflush(stdout); |
99 | sleep(1); /* TODO: replace me with a sleep_on */ | ||
118 | continue; | 100 | continue; |
119 | } else { | ||
120 | printf("<empty syslog>\n"); | ||
121 | } | 101 | } |
102 | puts("<empty syslog>"); | ||
122 | } | 103 | } |
123 | 104 | ||
124 | // Read Memory | 105 | // Read Memory |
125 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING | 106 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
126 | log_len = buf->tail - cur; | 107 | log_len = buf->tail - cur; |
127 | if (log_len < 0) | 108 | if (log_len < 0) |
128 | log_len += buf->size; | 109 | log_len += buf->size; |
129 | buf_data = xmalloc(log_len); | 110 | buf_data = xmalloc(log_len); |
130 | 111 | ||
131 | if (buf->tail >= cur) { | 112 | if (buf->tail >= cur) |
132 | memcpy(buf_data, buf->data + cur, log_len); | 113 | j = log_len; |
133 | } else { | 114 | else |
134 | memcpy(buf_data, buf->data + cur, buf->size - cur); | 115 | j = buf->size - cur; |
116 | memcpy(buf_data, buf->data + cur, j); | ||
117 | |||
118 | if (buf->tail < cur) | ||
135 | memcpy(buf_data + buf->size - cur, buf->data, buf->tail); | 119 | memcpy(buf_data + buf->size - cur, buf->data, buf->tail); |
136 | } | ||
137 | cur = buf->tail; | 120 | cur = buf->tail; |
138 | #else | 121 | #else |
139 | while (cur != buf->tail) { | 122 | while (cur != buf->tail) { |
@@ -146,16 +129,15 @@ int logread_main(int argc, char **argv) | |||
146 | // release the lock on the log chain | 129 | // release the lock on the log chain |
147 | sem_up(log_semid); | 130 | sem_up(log_semid); |
148 | 131 | ||
149 | #ifdef CONFIG_FEATURE_LOGREAD_REDUCED_LOCKING | 132 | #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING |
150 | for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) { | 133 | for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) { |
151 | fputs(buf_data + j, stdout); | 134 | fputs(buf_data + j, stdout); |
152 | } | 135 | } |
153 | free(buf_data); | 136 | free(buf_data); |
154 | #endif | 137 | #endif |
155 | fflush(stdout); | ||
156 | } while (follow); | 138 | } while (follow); |
157 | 139 | ||
158 | shmdt(buf); | 140 | shmdt(buf); |
159 | 141 | ||
160 | return EXIT_SUCCESS; | 142 | fflush_stdout_and_exit(EXIT_SUCCESS); |
161 | } | 143 | } |