Example #1
0
File: file.c Project: nrhtr/genesis
void flush_files(void)
{
    filec_t *file;

    for (file = files; file; file = file->next)
        flush_file(file);
}
Example #2
0
/**
 * Main processing loop
 */
void process_csv(struct csv_context *ctx) {
    FILE *fp;
    char buf[READ_BUF_SIZE];
    size_t bytes_read;

    // Read from a file or STDIN
    if(!ctx->from_stdin) {
        // Attempt to open our file
        if(!(fp = fopen(ctx->in_file, "r"))) {
            fprintf(stderr, "Couldn't open input file '%s'\n", ctx->in_file);
            exit(EXIT_FAILURE);
        }
    } else {
        // Just read from STDIN
        fp = stdin;
    }

    // Process the file
    while((bytes_read = fread(buf, 1, sizeof(buf), fp)) > 0) {
        // Parse our CSV
        if(csv_parse(&ctx->parser, buf, bytes_read, cb_col, cb_row, (void*)ctx) != bytes_read) {
            fprintf(stderr, "Error while parsing file!\n");
            exit(EXIT_FAILURE);
        }
    }

    // Write any additional rows to disk as long as it's just just our header we've been
    // keeping around (if we're injecting headers).
    if(CBUF_POS(ctx->csv_buf) > ctx->header_len) flush_file(ctx, 0);

    // Close our file
    fclose(fp);
}
Example #3
0
int	launch_not_found(t_cmd *cmd)
{
  write_str(g_fstdout, "Error: command ");
  write_str(g_fstdout, cmd->cmd);
  write_str(g_fstdout, " not found.\n");
  flush_file(g_fstdout);
  return (SIGRTN_NOTFND);
}
Example #4
0
void		param_error(int err_id, int line)
{
  char		*line_str;
  int		size;

  size = get_param_error_size();
  if (err_id < 0 || err_id >= size)
    err_id = 0;
  write_str(g_fstdout, "Error line ");
  line_str = utl_itoa(line);
  write_str(g_fstdout, line_str);
  free(line_str);
  write_str(g_fstdout, ": ");
  write_str(g_fstdout, g_param_error[err_id]);
  flush_file(g_fstdout);
}
Example #5
0
int save_avatar(const char_t *id, const uint8_t *data, uint32_t size)
{
    char_t path[UTOX_FILE_NAME_LENGTH];

    get_avatar_location(path, id);

    FILE *file = fopen((char*)path, "wb");
    if (file) {
        fwrite(data, size, 1, file);
        flush_file(file);
        fclose(file);
        return 1;
    } else {
        debug("Avatars:\terror opening avatar file (%s) for writing\n", (char *)path);
        return 0;
    }
}
static bool write_server_command(NVQRConnection c, NVQRqueryOp op,
                                 int queryType, pid_t pid)
{
    NVQRQueryCmdBuffer buf;
    bool ret;

    memset(&buf, 0, sizeof(buf));
    buf.op = op;
    buf.queryType = queryType;
    buf.pid = pid;

    ret = write_file(c.server_handle, &buf, sizeof(buf)) == sizeof(buf);

    flush_file(c.server_handle);

    return ret;
}
Example #7
0
static void	print_sigmsg(int signal, int coredump)
{
  int		it;

  it = 0;
  while (g_sigmsg[it].sig)
    {
      if (g_sigmsg[it].sig == signal)
	{
	  write_str(g_fstdout, g_sigmsg[it].msg);
	  if (coredump)
	    write_str(g_fstdout, " (core dumped)");
	  write_char(g_fstdout, '\n');
	  flush_file(g_fstdout);
	}
      it++;
    }
}
Example #8
0
/**
 * Column callback
 */
static inline void cb_col(void *s, size_t len, void *data) {
    struct csv_context *ctx = (struct csv_context *)data;
    size_t cnt;

    // Put a comma if we should
    if(ctx->put_comma) {
        ctx->csv_buf = cbuf_putc(ctx->csv_buf, ',');
    }
    ctx->put_comma = 1;

    // If we are keeping same columns together see if we're on one
    if(ctx->gcol > -1 && ctx->col == ctx->gcol) {
        // Don't treat header columns as a group column
        if(!ctx->use_header || ctx->header_len) {
            // If we have a last column value and we're in overflow, check
	    	// the new row's value against the last one
            if(ctx->gcol_buf && ctx->opos && memcmp(ctx->gcol_buf, s, len) != 0) {
                // Flush the data we have!
                flush_file(ctx, 1);
            } else if(!ctx->gcol_buf) {
                // Initialize a new group column buffer
                ctx->gcol_buf = cbuf_init(len);
            }

            // Update our last group column value
            ctx->gcol_buf = cbuf_setlen(ctx->gcol_buf, (const char*)s, len);
        }
    }

    // Make sure we can write all the data
    while((cnt = csv_write(CBUF_PTR(ctx->csv_buf), CBUF_REM(ctx->csv_buf), s, len)) > CBUF_REM(ctx->csv_buf)) {
        // We didn't have room, reallocate
        ctx->csv_buf = cbuf_double(ctx->csv_buf);
    }

    // Increment where we are in our buffer
    CBUF_POS(ctx->csv_buf)+=cnt;

    // Increment our column
    ctx->col++;
}
Example #9
0
/**
 * Row parsing callback
 */
void cb_row(int c, void *data) {
    // Type cast to our context structure
    struct csv_context *ctx = (struct csv_context*)data;

    // Put a newline
    ctx->csv_buf = cbuf_putc(ctx->csv_buf, '\n');

    // If we're injecting headers, and we don't have a header length, then
    // this row is a header.  Otherwise, just increment our row count.
    if(ctx->use_header && !ctx->header_len) {
        // Set the length of our header
        ctx->header_len = CBUF_POS(ctx->csv_buf);
        
        // Only increment our row count if we're counting header rows
        if(ctx->count_header) {
            ctx->row++;
        }
    } else {
        // Increment row count
        ctx->row++;
    }

    // If we're at or above our row limit, either keep track/ of the position
    // of this row (if we're grouping columns), or write these rows to disk.
    if(ctx->row >= ctx->max_rows) {
        // Mark the position of this row if we're grouping columns, or flush
        if(ctx->gcol >= 0) {
            ctx->opos = CBUF_POS(ctx->csv_buf);
        } else {
            flush_file(ctx, 0);
        }
    }

    // Back on column zero
    ctx->col=0;

    // We don't need a comma for the next column
    ctx->put_comma = 0;
}
Example #10
0
File: tox.c Project: Boerde/uTox
static void write_save(Tox *tox)
{
    void *data;
    uint32_t size;
    uint8_t path_tmp[512], path_real[512], *p;
    FILE *file;

    size = tox_size(tox);
    data = malloc(size);
    tox_save(tox, data);

    p = path_real + datapath(path_real);
    memcpy(p, "tox_save", sizeof("tox_save"));

    unsigned int path_len = (p - path_real) + sizeof("tox_save");
    memcpy(path_tmp, path_real, path_len);
    memcpy(path_tmp + (path_len - 1), ".tmp", sizeof(".tmp"));

    file = fopen((char*)path_tmp, "wb");
    if(file) {
        fwrite(data, size, 1, file);
        flush_file(file);
        fclose(file);
        if (rename((char*)path_tmp, (char*)path_real) != 0) {
            debug("Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real);
            remove((const char *)path_real);
            if (rename((char*)path_tmp, (char*)path_real) != 0) {
                debug("Saving Failed\n");
            } else {
                debug("Saved data\n");
            }
        } else {
            debug("Saved data\n");
        }
    }

    free(data);
}
Example #11
0
int     main(int argc, char **argv)
{
    struct stat st;
    char   *slash;
    int     c;
    int     fd;
    int     mode = PQ_MODE_DEFAULT;
    char   *site_to_flush = 0;
    char   *id_to_flush = 0;
    ARGV   *import_env;
    int     bad_site;

    /*
     * Fingerprint executables and core dumps.
     */
    MAIL_VERSION_STAMP_ALLOCATE;

    /*
     * Be consistent with file permissions.
     */
    umask(022);

    /*
     * To minimize confusion, make sure that the standard file descriptors
     * are open before opening anything else. XXX Work around for 44BSD where
     * fstat can return EBADF on an open file descriptor.
     */
    for (fd = 0; fd < 3; fd++)
	if (fstat(fd, &st) == -1
	    && (close(fd), open("/dev/null", O_RDWR, 0)) != fd)
	    msg_fatal_status(EX_UNAVAILABLE, "open /dev/null: %m");

    /*
     * Initialize. Set up logging, read the global configuration file and
     * extract configuration information. Set up signal handlers so that we
     * can clean up incomplete output.
     */
    if ((slash = strrchr(argv[0], '/')) != 0 && slash[1])
	argv[0] = slash + 1;
    msg_vstream_init(argv[0], VSTREAM_ERR);
    msg_cleanup(unavailable);
    msg_syslog_init(mail_task("postqueue"), LOG_PID, LOG_FACILITY);
    set_mail_conf_str(VAR_PROCNAME, var_procname = mystrdup(argv[0]));

    /*
     * Check the Postfix library version as soon as we enable logging.
     */
    MAIL_VERSION_CHECK;

    /*
     * Parse JCL. This program is set-gid and must sanitize all command-line
     * parameters. The configuration directory argument is validated by the
     * mail configuration read routine. Don't do complex things until we have
     * completed initializations.
     */
    while ((c = GETOPT(argc, argv, "c:fi:ps:v")) > 0) {
	switch (c) {
	case 'c':				/* non-default configuration */
	    if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
		msg_fatal_status(EX_UNAVAILABLE, "out of memory");
	    break;
	case 'f':				/* flush queue */
	    if (mode != PQ_MODE_DEFAULT)
		usage();
	    mode = PQ_MODE_FLUSH_QUEUE;
	    break;
	case 'i':				/* flush queue file */
	    if (mode != PQ_MODE_DEFAULT)
		usage();
	    mode = PQ_MODE_FLUSH_FILE;
	    id_to_flush = optarg;
	    break;
	case 'p':				/* traditional mailq */
	    if (mode != PQ_MODE_DEFAULT)
		usage();
	    mode = PQ_MODE_MAILQ_LIST;
	    break;
	case 's':				/* flush site */
	    if (mode != PQ_MODE_DEFAULT)
		usage();
	    mode = PQ_MODE_FLUSH_SITE;
	    site_to_flush = optarg;
	    break;
	case 'v':
	    if (geteuid() == 0)
		msg_verbose++;
	    break;
	default:
	    usage();
	}
    }
    if (argc > optind)
	usage();

    /*
     * Further initialization...
     */
    mail_conf_read();
    /* Re-evaluate mail_task() after reading main.cf. */
    msg_syslog_init(mail_task("postqueue"), LOG_PID, LOG_FACILITY);
    mail_dict_init();				/* proxy, sql, ldap */
    get_mail_conf_str_table(str_table);

    /*
     * This program is designed to be set-gid, which makes it a potential
     * target for attack. If not running as root, strip the environment so we
     * don't have to trust the C library. If running as root, don't strip the
     * environment so that showq can receive non-default configuration
     * directory info when the mail system is down.
     */
    if (geteuid() != 0) {
	import_env = mail_parm_split(VAR_IMPORT_ENVIRON, var_import_environ);
	clean_env(import_env->argv);
	argv_free(import_env);
    }
    if (chdir(var_queue_dir))
	msg_fatal_status(EX_UNAVAILABLE, "chdir %s: %m", var_queue_dir);

    signal(SIGPIPE, SIG_IGN);

    /* End of initializations. */

    /*
     * Further input validation.
     */
    if (site_to_flush != 0) {
	bad_site = 0;
	if (*site_to_flush == '[') {
	    bad_site = !valid_mailhost_literal(site_to_flush, DONT_GRIPE);
	} else {
	    bad_site = !valid_hostname(site_to_flush, DONT_GRIPE);
	}
	if (bad_site)
	    msg_fatal_status(EX_USAGE,
	      "Cannot flush mail queue - invalid destination: \"%.100s%s\"",
		   site_to_flush, strlen(site_to_flush) > 100 ? "..." : "");
    }
    if (id_to_flush != 0) {
	if (!mail_queue_id_ok(id_to_flush))
	    msg_fatal_status(EX_USAGE,
		       "Cannot flush queue ID - invalid name: \"%.100s%s\"",
		       id_to_flush, strlen(id_to_flush) > 100 ? "..." : "");
    }

    /*
     * Start processing.
     */
    switch (mode) {
    default:
	msg_panic("unknown operation mode: %d", mode);
	/* NOTREACHED */
    case PQ_MODE_MAILQ_LIST:
	show_queue();
	exit(0);
	break;
    case PQ_MODE_FLUSH_SITE:
	flush_site(site_to_flush);
	exit(0);
	break;
    case PQ_MODE_FLUSH_FILE:
	flush_file(id_to_flush);
	exit(0);
	break;
    case PQ_MODE_FLUSH_QUEUE:
	flush_queue();
	exit(0);
	break;
    case PQ_MODE_DEFAULT:
	usage();
	/* NOTREACHED */
    }
}
Example #12
0
int rtp_connection_kick(struct rtp_connection *connection) {
	
	int n, err = 0, retval = 0, i = 0, bytes_available = 0, sk = 0;
	struct hostent *hp;
	char buffer[1000];
	struct rtp_packet *packet;
	fd_set rfds;
	int readchars = 0;
	struct timeval tv;

	packet = create_rtp_packet(get_payload_size(connection));
	if(packet == NULL) {
		err = RTP_PACKET_ALLOC_ERROR;
		goto exit_no_packet;
	}


	/* Initialize rtp packet */
	init_rtp_packet(packet, connection->seq_no, connection->timestamp,
		connection->ssrc);


	while(1) {
		/* TODO Here we should take into account the
		 * processing time taken to send the packets,
		 * so that we don't introduce extra latency
		 * between samples */
		tv.tv_sec = connection->send_interval.tv_sec;
		tv.tv_usec = connection->send_interval.tv_usec;

		/* Watch stdin (fd 0) to see when it has input. */
		FD_ZERO(&rfds);
		FD_SET(0, &rfds);

		retval = select(fileno(stdin)+1, &rfds, NULL, NULL, &tv);

		if (retval < 0) {
			perror("Error in select: ");
			goto exit;
		}
		
		// Check amount of data in pipe
		err = ioctl(connection->data_input, FIONREAD, &bytes_available);
		if(err != 0) {
			perror("Error while checking size of pipe: ");
			goto exit;
		}

		if (FD_ISSET(fileno(stdin), &rfds)) { // stdin
			read(fileno(stdin), buffer, 1);
			if(buffer[0] == 'e' || buffer[0] == 'E') {
				goto exit;
			}
			else if(buffer[0] == 'f' || buffer[0] == 'F') { // Flush
				flush_file(connection->data_input);
				continue;
			}
		}
		
		if (bytes_available >= packet->payload_size) { // audio stream
			readchars = read(connection->data_input, packet->payload, packet->payload_size);
			for(i = 0; i < connection->howmany; i++) {
				sk = (connection->destinations[i].addr.ss_family ==
					AF_INET) ? connection->bind_sk4 :
					connection->bind_sk6;
				n = sendto(sk, packet->start,
					packet->packet_size, 0,
					(const struct sockaddr *)
					&connection->destinations[i].addr,
					connection->destinations[i].length);
				if (n < 0) {
					err = UDP_SEND_ERROR;
					goto exit;
				}
			}
			
//			printf(".");
//			fflush(stdout);

			connection->seq_no++;
			connection->timestamp +=
				get_timestamp_per_packet_inc(connection);
			/* TODO packet no as seq no might wrap */
			init_rtp_packet(packet, htons(connection->seq_no),
				htonl(connection->timestamp), connection->ssrc);

		}

	}
	
exit:	
	free(packet);
exit_no_packet:
	return err;
}
Example #13
0
	ThreadedOFStreamWriter::~ThreadedOFStreamWriter()
	{
      flush_file();
    }
// Loop forever, converting the incoming GTH data to libpcap format
static void
convert_to_pcap(GTH_api *api,
		int data_socket,
		const char *base_name,
		const int n_sus_per_file,
		const int duration_per_file,
		Channel_t channels[],
		int n_channels,
		const enum PCap_format format)
{
  u16 length;
  GTH_mtp2 signal_unit;
  int su_count;
  int file_number = 1;
  HANDLE_OR_FILEPTR file;
  int write_to_stdout = 0;
  int write_to_pipe;

  write_to_stdout = (strcmp(base_name, "-") == 0);
  write_to_pipe = is_filename_a_pipe(base_name);

  init_timer(duration_per_file);

  while (1) {
    char filename[MAX_FILENAME];

    if (!write_to_stdout && !write_to_pipe)
      {
	snprintf(filename, MAX_FILENAME, "%s.%d",
		 base_name, file_number);
	open_file_for_writing(&file, filename);
	fprintf(stderr, "saving to file %s\n", filename);
      }
    else if (write_to_stdout)
      {
	file = stdout_handle_or_file();
	fprintf(stderr, "saving capture to stdout\n");
      }
    else
      {
	fprintf(stderr, "saving capture to a windows named pipe\n");
	file = open_windows_pipe(base_name);
      }

    write_pcap_global_header(file, format, channels, n_channels);

    file_number++;
    su_count = 0;

    do
      {
	if (wait_for_packet(api, data_socket) != 0)
	  {
	    read_exact(data_socket, (void*)&length, sizeof length);
	    length = ntohs(length);
	    assert(length <= sizeof signal_unit);
	    read_exact(data_socket, (void*)&signal_unit, length);

	    length -= (signal_unit.payload - (char*)&(signal_unit.tag));
	    write_packet(file,
			 ntohs(signal_unit.timestamp_hi),
			 ntohl(signal_unit.timestamp_lo),
			 ntohs(signal_unit.tag),
			 signal_unit.payload,
			 length,
			 format);
	    flush_file(file);
	    su_count++;
	  }
      }
    while ( !is_time_to_rotate(su_count, n_sus_per_file, duration_per_file)
	    || write_to_pipe
	    || write_to_stdout );

    fclose(file);
  }
}
// Loop forever, converting the incoming GTH data to libpcap format
static void
convert_to_pcap(GTH_api *api,
int data_socket,
const char *base_name,
const int n_sus_per_file,
const int duration_per_file,
const int stop_after_interval,
const int output_filename_format,
Channel_t channels[],
int n_channels,
const enum PCap_format format)
{
	u16 length;
	GTH_mtp2 signal_unit;
	int su_count;
	int file_number = 1;
	HANDLE_OR_FILEPTR file;
	int write_to_stdout = 0;
	int write_to_pipe;

	write_to_stdout = (strcmp(base_name, "-") == 0);
	write_to_pipe = is_filename_a_pipe(base_name);

	init_timer(duration_per_file);

	int always_true = 1;
	time_t rawtime;
	struct tm * timeinfo = NULL;

	unsigned long long interval_threshold=0;
		
	u32 curr_sec = 0;
	u32 curr_usec = 0;
	unsigned long long curr_ts;

	while (always_true) {
		char filename[MAX_FILENAME];

		if (!write_to_stdout && !write_to_pipe)
		{
			if (output_filename_format > 0)
			{
				if(timeinfo==NULL) //setting interval time
					time(&rawtime);
				else
					rawtime+=duration_per_file; //hard setting next interval (duration_per_file is in seconds)

				if (output_filename_format == 1)
					timeinfo = localtime(&rawtime); // local time
				else
					timeinfo = gmtime(&rawtime); // utc time
				
				interval_threshold=convert_epoch_micro(rawtime+duration_per_file);
				
				fprintf(stderr, "interval started at %04d/%02d/%02d %02d:%02d:%02d\n",
						timeinfo->tm_year + 1900,
						timeinfo->tm_mon + 1,
						timeinfo->tm_mday,
						timeinfo->tm_hour,
						timeinfo->tm_min,
						timeinfo->tm_sec);
						
				fprintf(stderr, "threshold epoch: %llu\n", interval_threshold);	
			}

			if (!stop_after_interval)
			{
				if (output_filename_format > 0)
				{
					snprintf(filename, MAX_FILENAME, "%s_%05d_%04d%02d%02d%02d%02d%02d",
						base_name, file_number,
						timeinfo->tm_year + 1900,
						timeinfo->tm_mon + 1,
						timeinfo->tm_mday,
						timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
				}
				else
				{
					snprintf(filename, MAX_FILENAME, "%s_%05d",
						base_name, file_number);
				}
			}
			else
			{
				if (output_filename_format > 0)
				{
					snprintf(filename, MAX_FILENAME, "%s_%04d%02d%02d%02d%02d%02d",
						base_name,
						timeinfo->tm_year + 1900,
						timeinfo->tm_mon + 1,
						timeinfo->tm_mday,
						timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
				}
				else
				{
					snprintf(filename, MAX_FILENAME, "%s",
						base_name);
				}
			}
			open_file_for_writing(&file, filename);
			fprintf(stderr, "saving to file %s\n", filename);
		}
		else if (write_to_stdout)
		{
			file = stdout_handle_or_file();
			fprintf(stderr, "saving capture to stdout\n");
		}
		else
		{
			fprintf(stderr, "saving capture to a windows named pipe\n");
			file = open_windows_pipe(base_name);
		}

		write_pcap_global_header(file, format, channels, n_channels);

		file_number++;
		su_count = 0;

		int rotation_time_reached = 0;
		do
		{
			if (wait_for_packet(api, data_socket) != 0)
			{
				read_exact(data_socket, (void*)&length, sizeof length);
				length = ntohs(length);
				assert(length <= sizeof signal_unit);
				read_exact(data_socket, (void*)&signal_unit, length);

				curr_sec = ntohs(signal_unit.timestamp_hi);
				curr_usec = ntohl(signal_unit.timestamp_lo);

				length -= (signal_unit.payload - (char*)&(signal_unit.tag));
				write_packet(file,
					curr_sec,
					curr_usec,
					ntohs(signal_unit.tag),
					signal_unit.payload,
					length,
					format);
				flush_file(file);
				su_count++;

				if (!write_to_pipe && !write_to_stdout)
				{
					if (duration_per_file)
					{
						curr_ts = convert_timestamp_micro(curr_sec, curr_usec, format);

						if (curr_ts >= interval_threshold)
						{
							fprintf(stderr, "interval threshold triggered.\n");
							set_timer(duration_per_file);
							break;
						}
					}
				}
			}
		} while (
			!(rotation_time_reached = is_time_to_rotate(su_count, n_sus_per_file, duration_per_file))
			|| write_to_pipe
			|| write_to_stdout
			);

		fclose(file);

		if (!write_to_pipe && !write_to_stdout)
		{
			if (rotation_time_reached && stop_after_interval)
			{
				fprintf(stderr, "stopped capturing when rotation time reached\n");
				always_true = 0;
			}
		}
	}
}