Changeset 5369:dfc6cbfe8025 in roaraudio


Ignore:
Timestamp:
12/21/11 01:08:49 (12 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

moved +fork out of socket.c into basic.c, now also support +fork=d:daemon and +fork=!daemon_command

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • include/libroar/socket.h

    r5364 r5369  
    6262 
    6363int roar_socket_open       (int mode, int type, const char * host, int port); 
    64 int roar_socket_open_fork  (int mode, const char * host, int port); 
    6564int roar_socket_open_file  (int mode, const char * host, int port) _LIBROAR_ATTR_TO_STATIC; 
    6665int roar_socket_open_proxy (int mode, int type, const char * host, int port, const char * proxy_type) _LIBROAR_ATTR_TO_STATIC; 
  • libroar/basic.c

    r5368 r5369  
    3636#include "libroar.h" 
    3737 
     38static int _start_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { 
     39#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) 
     40 enum { 
     41  NORMAL = 0, 
     42  SYSTEM = 1 
     43 } mode = NORMAL; 
     44 const char * daemonimage = NULL; 
     45 int socks[2]; 
     46 int r; 
     47 char fhstr[12]; 
     48 
     49 if ( !strncmp(server, "+fork=", 6) ) { 
     50  server += 6; 
     51  if ( server[0] == 0 ) { 
     52   // no special case, we just ignore it. 
     53  } else if ( server[0] == 'd' && server[1] == ':' ) { 
     54   server += 2; 
     55   daemonimage = server; 
     56  } else if ( server[0] == '!' ) { 
     57   server += 1; 
     58   daemonimage = server; 
     59   mode = SYSTEM; 
     60  } else { 
     61   roar_err_set(ROAR_ERROR_ILLSEQ); 
     62   return -1; 
     63  } 
     64 } 
     65 
     66 // TODO: FIXME: we should move this into the config structure. 
     67 if ( daemonimage == NULL ) 
     68  daemonimage = getenv("ROAR_DAEMONIMAGE"); 
     69 
     70 if ( daemonimage == NULL || *daemonimage == 0 ) 
     71  daemonimage = "roard"; 
     72 
     73 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { 
     74  roar_err_from_errno(); 
     75  return -1; 
     76 } 
     77 
     78 r = fork(); 
     79 
     80 if ( r == -1 ) { // error! 
     81  roar_err_from_errno(); 
     82  ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno)); 
     83  close(socks[0]); 
     84  close(socks[1]); 
     85  return -1; 
     86 } else if ( r == 0 ) { // we are the child 
     87  close(socks[0]); 
     88 
     89  close(ROAR_STDIN ); // we do not want roard to have any standard input 
     90  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, 
     91                      // but STDERR we keep open for error messages. 
     92 
     93  snprintf(fhstr, sizeof(fhstr), "%i", socks[1]); 
     94 
     95  switch (mode) { 
     96   case NORMAL: 
     97     execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (_LIBROAR_GOOD_CAST char*)NULL); 
     98    break; 
     99   case SYSTEM: 
     100     dup2(socks[1], ROAR_STDIN ); 
     101     dup2(socks[1], ROAR_STDOUT); 
     102     execl("/bin/sh", "/bin/sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); 
     103     execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL); 
     104    break; 
     105  } 
     106 
     107  // we are still alive? 
     108  ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!"); 
     109  _exit(1); 
     110 } else { // we are the parent 
     111  close(socks[1]); 
     112  if ( roar_vio_open_fh_socket(con->viocon, socks[0]) == -1 ) { 
     113   close(socks[0]); 
     114   return -1; 
     115  } else { 
     116   con->flags |= ROAR_CON_FLAGS_VIO; 
     117  } 
     118  return 0; 
     119 } 
     120 
     121 return -1; 
     122#else 
     123 ROAR_ERR("roar_socket_open_fork(*): There is no UNIX Domain Socket support in win32, download a real OS."); 
     124 return -1; 
     125#endif 
     126} 
     127 
    38128static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { 
    39129#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) 
     
    61151  con->flags |= ROAR_CON_FLAGS_VIO; 
    62152  return 0; 
     153 } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) { 
     154  return _start_server(con, server, type, flags, timeout); 
    63155 } 
    64156 
     
    288380 } else { 
    289381  /* connect via (char*)server */ 
    290   if ( _connect_server(con, server, -1, flags, timeout) == 0 ) 
    291    return 0; 
     382  if ( _connect_server(con, server, -1, flags, timeout) != 0 ) 
     383   return -1; 
     384  return 0; 
    292385 } 
    293386 
  • libroar/socket.c

    r5364 r5369  
    795795#endif 
    796796 } else if ( type == ROAR_SOCKET_TYPE_FORK ) { 
    797   return roar_socket_open_fork(mode, host, port); 
     797  roar_err_set(ROAR_ERROR_INVAL); 
     798  return -1; 
    798799 } else if ( type == ROAR_SOCKET_TYPE_FILE ) { 
    799800  return roar_socket_open_file(mode, host, port); 
     
    817818 
    818819 return fh; 
    819 } 
    820  
    821 int roar_socket_open_fork  (int mode, const char * host, int port) { 
    822 #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) 
    823  char * daemonimage; 
    824  int socks[2]; 
    825  int r; 
    826  char fhstr[8]; 
    827  
    828  if ( mode == MODE_LISTEN ) 
    829   return -1; 
    830  
    831  // TODO: FIXME: we should move this into the config structure. 
    832  daemonimage = getenv("ROAR_DAEMONIMAGE"); 
    833  
    834  if ( daemonimage == NULL || *daemonimage == 0 ) 
    835   daemonimage = "roard"; 
    836  
    837  if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { 
    838   return -1; 
    839  } 
    840  
    841  r = fork(); 
    842  
    843  if ( r == -1 ) { // error! 
    844   ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno)); 
    845   close(socks[0]); 
    846   close(socks[1]); 
    847   return -1; 
    848  } else if ( r == 0 ) { // we are the child 
    849   close(socks[0]); 
    850  
    851   close(ROAR_STDIN ); // we do not want roard to have any standard input 
    852   close(ROAR_STDOUT); // STDOUT is also not needed, so we close it, 
    853                       // but STDERR we keep open for error messages. 
    854  
    855   snprintf(fhstr, 7, "%i", socks[1]); 
    856  
    857   execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (_LIBROAR_GOOD_CAST char*)NULL); 
    858  
    859   // we are still alive? 
    860   ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!"); 
    861   _exit(1); 
    862  } else { // we are the parent 
    863   close(socks[1]); 
    864   return socks[0]; 
    865  } 
    866  
    867  return -1; 
    868 #else 
    869  ROAR_ERR("roar_socket_open_fork(*): There is no UNIX Domain Socket support in win32, download a real OS."); 
    870  return -1; 
    871 #endif 
    872820} 
    873821 
Note: See TracChangeset for help on using the changeset viewer.