/*********************************************************************** * <:copyright-BRCM:2006:DUAL/GPL:standard Copyright (c) 2006 Broadcom All Rights Reserved Unless you and Broadcom execute a separate written software license agreement governing use of this software, this software is licensed to you under the terms of the GNU General Public License version 2 (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, with the following added to such license: As a special exception, the copyright holders of this software give you permission to link this software with independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from this software. The special exception does not apply to any modifications of the software. Not withstanding the above, under no circumstances may you combine this software in any way with any other Broadcom software provided under a license other than the GPL, without Broadcom's express prior written consent. :> * ************************************************************************/ #include #include /* for shmat */ #include /* for struct sembuf */ #include "../oal.h" extern size_t strnlen(const char *s, size_t maxlen); /* * Some of these defines are duplicates of those in busybox/sysklogd, so * look there before changing anything here. */ static const long KEY_ID = 0x414e4547; /*"GENA"*/ static struct shbuf_ds { int size; // size of data written int tail; // end of message list char data[1]; // data/messages } *buf = NULL; // shared memory pointer // Semaphore operation structures static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup static struct sembuf SMrdn[2] = {{1, 0, 0}, {0, +1, SEM_UNDO}}; // set SMrdn static int log_shmid = -1; // ipc shared memory id static int log_semid = -1; // ipc semaphore id #define BCM_SYSLOG_MESSAGE_LEN_BYTES 4 /* * sem_up - up()'s a semaphore. */ static inline void sem_up(int semid) { if ( semop(semid, SMrup, 1) == -1 ) cmsLog_error("semop[SMrup]"); } /* * sem_down - down()'s a semaphore */ static inline void sem_down(int semid) { if ( semop(semid, SMrdn, 2) == -1 ) cmsLog_error("semop[SMrdn]"); } int oal_readLogPartial(int ptr, char* buffer) { int i=BCM_SYSLOG_READ_BUFFER_ERROR; int len; int end=0; const char *shbuf_data; if ( (log_shmid = shmget(KEY_ID, 0, 0)) == -1) { cmsLog_debug("Syslog disabled or log buffer not allocated\n"); goto output_end; } // Attach shared memory to our char* if ( (buf = shmat(log_shmid, NULL, SHM_RDONLY)) == NULL) { cmsLog_error("Can't get access to circular buffer from syslogd\n"); end = 1; goto output_end; } shbuf_data = &(buf->data[0]); if ( (log_semid = semget(KEY_ID, 0, 0)) == -1) { cmsLog_error("Can't get access to semaphone(s) for circular buffer from syslogd\n"); end = 1; goto output_end; } sem_down(log_semid); // Find starting point for the read if (ptr == BCM_SYSLOG_FIRST_READ){ // If this is the first read, find the first string after the tail if (buf->tail == 0 && shbuf_data[0] == '\0' && shbuf_data[1] == '\0' && shbuf_data[2] == '\0') { // initial, empty log buffer i = buf->tail; } else { i = buf->tail + strnlen(&(buf->data[buf->tail]), buf->size-buf->tail) + 1; if (i >= buf->size) { // wrap around to the beginning and try to find first string i = strnlen(&(buf->data[0]), buf->size) + 1; if (i >= buf->size) { cmsLog_error("Could not find starting point! tail=%d", buf->tail); i = buf->tail; } } } } else { i = ptr; } // i is the index to the start of the read if (i == buf->tail) { cmsLog_debug("\n"); i = BCM_SYSLOG_READ_BUFFER_END; end = 1; goto nothing2display; } readnext: if ( i != buf->tail) { if (i >= buf->size ) i = 0; snprintf(buffer, BCM_SYSLOG_MAX_LINE_SIZE, "%s", buf->data+i); i += strlen(buf->data+i) + 1; if (i >= buf->size ) i = 0; len = strlen(buffer); if (!((buffer[len] == '\0') && (buffer[len-1] == '\n'))) { // did not get the whole line yet, keep copying (from beginning) snprintf(&buffer[len], BCM_SYSLOG_MAX_LINE_SIZE-len, "%s", buf->data+i); len = strlen(buffer); i += strlen(buf->data+i) + 1; if (i >= buf->size ) i = 0; } /* work around for syslogd.c bug which generate first log without timestamp */ if (strlen(buffer) < 16 || buffer[3] != ' ' || buffer[6] != ' ' || buffer[9] != ':' || buffer[12] != ':' || buffer[15] != ' ') { goto readnext; } buffer[len-BCM_SYSLOG_MESSAGE_LEN_BYTES-1] = '\n'; buffer[len-BCM_SYSLOG_MESSAGE_LEN_BYTES] = '\0'; } else { /* read to the end already */ i = BCM_SYSLOG_READ_BUFFER_END; end = 1; } nothing2display: sem_up(log_semid); output_end: if (log_shmid != -1) shmdt(buf); if (end) { i=BCM_SYSLOG_READ_BUFFER_END; buffer[0]='\0'; } return i; }