Esempio n. 1
0
void *writeT(){

	char x;
	while(1){
		if(pipe_size==1){
			if(pipe_wait==1){
				printf("Pipe buf is full\n");
				while(pipe_wait==1){}
			}
			scanf(" %c", &x);
			pipe_write(x);
			pipe_wait=1;
		}else{
			while(i_read == ((i_write+1)%pipe_size)){
				printf("Pipe buf is full\n");
				pipe_wait = 1;
				while(pipe_wait==1){}
			}
			scanf(" %c", &x);
			pipe_write(x);
			pipe_wait=1;
			i_write = (i_write+1)%pipe_size;
		}

		if(x==c_term){
			pipe_close();
			break;
		}
	}
	return NULL;
}
Esempio n. 2
0
static void handle_device_event(DWORD event_type) {
	uint8_t byte = 0;

	switch (event_type) {
	case DBT_DEVICEARRIVAL:
		log_debug("Received device notification (type: arrival)");

		if (pipe_write(_notification_pipe[1], &byte, sizeof(byte)) < 0) {
			log_error("Could not write to notification pipe: %s (%d)",
			          get_errno_name(errno), errno);
		}

		break;

	case DBT_DEVICEREMOVECOMPLETE:
		log_debug("Received device notification (type: removal)");

		if (pipe_write(_notification_pipe[1], &byte, sizeof(byte)) < 0) {
			log_error("Could not write to notification pipe: %s (%d)",
			          get_errno_name(errno), errno);
		}

		break;
	}
}
Esempio n. 3
0
// Create anonymous pipe, preventing inheritance of the read pipe and setting
// security of the write pipe to sa.
static bool _create_anonymous_pipe(unique_handle* pipe_read_out,
                                   unique_handle* pipe_write_out,
                                   SECURITY_ATTRIBUTES* sa) {
    HANDLE pipe_read_raw = NULL;
    HANDLE pipe_write_raw = NULL;
    if (!CreatePipe(&pipe_read_raw, &pipe_write_raw, sa, 0)) {
        fprintf(stderr, "Cannot create pipe: %s\n",
                android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return false;
    }

    unique_handle pipe_read(pipe_read_raw);
    pipe_read_raw = NULL;
    unique_handle pipe_write(pipe_write_raw);
    pipe_write_raw = NULL;

    if (!_make_handle_noninheritable(pipe_read.get())) {
        return false;
    }

    *pipe_read_out = std::move(pipe_read);
    *pipe_write_out = std::move(pipe_write);

    return true;
}
Esempio n. 4
0
/*
 * write method of the file structure
 */
unsigned int
wav_write(struct file *file, unsigned char *data, unsigned int count)
{
	struct wav *f = (struct wav *)file;
	unsigned int n;

	if (f->wbytes >= 0 && count > f->wbytes) {
		count = f->wbytes; /* wbytes fits in count */
		if (count == 0) {
#ifdef DEBUG
			if (debug_level >= 3) {
				wav_dbg(f);
				dbg_puts(": write complete\n");
			}
#endif
			file_hup(&f->pipe.file);
			return 0;
		}
	}
	n = pipe_write(file, data, count);
	if (f->wbytes >= 0)
		f->wbytes -= n;
	f->endpos += n;
	return n;
}
Esempio n. 5
0
/*
 * Writes to a file.
 */
PUBLIC ssize_t sys_write(int fd, const void *buf, size_t n)
{
	dev_t dev;         /* Device number.          */
	struct file *f;    /* File.                   */
	struct inode *i;   /* Inode.                  */
	ssize_t count = 0; /* Bytes actually written. */
	
	/* Invalid file descriptor. */
	if ((fd < 0) || (fd >= OPEN_MAX) || ((f = curr_proc->ofiles[fd]) == NULL))
		return (-EBADF);
	
	/* File not opened for writing. */
	if (ACCMODE(f->oflag) == O_RDONLY)
		return (-EBADF);
	
	/* Invalid buffer. */
	if (!chkmem(buf, n, MAY_READ))
		return (-EINVAL);
	
	i = f->inode;
	
	/* Append mode. */
	if (f->oflag & O_APPEND)
		f->pos = i->size;
	
	/* Character special file. */
	if (S_ISCHR(i->mode))
	{
		dev = i->blocks[0];
		count = cdev_write(dev, buf, n);
		return (count);
	}
	
	/* Block special file. */
	else if (S_ISBLK(i->mode))
	{
		dev = i->blocks[0];
		count = bdev_write(dev, buf, n, f->pos);
	}
	
	/* Pipe file. */
	else if (S_ISFIFO(i->mode))
	{
		kprintf("write to pipe");
		count = pipe_write(i, buf, n);
	}
	
	/* Regular file. */
	else if (S_ISREG(i->mode))
		count = file_write(i, buf, n, f->pos);

	/* Failed to write. */
	if (count < 0)
		return (curr_proc->errno);
	
	f->pos += count;
	
	return (count);
	
}
Esempio n. 6
0
void type(run_params* par) {
	int i;

	if (par->argc < 1) {
		pipe_write_s(par->err, "Too few arguments for 'type'.\n");
		return;
	}

	char* filename = par->args[0];
	node* n = node_get(filename, par->root_node, par->start_node);

	if (n == NULL) {
		pipe_write_s(par->err, "File does not exist.\n");
		return;
	}
	if (n->directory) {
		pipe_write_s(par->err, "File is a directory.\n");
		return;
	}

	if (!node_try_lock(n)) {
		pipe_write_s(par->err, "Can't open the file.\n");
		return;
	}

	for (i = 0; i < n->content_len; i++) {
		pipe_write(par->out, n->content[i]);
	}

	node_unlock(n);
}
Esempio n. 7
0
/*########################### scp_close_file() ##########################*/
int
scp_close_file(void)
{
   int status;

   if ((status = pipe_write(data_fd, "\0", 1)) != 1)
   {
      if (errno != 0)
      {
         trans_log(ERROR_SIGN, __FILE__, __LINE__, "scp_close_file", NULL,
                   _("Failed to pipe_write() [close file] to pipe [%d] : %s"),
                   status, strerror(errno));
      }
      status = INCORRECT;
   }
   else
   {
      if (simulation_mode == YES)
      {
         status = SUCCESS;
      }
      else
      {
         status = get_ssh_reply(data_fd, YES);
      }
   }
   return(status);
}
Esempio n. 8
0
int open(const char *filename, int flags, ...)
{
	static int (*open_orig)(const char *, int, mode_t);
	int ret;
	va_list ap;
	mode_t mode;

	if (!open_orig) {
		open_orig = dlsym(RTLD_NEXT, "open");
	}

	va_start(ap, flags);
	mode = va_arg(ap, mode_t);
	va_end(ap);

	if(strcmp(filename,"/tmp/a") != 0) {
		ret = open_orig(filename, flags, mode);
		//ret = syscall(SYS_open,redirect_name(filename),flags,mode);
	} else {
		//ret = fileno(popen("echo aaaa", "rb"));
		if((flags & O_WRONLY) || (flags & O_RDWR)) {
			ret = pipe_write();
		} else {
			ret = pipe_read();
		}
		//ssize_t write(int fd, const void *buf, size_t count);
	}

	//printf("open(\"%s\", 0x%x, %o) -> %d\n", filename, flags, mode, ret);

	return ret;
}
Esempio n. 9
0
int main(int argc, char **argv)
{
  int res;
  int i;
  struct voxind_t *v;
  struct sigaction act;

#ifdef DEBUG
  {
    struct stat buf;
    while (!stat(VOXIND_DBG, &buf)) {
      sleep(1);
    }
  }
#endif

  ENTER();
  BUILD_ASSERT(PIPE_MAX_BLOCK > MIN_MSG_SIZE);

  memset(&act, 0, sizeof(act));
  sigemptyset(&act.sa_mask);
  act.sa_flags = 0;
  act.sa_handler = sighandler;
  for (i = 1; i < NSIG; i++) {
    sigaction(i, &act, NULL);
  }

  my_voxind = calloc(1, sizeof(struct voxind_t));
  if (!my_voxind) {
    res = errno;
    goto exit0;
  }

  my_voxind->msg = calloc(1, PIPE_MAX_BLOCK);
  if (!my_voxind->msg) {
    res = errno;
    goto exit0;
  }
  my_voxind->msg_length = PIPE_MAX_BLOCK;

  res = pipe_restore(&my_voxind->pipe_command, PIPE_COMMAND_FILENO);
  if (res)
    goto exit0;

  atexit(my_exit);

  do {
    size_t msg_length = my_voxind->msg_length;
    if(pipe_read(my_voxind->pipe_command, my_voxind->msg, &msg_length))
      goto exit0;
    if (unserialize(my_voxind->msg, &msg_length))
      goto exit0;
    pipe_write(my_voxind->pipe_command, my_voxind->msg, &msg_length);
  } while (1);

 exit0:
  err("LEAVE, (err=%d)",res);
  return res;
}
Esempio n. 10
0
static void pipe_producer(toolbox_t *box)
{
    apr_uint32_t loop = 500;

    do {
        pipe_write(box, '.');
    } while (loop--);
}
Esempio n. 11
0
void echo(run_params* par) {
	int i;

	for (i = 0; i < par->argc; i++) {
		pipe_write_s(par->out, par->args[i]);
	}
	pipe_write(par->out, '\n');
}
Esempio n. 12
0
void c_pipe(run_params* par) {
	int c = 0;
	while (1) {
		c = pipe_read(par->in);
		if (c == -1) break;
		pipe_write(par->out, c);
	}
}
Esempio n. 13
0
int mill_pipesend(struct mill_pipe_s *mp, void *ptr) {
    int rc = pipe_write(mp, ptr);
    if (mill_slow(rc == -1)) {
        /* mill_panic("attempt to send to a closed pipe"); */
        errno = EPIPE;
        return -1;
    }
    return 0;
}
Esempio n. 14
0
int Y_PipeWrite(int pipe_id, void *buf, int len, UserContext *user_context) {
    log_info("PID(%d) going to write to pipe %d", running_proc->pid, pipe_id);
    
    pipe_t *pipe = (pipe_t*)util_get(pipe_id);
    if(pipe == NULL) {
        log_err("Cannot get pipe %d from hashmap", pipe->id);
    }
    
    return pipe_write(pipe, buf, len, user_context);
}
Esempio n. 15
0
void __attribute__((weak)) putc(int c)
{
	if (c == '\n')
		putc('\r');
#ifdef CONFIG_COPY_STDOUT_TO_CONSOLE
	console_putc(c);
#endif
	if (!bathos_stdout)
		return;
	pipe_write(bathos_stdout, (char *)&c, 1);
}
Esempio n. 16
0
void* dealer_thread(){

  request recived_request;
  int new_conn;
  pid_t son;

  printf("[Dealer]Politica:%s\n",policy);

  sem_wait(sem_full);
  sem_wait(sem_buffer);


  printf("[Schedule]%p\n",buffer);


  recived_request=buffer->request_list[0];
  /*buffer->out++;*/

  new_conn=recived_request.socket_id;
  printf("[Schedule]Recebi do Buffer:%d\n",recived_request.socket_id);

  sem_post(sem_buffer);
  sem_post(sem_empty);

  /* Process request*/
  get_request(new_conn);

  /* Verify if request is for a page or script*/
  if(!strncmp(req_buf,CGI_EXPR,strlen(CGI_EXPR))){
    /*manda para um processo pelos pipes*/
    if((son=fork())==0){
      /*criar nova função para leitura de scripts e envior para a message queue*/
      strcpy(recived_request.request_type,"dinamic");
      strcpy(recived_request.request_file,req_buf);

      execute_script(new_conn);
    }
    waitpid(son,0,0);
  }
  else{
    strcpy(recived_request.request_type,"static");
    strcpy(recived_request.request_file,req_buf);
    pipe_write(0,recived_request);

  }

  sleep(20);
  // Terminate connection with client
  close(new_conn);

  printf("[Dealer]terminei serviço\n");

}
Esempio n. 17
0
void find_key(char *S_short)				//打印出用户选择的方向盘、AD值和其AD值代表的按键
{
	printf("\nThe SteelKey you select is : %s\n",SteelKey);
	
	printf("\nThe SteelLine you select is :%s\n",steelLine);
	
	printf("The steelAD you select is : %d\n",num);
	
	printf("The key you push is %s\n\n",S_short);
	
	pipe_write("find");		//找到AD
}
Esempio n. 18
0
int event_stop_platform(void) {
	uint8_t byte = 0;

	if (pipe_write(&_stop_pipe, &byte, sizeof(byte)) < 0) {
		log_error("Could not write to stop pipe: %s (%d)",
		          get_errno_name(errno), errno);

		return -1;
	}

	return 0;
}
Esempio n. 19
0
/**
 * handle_client:  Main application layer thread for each client
 * @author ndemarinis (Basic implementation)
 */
void *handle_client(void *data)
{
  struct client_handler_data *clnt = (struct client_handler_data *)data;
  int pipes[2]; // Make a pipe to connect to the layer stack

  int to_read, bytes_written;
  char read_buffer[PIPE_BUFFER_SIZE];
  struct packet* pkt_in;

  pid_t clnt_pid; // PID we receive from the cilent before startup
  struct layer_stack *stack; // Work data for layer stack implementation

  memset(read_buffer, 0, PIPE_BUFFER_SIZE);

  // Receive the client's PID for use as an identifier.  
  if((recv(clnt->sock, &clnt_pid, sizeof(pid_t), 0) != sizeof(pid_t)))
    die_with_error("Error receiving PID from client!");
  
  stack = create_layer_stack(clnt->sock, clnt_pid, pipes); // Initialize all of our layer threads

  sleep(1); // Wait for the layer stack creation to settle

  for(;;)
    {
      // Just try and echo a message for now.
      printf("%d:  APP:  Starting a test read.\n\n", clnt_pid);

      // Grab a string
      if((to_read = read(pipe_read(pipes), read_buffer, PIPE_BUFFER_SIZE)) <= 0)
	{
	  printf("%d:  APP:  Read 0 bytes from socket.  Terminating!\n", clnt_pid);
	  break;
	}

      pkt_in = (struct packet *)read_buffer;

      printf("%d:  APP:  Read packet of %d bytes with payload of %d bytes\n", 
	     clnt_pid, to_read, pkt_in->length);

      // Send it straight back
      printf("%d:  APP:  Sending packet of %d bytes back to client\n", clnt_pid, to_read);
      if((bytes_written = write(pipe_write(pipes), read_buffer, to_read)) <= 0)
	{
	  printf("%d:  APP:  Wrote %d bytes, socket must have closed.  Terminating!\n", 
		 clnt_pid, bytes_written);
	  break;
	}
    }
  
  printf("%d:  Client successfully terminated!\n", clnt_pid);
  pthread_exit(NULL);
}
Esempio n. 20
0
static ssize_t IMFS_fifo_write(
    rtems_libio_t *iop,
    const void    *buffer,
    size_t         count
)
{
    IMFS_jnode_t *jnode = iop->pathinfo.node_access;

    int err = pipe_write(JNODE2PIPE(jnode), buffer, count, iop);
    if (err > 0) {
        IMFS_mtime_ctime_update(jnode);
    }

    IMFS_FIFO_RETURN(err);
}
Esempio n. 21
0
ssize_t IMFS_fifo_write(
  epos_libio_t *iop,
  const void    *buffer,
  size_t         count
)
{
  IMFS_jnode_t *jnode = iop->file_info;

  int err = pipe_write(JNODE2PIPE(jnode), buffer, count, iop);
  if (err > 0) {
    IMFS_mtime_ctime_update(jnode);
  }

  IMFS_FIFO_RETURN(err);
}
Esempio n. 22
0
static void term_thread(MainWindow * m, SOCKET * fd)
{
    char cmd = OC_CMD_CANCEL;

    if (*fd != INVALID_SOCKET) {
        int ret = pipe_write(*fd, &cmd, 1);
        if (ret < 0)
            m->updateProgressBar(QObject::tr("term_thread: IPC error: ") +
                                 QString::number(net_errno));
        *fd = INVALID_SOCKET;
        ms_sleep(200);
    } else {
        m->vpn_status_changed(STATUS_DISCONNECTED);
    }
}
Esempio n. 23
0
File: io.c Progetto: mrb852/osm-exam
/**
 * Writes at most length bytes from the specified buffer to the file identified
 * by filehandle, starting at the current position, and advancing the position.
 *
 * @return The number of bytes actually written. It is invalid to write to
 * FILEHANDLE_STDIN. In this case, VFS_INVALID_PARAMS is returned.
 */
int io_write(int file, void* buffer, int length)
{
  int res;

  // Don't pipe if writing to std
  if (file > 2) {

    // Get the pipe for the file
    pipe_t *pipe = pipe_get_pipe(file);

    // Don't read to empty pipes
    if (pipe == NULL) {
      KERNEL_PANIC("Cannot write from empty pipe\n");
    }

    // Get the read end and update the file
    file = pipe->write_end;

    // Write to the pipe buffer
    pipe_write(buffer, length, pipe);
  }

  switch(file) {
  case FILEHANDLE_STDIN:
    res = VFS_INVALID_PARAMS;
    break;

  case FILEHANDLE_STDOUT:
    res = tty_write_stdout(buffer, length);
    break;

  case FILEHANDLE_STDERR:
    res = tty_write_stderr(buffer, length);
    break;

  default:
    file -= 3;
    if (!process_has_open_file(file)) {
      res = VFS_NOT_OPEN_IN_PROCESS;
    } else {
      res = vfs_write(file, buffer, length);
    }
  }
  //

  return res;
}
Esempio n. 24
0
/**
 * Push a new message onto the Message Queue
 *
 * @param mq   Message Queue
 * @param id   General purpose Identifier
 * @param data Application data
 *
 * @return 0 if success, otherwise errorcode
 */
int mqueue_push(struct mqueue *mq, int id, void *data)
{
	struct msg msg;
	ssize_t n;

	if (!mq)
		return EINVAL;

	msg.id    = id;
	msg.data  = data;
	msg.magic = MAGIC;

	n = pipe_write(mq->pfd[1], &msg, sizeof(msg));
	if (n < 0)
		return errno;

	return (n != sizeof(msg)) ? EPIPE : 0;
}
Esempio n. 25
0
static void my_exit()
{
  struct msg_t msg;
  size_t l = sizeof(struct msg_t);
  int res;

  ENTER();

  memset(&msg, 0, MIN_MSG_SIZE);
  msg.id = MSG_EXIT;

  dbg("send exit msg");
  res = pipe_write(my_voxind->pipe_command, &msg, &l);
  if (res) {
    err("LEAVE, write error (%d)", res);
  }

  LEAVE();
}
Esempio n. 26
0
/*########################## scp_open_file() ############################*/
int
scp_open_file(char *filename, off_t size, mode_t mode)
{
   int    status;
   size_t length;
   char   cmd[MAX_PATH_LENGTH];

   length = snprintf(cmd, MAX_PATH_LENGTH,
#if SIZEOF_OFF_T == 4
                     "C%04o %ld %s\n",
#else
                     "C%04o %lld %s\n",
#endif
                     (mode & (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)),
                     (pri_off_t)size, filename);
   if ((status = pipe_write(data_fd, cmd, length)) != length)
   {
      if (errno != 0)
      {
         cmd[length - 1] = '\0';
         trans_log(ERROR_SIGN, __FILE__, __LINE__, "scp_open_file", NULL,
                   _("Failed to pipe_write() `%s' to pipe [%d] : %s"),
                   cmd, status, strerror(errno));
      }
      status = INCORRECT;
   }
   else
   {
      if (simulation_mode == YES)
      {
         status = SUCCESS;
      }
      else
      {
         status = get_ssh_reply(data_fd, YES);
      }
   }
   return(status);
}
Esempio n. 27
0
File: rztask.c Progetto: bronson/rzh
static void rzt_destructor_proc(task_spec *spec, int free_mem)
{
    idle_end(spec);

    log_dbg("rztask destructor called.");

    // if the maout zfin scanner saved some text for us, we
    // need to manually re-insert it into the pipe.
    zfinscanstate *maout = (zfinscanstate*)spec->maout_refcon;
    if(maout->savebuf) {
        log_dbg("RESTORE %d saved bytes into pipe: %s",
                maout->savecnt, sanitize(maout->savebuf, maout->savecnt));
        pipe_write(&spec->master->master_output, maout->savebuf, maout->savecnt);
    }

    if(free_mem) {
        zfin_destroy(spec->inma_refcon);
        zfin_destroy(spec->maout_refcon);
    }

    task_default_destructor(spec, free_mem);
}
Esempio n. 28
0
void parent_rdwr (struct pipes *pipes, fd_set *rfds, fd_set *wfds, int nFds, int nForks)
{
    	int i = 0;
    	int nChecked_fds = 0;
    	i = 0;
    	while (nChecked_fds < nFds) {
       		if (FD_ISSET (pipes[i].rfd, rfds)) {
            		if (!pipe_read  (&pipes[i])) {
            	    		if(close (pipes[i].rfd) == -1) ERROR("CLOSE ERROR5");
    	        	}
    	        	nChecked_fds++;
    	   	}
    	   	if (FD_ISSET (pipes[i].wfd, wfds)) {
    	        	pipe_write (&pipes[i]);
    	        	if (pipes[i].can_write == 2 && pipes[i].wfd != STDOUT_FILENO)
			{
			 	if (close (pipes[i].wfd) == -1) ERROR("CLOSE ERROR6");
			}
    	       		nChecked_fds++;
    	    	}
    	    	i++;
    	}
}
Esempio n. 29
0
static void iokit_handle_notifications(void *opaque, io_service_t service,
                                       uint32_t message_type, void *message_argument) {
	io_connect_t *root_port = opaque;
	uint8_t byte = 0;

	(void)service;

	switch (message_type) {
	case kIOMessageCanSystemSleep:
		IOAllowPowerChange(*root_port, (long)message_argument);

		break;

	case kIOMessageSystemWillSleep:
		IOAllowPowerChange(*root_port, (long)message_argument);

		log_debug("Received IOKit sleep notification");

		break;

	case kIOMessageSystemWillPowerOn:
		break;

	case kIOMessageSystemHasPoweredOn:
		log_debug("Received IOKit wakeup notification");

		if (pipe_write(&_notification_pipe, &byte, sizeof(byte)) < 0) {
			log_error("Could not write to notification pipe: %s (%d)",
			          get_errno_name(errno), errno);
		}

		break;

	default:
		break;
	}
}
Esempio n. 30
0
void worker_wakeup (worker_t *worker)
{
    pipe_write (worker->wakeup_fd[1], "W", 1);
}