int rotate_log(void) { char *tmp; int i; char *r1, *r2; if (rwl_trywrlock(&log_file_lock)) { fprintf(stderr, "failed to wrlock log_file_lock for rotation\n"); return 0; } // check if still oversize and not rotated by another thread off_t l = lseek(log_file, 0, SEEK_CUR); if (l < log_rotate_size) { rwl_wrunlock(&log_file_lock); return 0; } // rename tmp = hmalloc(strlen(log_fname) + 6); sprintf(tmp, "%s.tmp", log_fname); if (rename(log_fname, tmp) != 0) { fprintf(stderr, "aprsc logger: Failed to rename %s to %s: %s\n", log_fname, tmp, strerror(errno)); // continue anyway, try to reopen } // reopen if (close(log_file)) fprintf(stderr, "aprsc logger: Could not close log file %s: %s\n", log_fname, strerror(errno)); log_file = open(log_fname, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP); if (log_file < 0) { fprintf(stderr, "aprsc logger: Could not open %s: %s\n", log_fname, strerror(errno)); log_file = -1; } rwl_wrunlock(&log_file_lock); // do the rest of the rotation r1 = hmalloc(strlen(log_fname) + 16); r2 = hmalloc(strlen(log_fname) + 16); for (i = log_rotate_num-1; i > 0; i--) { sprintf(r1, "%s.%d", log_fname, i-1); sprintf(r2, "%s.%d", log_fname, i); if (rename(r1, r2) != 0 && errno != ENOENT) { fprintf(stderr, "rename %s => %s failed:%s\n", r1, r2, strerror(errno)); } } if (rename(tmp, r1) != 0) { fprintf(stderr, "aprsc logger: Failed to rename %s to %s: %s\n", tmp, r1, strerror(errno)); } hfree(tmp); hfree(r1); hfree(r2); return 0; }
int clientlist_add(struct client_t *c) { struct clientlist_t *cl; int old_fd; /* allocate and fill in */ cl = hmalloc(sizeof(*cl)); strncpy(cl->username, c->username, sizeof(cl->username)); cl->username[sizeof(cl->username)-1] = 0; cl->validated = c->validated; cl->fd = c->fd; /* get lock for list and insert */ rwl_wrlock(&clientlist_lock); old_fd = check_if_validated_client(c->username, strlen(c->username)); if (clientlist) clientlist->prevp = &cl->next; cl->next = clientlist; cl->prevp = &clientlist; cl->client_id = (void *)c; clientlist = cl; rwl_wrunlock(&clientlist_lock); return old_fd; }
void clientlist_remove(struct client_t *c) { struct clientlist_t *cl; /* get lock for list, find, and remove */ rwl_wrlock(&clientlist_lock); cl = clientlist_find_id((void *)c); if (cl) { if (cl->next) cl->next->prevp = cl->prevp; *cl->prevp = cl->next; hfree(cl); } rwl_wrunlock(&clientlist_lock); }
int open_log(char *name, int reopen) { if (!reopen) rwl_wrlock(&log_file_lock); if (log_name) hfree(log_name); if (!(log_name = hstrdup(name))) { fprintf(stderr, "aprsc logger: out of memory!\n"); exit(1); } if (log_basename) hfree(log_basename); log_basename = hmalloc(strlen(log_name) + 5); sprintf(log_basename, "%s.log", log_name); if (log_dest == L_SYSLOG) openlog(name, LOG_NDELAY|LOG_PID, log_facility); if (log_dest == L_FILE) { if (log_fname) hfree(log_fname); log_fname = hmalloc(strlen(log_dir) + strlen(log_basename) + 2); sprintf(log_fname, "%s/%s", log_dir, log_basename); log_file = open(log_fname, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP); if (log_file < 0) { fprintf(stderr, "aprsc logger: Could not open %s: %s\n", log_fname, strerror(errno)); exit(1); } } rwl_wrunlock(&log_file_lock); if (log_dest == L_FILE) hlog(LOG_DEBUG, "Log file %s %sopened on fd %d", log_fname, (reopen) ? "re" : "", log_file); return 0; }
int accesslog_close(char *reopenpath) { hlog(LOG_DEBUG, "Closing access log..."); rwl_wrlock(&accesslog_lock); hlog(LOG_DEBUG, "Closing access log, got lock"); if (close(accesslog_file)) hlog(LOG_CRIT, "Could not close %s: %s", accesslog_fname, strerror(errno)); hfree(accesslog_fname); hfree(accesslog_dir); accesslog_fname = accesslog_dir = NULL; accesslog_file = -1; if (reopenpath) { return accesslog_open(reopenpath, 1); } else { rwl_wrunlock(&accesslog_lock); return 0; } }
int close_log(int reopen) { hlog(LOG_DEBUG, "close_log"); char *s = NULL; if (log_name) s = hstrdup(log_name); rwl_wrlock(&log_file_lock); if (log_name) { hfree(log_name); log_name = NULL; } if (log_dest == L_SYSLOG) { closelog(); } else if (log_dest == L_FILE) { if (log_file >= 0) { if (close(log_file)) fprintf(stderr, "aprsc logger: Could not close log file %s: %s\n", log_fname, strerror(errno)); log_file = -1; } if (log_fname) { hfree(log_fname); log_fname = NULL; } } if (reopen && s) open_log(s, 1); if (!reopen) rwl_wrunlock(&log_file_lock); if (s) hfree(s); return 0; }
int accesslog_open(char *logd, int reopen) { if (!reopen) rwl_wrlock(&accesslog_lock); if (accesslog_fname) hfree(accesslog_fname); if (accesslog_dir) hfree(accesslog_dir); accesslog_dir = hstrdup(logd); accesslog_fname = hmalloc(strlen(accesslog_dir) + strlen(accesslog_basename) + 2); sprintf(accesslog_fname, "%s/%s", accesslog_dir, accesslog_basename); accesslog_file = open(accesslog_fname, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP); if (accesslog_file < 0) hlog(LOG_CRIT, "Could not open %s: %s", accesslog_fname, strerror(errno)); rwl_wrunlock(&accesslog_lock); return accesslog_file; }