Changeset 1256:24bb686ce5e1 in roaraudio
- Timestamp:
- 02/27/09 02:27:22 (15 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
include/libroar/vio_cmd.h
r1255 r1256 38 38 #include "libroar.h" 39 39 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 40 44 struct roar_vio_cmd_child { 41 45 int opened; … … 63 67 // possible VIOs: 64 68 69 // cmd: 70 ssize_t roar_vio_cmd_read (struct roar_vio_calls * vio, void *buf, size_t count); 71 ssize_t roar_vio_cmd_write (struct roar_vio_calls * vio, void *buf, size_t count); 72 int roar_vio_cmd_nonblock(struct roar_vio_calls * vio, int state); 73 int roar_vio_cmd_sync (struct roar_vio_calls * vio); 74 65 75 /* 66 76 // basic -
libroar/vio_cmd.c
r1255 r1256 112 112 113 113 int 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; 115 171 } 116 172 … … 135 191 } 136 192 193 // VIOs: 194 195 ssize_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 212 ssize_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 230 int 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 241 int 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 137 263 //ll
Note: See TracChangeset
for help on using the changeset viewer.