static int dump_reports(void) { int fd; FILE *fp; char fname[128]; char tname[128]; if (disk_is_full()) { syslog(LOG_NOTICE, "%s", "Not enough free disk space to write XML files"); return 1; } #if HAVE_LIBNCAP snprintf(fname, 128, "%d.dscdata.xml", Ncap_finish_time()); #else snprintf(fname, 128, "%d.dscdata.xml", Pcap_finish_time()); #endif snprintf(tname, 128, "%s.XXXXXXXXX", fname); fd = mkstemp(tname); if (fd < 0) { syslog(LOG_ERR, "%s: %s", tname, strerror(errno)); return 1; } fp = fdopen(fd, "w"); if (NULL == fp) { syslog(LOG_ERR, "%s: %s", tname, strerror(errno)); close(fd); return 1; } if (debug_flag) fprintf(stderr, "writing to %s\n", tname); fprintf(fp, "<dscdata>\n"); /* amalloc_report(); */ pcap_report(fp); dns_message_report(fp); ip_message_report(fp); fprintf(fp, "</dscdata>\n"); /* * XXX need chmod because files are written as root, but may be processed * by a non-priv user */ fchmod(fd, 0664); fclose(fp); if (debug_flag) fprintf(stderr, "renaming to %s\n", fname); rename(tname, fname); return 0; }
static int dump_report(md_array_printer * printer) { int fd; FILE *fp; char fname[128]; char tname[128]; if (disk_is_full()) { dsyslogf(LOG_NOTICE, "Not enough free disk space to write %s files", printer->format); return 1; } snprintf(fname, 128, "%d.dscdata.%s", Pcap_finish_time(), printer->extension); snprintf(tname, 128, "%s.XXXXXXXXX", fname); fd = mkstemp(tname); if (fd < 0) { dsyslogf(LOG_ERR, "%s: %s", tname, strerror(errno)); return 1; } fp = fdopen(fd, "w"); if (NULL == fp) { dsyslogf(LOG_ERR, "%s: %s", tname, strerror(errno)); close(fd); return 1; } dfprintf(0, "writing to %s", tname); fputs(printer->start_file, fp); /* amalloc_report(); */ pcap_report(fp, printer); dns_message_report(fp, printer); fputs(printer->end_file, fp); /* * XXX need chmod because files are written as root, but may be processed * by a non-priv user */ fchmod(fd, 0664); fclose(fp); dfprintf(0, "renaming to %s", fname); if (rename(tname, fname)) { dsyslogf(LOG_ERR, "unable to move report from %s to %s: %s", tname, fname, strerror(errno)); } return 0; }