Changeset 5373:8da157c10483 in roaraudio


Ignore:
Timestamp:
12/21/11 19:02:52 (12 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:
  • Updated config structure
  • Added a flag to ask libroar to only connect to local servers
  • Added a way to override fork()
  • added support for +internal
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • include/libroar/config.h

    r5357 r5373  
    3939#include "libroar.h" 
    4040 
     41struct roar_connection; 
     42 
    4143// WorkAroundS: 
    4244#define ROAR_LIBROAR_CONFIG_WAS_NONE        0x00 
     
    6769 ROAR_LIBROAR_CONFIG_OPMODE_MS     = 2, 
    6870#define ROAR_LIBROAR_CONFIG_OPMODE_MS       ROAR_LIBROAR_CONFIG_OPMODE_MS 
     71}; 
     72 
     73struct roar_libroar_forkapi { 
     74 int   (*prefork)(void ** context, void * userdata); 
     75 pid_t (*fork   )(void ** context, void * userdata); 
     76 int   (*failed )(void ** context, void * userdata); 
     77 int   (*parent )(void ** context, void * userdata, pid_t child); 
     78 int   (*child  )(void ** context, void * userdata); 
     79 void * userdata; 
    6980}; 
    7081 
     
    115126#endif 
    116127 enum roar_libroar_config_opmode opmode; 
     128 struct roar_libroar_forkapi * forkapi; 
     129 struct roar_vio_calls * (*connect_internal)(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout); 
     130 char * daemonimage; 
     131 int serverflags; 
    117132}; 
    118133 
     
    129144const char * roar_libroar_get_server(void) _LIBROAR_ATTR_USE_RESULT; 
    130145 
     146int    roar_libroar_set_forkapi(struct roar_libroar_forkapi * api); 
     147 
     148int    roar_libroar_set_connect_internal(struct roar_vio_calls * (*func)(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout)); 
     149 
    131150void   roar_libroar_nowarn(void); 
    132151void   roar_libroar_warn(void); 
  • include/libroar/enumdev.h

    r4708 r5373  
    4040 
    4141#define ROAR_ENUM_FLAG_NONE         0x0000 /* no flags set                     */ 
    42 #define ROAR_ENUM_FLAG_DESC         0x0001 /* ask for server description       */ 
     42#define ROAR_ENUM_FLAG_DESC         0x0001 /* ask for server description     1 */ 
    4343#define ROAR_ENUM_FLAG_LOCATION     0x0002 /* ask for server location        1 */ 
    44 #define ROAR_ENUM_FLAG_NONBLOCK     0x0004 /* do not block                   1 */ 
     44#define ROAR_ENUM_FLAG_NONBLOCK     0x0004 /* do not block                     */ 
    4545#define ROAR_ENUM_FLAG_HARDNONBLOCK 0x0008 /* do even less block than NONBLOCK */ 
     46#define ROAR_ENUM_FLAG_LOCALONLY    0x0010 /* only list local servers          */ 
    4647/* 
    4748 * 1 = This is a request. The result may include or not include the data anyway. 
    48  *     This is only so the lib does not need to spend extra work if data is not needed. 
     49 *     This is only so the lib does not need to spend extra work when data is not needed. 
    4950 */ 
    5051 
  • include/libroar/libroar.h

    r5367 r5373  
    202202int roar_usleep(uint_least32_t t); 
    203203int roar_sleep(int t); 
     204pid_t roar_fork(const struct roar_libroar_forkapi * api); 
    204205 
    205206// call this function after we fork/exec()ed or similar. 
  • libroar/basic.c

    r5369 r5373  
    6464 } 
    6565 
    66  // TODO: FIXME: we should move this into the config structure. 
    6766 if ( daemonimage == NULL ) 
     67  daemonimage = roar_libroar_get_config()->daemonimage; 
     68 
     69 // we keep this step to be compatible with older versions. 
     70 if ( daemonimage == NULL || *daemonimage == 0 ) { 
    6871  daemonimage = getenv("ROAR_DAEMONIMAGE"); 
     72  if ( daemonimage != NULL ) { 
     73   ROAR_WARN("_start_server(*): Usage of $ROAR_DAEMONIMAGE is obsolete. Use ROAR_OPTIONS=daemonimage:..."); 
     74  } 
     75 } 
    6976 
    7077 if ( daemonimage == NULL || *daemonimage == 0 ) 
     
    7683 } 
    7784 
    78  r = fork(); 
     85 r = roar_fork(NULL); 
    7986 
    8087 if ( r == -1 ) { // error! 
    81   roar_err_from_errno(); 
    82   ROAR_ERR("roar_socket_open_fork(*): Can not fork: %s", strerror(errno)); 
     88  ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error)); 
    8389  close(socks[0]); 
    8490  close(socks[1]); 
     
    106112 
    107113  // we are still alive? 
    108   ROAR_ERR("roar_socket_open_fork(*): alive after exec(), that's bad!"); 
     114  ROAR_ERR("_start_server(*): alive after exec(), that's bad!"); 
    109115  _exit(1); 
    110116 } else { // we are the parent 
     
    121127 return -1; 
    122128#else 
    123  ROAR_ERR("roar_socket_open_fork(*): There is no UNIX Domain Socket support in win32, download a real OS."); 
     129 ROAR_ERR("_start_server(*): There is no UNIX Domain Socket support in win32, download a real OS."); 
    124130 return -1; 
    125131#endif 
     132} 
     133 
     134static int _connect_internal(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { 
     135 struct roar_libroar_config * config = roar_libroar_get_config(); 
     136 struct roar_vio_calls * vio; 
     137 
     138 if ( config->connect_internal != NULL ) { 
     139  vio = config->connect_internal(con, server, type, flags, timeout); 
     140  if ( vio == NULL ) 
     141   return -1; 
     142 
     143  con->viocon = vio; 
     144  con->flags |= ROAR_CON_FLAGS_VIO; 
     145 
     146  return 0; 
     147 } 
     148 
     149 roar_err_set(ROAR_ERROR_BADHOST); 
     150 return -1; 
    126151} 
    127152 
     
    153178 } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) { 
    154179  return _start_server(con, server, type, flags, timeout); 
     180 } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) { 
     181  return _connect_internal(con, server, type, flags, timeout); 
    155182 } 
    156183 
     
    346373#ifdef ROAR_HAVE_LIBSLP 
    347374 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) && 
    348       !(flags & ROAR_ENUM_FLAG_NONBLOCK) 
     375      !(flags & ROAR_ENUM_FLAG_NONBLOCK) && 
     376      !(flags & ROAR_ENUM_FLAG_LOCALONLY) 
    349377    ) { 
    350378  if ( (server = roar_slp_find_roard(0)) != NULL ) { 
     
    391419 
    392420int roar_connect     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { 
     421 struct roar_libroar_config * config = roar_libroar_get_config(); 
     422 
    393423 if ( con == NULL ) { 
    394424  roar_err_set(ROAR_ERROR_FAULT); 
     
    400430 
    401431 roar_err_set(ROAR_ERROR_UNKNOWN); 
    402  if ( roar_connect_raw(con, server, flags, timeout) == -1 ) 
    403   return -1; 
    404  
     432 if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 ) 
     433  return -1; 
     434 
     435 // TODO: this should be moved to be within _connect_server() so it also stores server names of auto located servers. 
    405436 if ( server != NULL ) { 
    406437  con->server_name = roar_mm_strdup(server); 
  • libroar/cdrom.c

    r5289 r5373  
    6464 } 
    6565 
    66  if ( (pid = fork()) == -1 ) { 
     66 if ( (pid = roar_fork(NULL)) == (pid_t)-1 ) { 
    6767  return -1; 
    6868 } 
  • libroar/config.c

    r5357 r5373  
    5050 
    5151#ifdef ROAR_SUPPORT_TRAP 
    52   config.trap_policy = ROAR_TRAP_IGNORE; 
     52  config.trap_policy      = ROAR_TRAP_IGNORE; 
    5353#endif 
    54   config.opmode      = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL; 
    55   config.server   = NULL; 
    56   config.authfile = NULL; 
     54  config.opmode           = ROAR_LIBROAR_CONFIG_OPMODE_NORMAL; 
     55  config.server           = NULL; 
     56  config.authfile         = NULL; 
     57  config.forkapi          = NULL; 
     58  config.connect_internal = NULL; 
     59  config.daemonimage      = NULL; 
    5760 
    5861  if ( home != NULL ) { 
    59    snprintf(authfile, 1023, "%s/.roarauth", home); 
    60    authfile[1023]    = 0; 
     62   snprintf(authfile, sizeof(authfile)-1, "%s/.roarauth", home); 
     63   authfile[sizeof(authfile)-1] = 0; 
    6164   config.authfile    = authfile; 
    6265   config.serversfile = NULL; 
     
    304307  } else if ( !strcmp(k, "x11-display") ) { 
    305308   config->x11.display = v; 
     309  } else if ( !strcmp(k, "daemonimage") ) { 
     310   config->daemonimage = v; 
     311  } else if ( !strcmp(k, "serverflags") ) { 
     312   if ( !strcmp(v, "nonblock") ) { 
     313    config->serverflags |= ROAR_ENUM_FLAG_NONBLOCK; 
     314   } else if ( !strcmp(v, "hardnonblock") ) { 
     315    config->serverflags |= ROAR_ENUM_FLAG_HARDNONBLOCK; 
     316   } else if ( !strcmp(v, "localonly") ) { 
     317    config->serverflags |= ROAR_ENUM_FLAG_LOCALONLY; 
     318   } else { 
     319    ROAR_WARN("roar_libroar_config_parse(*): Unknown serverflag: %s", v); 
     320   } 
    306321  } else { 
    307322   ROAR_WARN("roar_libroar_config_parse(*): Unknown option: %s", k); 
     
    389404} 
    390405 
     406int    roar_libroar_set_forkapi(struct roar_libroar_forkapi * api) { 
     407 roar_libroar_get_config_ptr()->forkapi = api; 
     408 return 0; 
     409} 
     410 
     411int    roar_libroar_set_connect_internal(struct roar_vio_calls * (*func)(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout)) { 
     412 roar_libroar_get_config_ptr()->connect_internal = func; 
     413 return 0; 
     414} 
     415 
    391416void   roar_libroar_nowarn(void) { 
    392417 roar_libroar_get_config_ptr()->nowarncounter++; 
  • libroar/enumdev.c

    r5324 r5373  
    105105 (void)flags, (void)dir, (void)socktype; 
    106106 
    107  if ( maxlen < 11 ) 
    108   return -1; 
    109  
    110  new = roar_libroar_get_server(); 
    111  _add(new); 
    112  
    113  new = getenv("ROAR_SERVER"); 
    114  _add(new); 
     107 if ( maxlen < 12 ) 
     108  return -1; 
     109 
     110 // We can not yet test if those servers are local or reachable via network so 
     111 // we skip them for now. 
     112 // TODO: test those servers. 
     113 if ( !(flags & ROAR_ENUM_FLAG_LOCALONLY) ) { 
     114  new = roar_libroar_get_server(); 
     115  _add(new); 
     116 
     117  new = getenv("ROAR_SERVER"); 
     118  _add(new); 
    115119 
    116120#ifdef ROAR_HAVE_LIBX11 
    117  if ( (x11con = roar_x11_connect(NULL)) != NULL ) { 
    118   new = roar_x11_get_prop(x11con, "ROAR_SERVER"); 
    119   _add(new); 
    120   roar_x11_disconnect(x11con); 
    121  } 
    122 #endif 
    123  
    124  new = getenv("SSH_CLIENT"); 
    125  if ( new == NULL ) 
    126   new = getenv("SSH_CONNECTION"); 
    127  
    128  if ( new != NULL ) { 
    129   ssh_buf = roar_mm_strdup(new); 
    130   if ( ssh_buf != NULL ) { 
    131    tmp = strstr(ssh_buf, " "); 
    132    if ( tmp == NULL ) { 
    133     // invalid format. 
    134     roar_mm_free(ssh_buf); 
    135    } else { 
    136     *tmp = 0; 
    137     servers[ret++] = ssh_buf; 
     121  if ( (x11con = roar_x11_connect(NULL)) != NULL ) { 
     122   new = roar_x11_get_prop(x11con, "ROAR_SERVER"); 
     123   _add(new); 
     124   roar_x11_disconnect(x11con); 
     125  } 
     126#endif 
     127 
     128  new = getenv("SSH_CLIENT"); 
     129  if ( new == NULL ) 
     130   new = getenv("SSH_CONNECTION"); 
     131 
     132  if ( new != NULL ) { 
     133   ssh_buf = roar_mm_strdup(new); 
     134   if ( ssh_buf != NULL ) { 
     135    tmp = strstr(ssh_buf, " "); 
     136    if ( tmp == NULL ) { 
     137     // invalid format. 
     138     roar_mm_free(ssh_buf); 
     139    } else { 
     140     *tmp = 0; 
     141     servers[ret++] = ssh_buf; 
     142    } 
    138143   } 
    139144  } 
    140  } 
    141145 
    142146#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) 
    143  if ( (i = readlink("/etc/roarserver", buf, sizeof(buf)-1)) != -1 ) { 
    144    buf[i] = 0; 
    145    _add(buf); 
    146  } 
    147 #endif 
     147  if ( (i = readlink("/etc/roarserver", buf, sizeof(buf)-1)) != -1 ) { 
     148    buf[i] = 0; 
     149    _add(buf); 
     150  } 
     151#endif 
     152 } 
    148153 
    149154 if ( (new = roar_env_get_home(0)) != NULL ) { 
     
    158163 servers[ret++] = roar_mm_strdup("+abstract"); 
    159164 servers[ret++] = roar_mm_strdup("/tmp/muroard"); 
     165 servers[ret++] = roar_mm_strdup("/dev/roar"); 
    160166 
    161167 return ret; 
     
    290296 
    291297static struct locmed _libroar_locmod[] = { 
    292  {ROAR_ENUM_FLAG_NONBLOCK|ROAR_ENUM_FLAG_HARDNONBLOCK, _esl_defaults}, 
    293  {ROAR_ENUM_FLAG_NONE,                                 _esl_slp}, 
     298 {ROAR_ENUM_FLAG_NONBLOCK|ROAR_ENUM_FLAG_HARDNONBLOCK|ROAR_ENUM_FLAG_LOCALONLY, _esl_defaults}, 
     299 {ROAR_ENUM_FLAG_NONE,                                                          _esl_slp}, 
    294300#ifdef ROAR_HAVE_FOPEN 
    295  {ROAR_ENUM_FLAG_NONBLOCK,                             _esl_neighbours} 
     301 {ROAR_ENUM_FLAG_NONBLOCK,                                                      _esl_neighbours} 
    296302#endif 
    297303}; 
    298304 
    299305struct roar_server * roar_enum_servers(int flags, int dir, int socktype) { 
     306 struct roar_libroar_config * config = roar_libroar_get_config(); 
    300307 struct roar_server * ret = NULL; 
    301308 struct roar_server * c; 
     
    304311 size_t i, cp, unic; 
    305312 ssize_t r; 
    306  int testflags = flags; 
     313 int testflags; 
    307314 int is_uniq; 
     315 
     316 flags     |= config->serverflags; 
     317 testflags  = flags; 
    308318 
    309319 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) 
  • libroar/libroar.c

    r5270 r5373  
    154154} 
    155155 
     156static pid_t _libroar_fork(void ** context, void * userdata) { 
     157 pid_t ret; 
     158 (void)context, (void)userdata; 
     159 ret = fork(); 
     160 if ( ret == -1 ) 
     161  roar_err_from_errno(); 
     162 return ret; 
     163} 
     164 
     165static const struct roar_libroar_forkapi _libroar_forkapi = { 
     166 .prefork   = NULL, 
     167 .fork      = _libroar_fork, 
     168 .failed    = NULL, 
     169 .parent    = NULL, 
     170 .child     = NULL, 
     171 .userdata  = NULL 
     172}; 
     173 
     174pid_t roar_fork(const struct roar_libroar_forkapi * api) { 
     175 void * context = NULL; 
     176 int err; 
     177 pid_t ret; 
     178 
     179 if ( api == NULL ) 
     180  api = &_libroar_forkapi; 
     181 
     182 if ( api->fork == NULL ) { 
     183  roar_err_set(ROAR_ERROR_INVAL); 
     184  return -1; 
     185 } 
     186 
     187 if ( api->prefork != NULL ) { 
     188  if ( api->prefork(&context, api->userdata) != 0 ) { 
     189   if ( api->failed != NULL ) 
     190    api->failed(&context, api->userdata); 
     191 
     192   if ( context != NULL ) { 
     193    err = roar_error; 
     194    roar_mm_free(context); 
     195    roar_error = err; 
     196   } 
     197   return (pid_t)-1; 
     198  } 
     199 } 
     200 
     201 if ( (ret = api->fork(&context, api->userdata)) == (pid_t)-1 ) { 
     202  if ( api->failed != NULL ) 
     203   api->failed(&context, api->userdata); 
     204 
     205  if ( context != NULL ) { 
     206   err = roar_error; 
     207   roar_mm_free(context); 
     208   roar_error = err; 
     209  } 
     210  return (pid_t)-1; 
     211 } 
     212 
     213 if ( ret == (pid_t)0 ) { 
     214  if ( api->child != NULL ) 
     215   api->child(&context, api->userdata); 
     216 } else { 
     217  if ( api->parent != NULL ) 
     218   api->parent(&context, api->userdata, ret); 
     219 } 
     220 
     221 if ( context != NULL ) { 
     222  err = roar_error; 
     223  roar_mm_free(context); 
     224  roar_error = err; 
     225 } 
     226 
     227 return ret; 
     228} 
     229 
    156230int roar_reset(enum roar_reset what) { 
    157231 char c; 
  • libroar/pinentry.c

    r5270 r5373  
    8888 } 
    8989 
    90  pe->pid = fork(); 
     90 pe->pid = roar_fork(NULL); 
    9191 
    9292 switch (pe->pid) { 
  • libroar/socket.c

    r5369 r5373  
    11541154#endif 
    11551155 
    1156  r = fork(); 
     1156 r = roar_fork(NULL); 
    11571157 
    11581158 if ( r == -1 ) { // error! 
  • libroar/vio_cmd.c

    r5364 r5373  
    159159 } 
    160160 
    161  child->pid = fork(); 
     161 child->pid = roar_fork(NULL); 
    162162 
    163163 switch (child->pid) { 
Note: See TracChangeset for help on using the changeset viewer.