Ejemplo n.º 1
0
Archivo: hlog.c Proyecto: hessu/aprsc
static int hlog_write(int priority, const char *s)
{
    struct tm lt;
    struct timeval tv;
    char wb[LOG_LEN];
    int len, w;

    gettimeofday(&tv, NULL);
    gmtime_r(&tv.tv_sec, &lt);

    if (log_dest & L_STDERR) {
        rwl_rdlock(&log_file_lock);
        fprintf(stderr, "%4d/%02d/%02d %02d:%02d:%02d.%06d %s[%d:%lx] %s: %s\n",
                lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec, (int)tv.tv_usec,
                (log_name) ? log_name : "aprsc", (int)getpid(), (unsigned long int)pthread_self(), log_levelnames[priority], s);
        rwl_rdunlock(&log_file_lock);

    }

    if ((log_dest & L_FILE) && (log_file >= 0)) {
        len = snprintf(wb, LOG_LEN, "%4d/%02d/%02d %02d:%02d:%02d.%06d %s[%d:%lx] %s: %s\n",
                       lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec, (int)tv.tv_usec,
                       (log_name) ? log_name : "aprsc", (int)getpid(), (unsigned long int)pthread_self(), log_levelnames[priority], s);
        if (len >= LOG_LEN) {
            len = LOG_LEN-1; // Truncated! Do not write out the NUL byte in the end.
            wb[LOG_LEN-2] = '\n'; // Do write out a newline which was truncated.
        }

        wb[LOG_LEN-1] = 0;
        rwl_rdlock(&log_file_lock);
        if ((w = write(log_file, wb, len)) != len)
            fprintf(stderr, "aprsc logger: Could not write to %s (fd %d): %s\n", log_fname, log_file, strerror(errno));
        rwl_rdunlock(&log_file_lock);

        if (log_rotate_size) {
            off_t l = lseek(log_file, 0, SEEK_CUR);
            if (l >= log_rotate_size) {
                rotate_log();
            }
        }

    }

    if (log_dest & L_SYSLOG) {
        rwl_rdlock(&log_file_lock);
        syslog(priority, "%s: %s", log_levelnames[priority], s);
        rwl_rdunlock(&log_file_lock);
    }

    return 1;
}
Ejemplo n.º 2
0
Archivo: hlog.c Proyecto: hessu/aprsc
int accesslog(const char *fmt, ...)
{
    va_list args;
    char s[LOG_LEN], wb[LOG_LEN];
    time_t t;
    struct tm lt;
    int len;
    ssize_t w;

    va_start(args, fmt);
    vsnprintf(s, LOG_LEN, fmt, args);
    va_end(args);
    s[LOG_LEN-1] = 0;

    time(&t);
    gmtime_r(&t, &lt);

    len = snprintf(wb, LOG_LEN, "[%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d] %s\n",
                   lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec, s);
    wb[LOG_LEN-1] = 0;

    rwl_rdlock(&accesslog_lock);
    if (accesslog_file >= 0) {
        if ((w = write(accesslog_file, wb, len)) != len)
            hlog(LOG_CRIT, "Could not write to %s (fd %d): %s", accesslog_fname, accesslog_file, strerror(errno));
    } else {
        if (accesslog_file != -666) {
            hlog(LOG_ERR, "Access log not open, log lines are lost!");
            accesslog_file = -666;
        }
    }
    rwl_rdunlock(&accesslog_lock);

    return 1;
}
Ejemplo n.º 3
0
void process_outgoing(struct worker_t *self)
{
	struct pbuf_t *pb;
	int e;
	
	if ((e = rwl_rdlock(&pbuf_global_rwlock))) {
		hlog(LOG_CRIT, "worker: Failed to rdlock pbuf_global_rwlock!");
		exit(1);
	}
	
	while ((pb = *self->pbuf_global_prevp)) {
		//__sync_synchronize();
		/* Some safety checks against bugs and overload conditions */
		if (pb->is_free) {
			hlog(LOG_ERR, "worker %d: process_outgoing got pbuf %d marked free, age %d (now %d t %d)\n%.*s",
				self->id, pb->seqnum, tick - pb->t, tick, pb->t, pb->packet_len-2, pb->data);
			abort(); /* this would be pretty bad, so we crash immediately */
		} else if (pb->t > tick + 2) {
			/* 2-second offset is normal in case of one thread updating tick earlier than another
			 * and a little thread scheduling luck
			 */
			hlog(LOG_ERR, "worker %d: process_outgoing got packet %d from future with t %d > tick %d!\n%.*s",
				self->id, pb->seqnum, pb->t, tick, pb->packet_len-2, pb->data);
			status_error(86400, "packet_drop_future");
		} else if (tick - pb->t > 5) {
			/* this is a bit too old, are we stuck? */
			hlog(LOG_ERR, "worker %d: process_outgoing got packet %d aged %d sec (now %d t %d)\n%.*s",
				self->id, pb->seqnum, tick - pb->t, tick, pb->t, pb->packet_len-2, pb->data);
			status_error(86400, "packet_drop_hang");
		} else {
			process_outgoing_single(self, pb);
		}
		self->last_pbuf_seqnum = pb->seqnum;
		self->pbuf_global_prevp = &pb->next;
	}
	
	while ((pb = *self->pbuf_global_dupe_prevp)) {
		if (pb->is_free) {
			hlog(LOG_ERR, "worker %d: process_outgoing got dupe %d marked free, age %d (now %d t %d)\n%.*s",
				self->id, pb->seqnum, tick - pb->t, tick, pb->t, pb->packet_len-2, pb->data);
			abort();
		} else if (pb->t > tick + 2) {
			hlog(LOG_ERR, "worker: process_outgoing got dupe from future %d with t %d > tick %d!\n%.*s",
				pb->seqnum, pb->t, tick, pb->packet_len-2, pb->data);
		} else if (tick - pb->t > 5) {
			hlog(LOG_ERR, "worker: process_outgoing got dupe %d aged %d sec\n%.*s",
				pb->seqnum, tick - pb->t, pb->packet_len-2, pb->data);
		} else {
			process_outgoing_single(self, pb);
		}
		self->last_pbuf_dupe_seqnum = pb->seqnum;
		self->pbuf_global_dupe_prevp = &pb->next;
	}
	
	if ((e = rwl_rdunlock(&pbuf_global_rwlock))) {
		hlog(LOG_CRIT, "worker: Failed to rdunlock pbuf_global_rwlock!");
		exit(1);
	}
}
Ejemplo n.º 4
0
int clientlist_check_if_validated_client(char *username, int len)
{
	int fd;
	
	rwl_rdlock(&clientlist_lock);
	
	fd = check_if_validated_client(username, len);
	
	rwl_rdunlock(&clientlist_lock);
	
	return fd;
}