Changeset 1256:24bb686ce5e1 in roaraudio


Ignore:
Timestamp:
02/27/09 02:27:22 (15 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

wrote most of the vio_cmd_* code, there is still need to write the read() and write() calls

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • include/libroar/vio_cmd.h

    r1255 r1256  
    3838#include "libroar.h" 
    3939 
     40#define ROAR_VIO_CMD_OPTS_NONE      0x00 
     41#define ROAR_VIO_CMD_OPTS_NONBLOCK  0x01 
     42#define ROAR_VIO_CMD_OPTS_ON_DEMAND 0x02 
     43 
    4044struct roar_vio_cmd_child { 
    4145 int opened; 
     
    6367// possible VIOs: 
    6468 
     69// cmd: 
     70ssize_t roar_vio_cmd_read    (struct roar_vio_calls * vio, void *buf, size_t count); 
     71ssize_t roar_vio_cmd_write   (struct roar_vio_calls * vio, void *buf, size_t count); 
     72int     roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state); 
     73int     roar_vio_cmd_sync    (struct roar_vio_calls * vio); 
     74 
    6575/* 
    6676// basic 
  • libroar/vio_cmd.c

    r1255 r1256  
    112112 
    113113int roar_vio_cmd_fork(struct roar_vio_cmd_child * child) { 
    114  return -1; 
     114 int in[2], out[2]; 
     115 
     116 if ( child == NULL ) 
     117  return -1; 
     118 
     119 if ( child->opened ) 
     120  return 0; 
     121 
     122 if ( child->cmd == NULL ) 
     123  return -1; 
     124 
     125 // open some pipes... 
     126 if ( pipe(in) != 0 ) 
     127  return -1; 
     128 
     129 if ( pipe(out) != 0 ) { 
     130  close(in[0]); 
     131  close(in[1]); 
     132  return -1; 
     133 } 
     134 
     135 child->pid = fork(); 
     136 
     137 switch (child->pid) { 
     138  case -1: 
     139    close(in[0]); 
     140    close(in[1]); 
     141    close(out[0]); 
     142    close(out[1]); 
     143    return -1; 
     144   break; 
     145  case 0: 
     146    close(in[0]); 
     147    close(out[1]); 
     148    close(ROAR_STDIN); 
     149    close(ROAR_STDOUT); 
     150 
     151    if ( dup2(out[0], ROAR_STDIN) == -1 ) 
     152     _exit(1); 
     153 
     154    if ( dup2(in[1], ROAR_STDOUT) == -1 ) 
     155     _exit(1); 
     156 
     157    execlp("/bin/sh", "/bin/sh", "-c", child->cmd, NULL); 
     158 
     159    _exit(1); 
     160   break; 
     161 } 
     162 
     163 close(in[1]); 
     164 close(out[0]); 
     165 
     166 child->opened = 1; 
     167 child->in     = in[0]; 
     168 child->out    = out[1]; 
     169 
     170 return 0; 
    115171} 
    116172 
     
    135191} 
    136192 
     193// VIOs: 
     194 
     195ssize_t roar_vio_cmd_read    (struct roar_vio_calls * vio, void *buf, size_t count) { 
     196 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; 
     197 
     198 if ( !state->reader.opened ) { 
     199  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no reader is forked :) */ 
     200   return 0; 
     201 
     202  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no reader exists? -> err */ 
     203   return -1; 
     204 
     205  if ( roar_vio_cmd_fork(&(state->reader)) == -1 ) 
     206   return -1; 
     207 } 
     208 
     209 return -1; 
     210} 
     211 
     212ssize_t roar_vio_cmd_write   (struct roar_vio_calls * vio, void *buf, size_t count) { 
     213 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; 
     214 
     215 if ( !state->writer.opened ) { 
     216  if ( buf == NULL && count == 0 ) /* sync: no need to do anything if no writer is forked :) */ 
     217   return 0; 
     218 
     219  if ( !(state->options & ROAR_VIO_CMD_OPTS_ON_DEMAND) ) /* we are not on demand and no writer exists? -> err */ 
     220   return -1; 
     221 
     222  if ( roar_vio_cmd_fork(&(state->writer)) == -1 ) 
     223   return -1; 
     224 } 
     225 
     226 
     227 return -1; 
     228} 
     229 
     230int     roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state) { 
     231 struct roar_vio_cmd_state * self = (struct roar_vio_cmd_state *)vio->inst; 
     232 
     233 self->options |= ROAR_VIO_CMD_OPTS_NONBLOCK; 
     234 
     235 if ( state == ROAR_SOCKET_BLOCK ) 
     236  self->options -= ROAR_VIO_CMD_OPTS_NONBLOCK; 
     237 
     238 return 0; 
     239} 
     240 
     241int     roar_vio_cmd_sync    (struct roar_vio_calls * vio) { 
     242 struct roar_vio_cmd_state * state = (struct roar_vio_cmd_state *)vio->inst; 
     243 int oldblock; 
     244 int ret = 0; 
     245 
     246 oldblock = state->options & ROAR_VIO_CMD_OPTS_NONBLOCK ? ROAR_SOCKET_NONBLOCK : ROAR_SOCKET_BLOCK; 
     247 
     248 if ( roar_vio_cmd_nonblock(vio, ROAR_SOCKET_BLOCK) == -1 ) 
     249  return -1; 
     250 
     251 if ( roar_vio_cmd_write(vio, NULL, 0) == -1 ) 
     252  ret = -1; 
     253 
     254 if ( roar_vio_cmd_read(vio, NULL, 0) == -1 ) 
     255  ret = -1; 
     256 
     257 if ( roar_vio_cmd_nonblock(vio, oldblock) == -1 ) 
     258  return -1; 
     259 
     260 return ret; 
     261} 
     262 
    137263//ll 
Note: See TracChangeset for help on using the changeset viewer.