source: roaraudio/libroar/basic.c @ 5844:6111f08378d3

Last change on this file since 5844:6111f08378d3 was 5844:6111f08378d3, checked in by phi, 11 years ago

test return value of _spawnlp()

File size: 21.9 KB
Line 
1//basic.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38enum mode {
39 NORMAL = 0,
40 SYSTEM = 1
41};
42
43static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout);
44
45int __get_daemonimage(const char ** daemonimage, enum mode * mode, const char * server) {
46 *daemonimage = NULL;
47 *mode = NORMAL;
48
49 if ( !!strncmp(server, "+fork", 5) ) {
50  roar_err_set(ROAR_ERROR_INVAL);
51  return -1;
52 }
53
54 server += 5;
55
56 if ( server[0] == '=' ) {
57  server++;
58
59  if ( server[0] == 0 ) {
60   // no special case, we just ignore it.
61  } else if ( server[0] == 'd' && server[1] == ':' ) {
62   server += 2;
63   *daemonimage = server;
64   *mode = NORMAL;
65  } else if ( server[0] == '!' ) {
66   server += 1;
67   *daemonimage = server;
68   *mode = SYSTEM;
69  } else {
70   roar_err_set(ROAR_ERROR_ILLSEQ);
71   return -1;
72  }
73 }
74
75 if ( *daemonimage == NULL )
76  *daemonimage = roar_libroar_get_config()->daemonimage;
77
78 // we keep this step to be compatible with older versions.
79 if ( *daemonimage == NULL || **daemonimage == 0 ) {
80  *daemonimage = roar_env_get("ROAR_DAEMONIMAGE");
81  if ( *daemonimage != NULL ) {
82   ROAR_WARN("__get_daemonimage(*): Usage of $ROAR_DAEMONIMAGE is obsolete. Use ROAR_OPTIONS=daemonimage:...");
83  }
84 }
85
86 if ( *daemonimage == NULL || **daemonimage == 0 )
87  *daemonimage = roar_libroar_get_path_static("bin-default-daemonimage");
88
89 return 0;
90}
91
92#if defined(ROAR_TARGET_WIN32)
93#define NUM_TRIES 16
94static int _start_server_win32(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) {
95 enum mode mode = NORMAL;
96 const char * daemonimage = NULL;
97 char buf[64];
98 int port;
99 size_t i;
100 int fh;
101 int ret;
102
103 if ( __get_daemonimage(&daemonimage, &mode, server) == -1 )
104  return -1;
105
106 if ( mode != NORMAL ) {
107  roar_err_set(ROAR_ERROR_NOSYS);
108  return -1;
109 }
110
111 for (i = 0; i < NUM_TRIES; i++) {
112  port = roar_random_uint16();
113  if ( port < 1 )
114   continue;
115
116  fh = roar_socket_connect(ROAR_SOCKET_TYPE_TCP, "localhost", port);
117  if ( fh == -1 )
118   break;
119
120  closesocket(fh);
121 }
122
123 if ( i == NUM_TRIES ) {
124  roar_err_set(ROAR_ERROR_LOOP);
125  return -1;
126 }
127
128 // port is the port to use. It seems to be 'free'.
129
130#if defined(_P_DETACH)
131#define MODE _P_DETACH
132#elif defined(_P_NOWAIT)
133#define MODE _P_NOWAIT
134#else
135#define MODE _P_NOWAITO
136#endif
137
138 snprintf(buf, sizeof(buf), "%i", port);
139 roar_err_clear_all();
140 if ( _spawnlp(MODE, daemonimage, daemonimage, "--tcp", "--bind", "localhost", "--port", buf, (const char*)NULL) < 0 ) {
141  roar_err_update();
142  ROAR_ERR("_start_server_win32(server='%s', ...): Can not start server: %s: %s", server, daemonimage, roar_errorstring);
143  return -1;
144 }
145
146 snprintf(buf, sizeof(buf), "localhost:%i", port);
147
148 // give the daemon some time to come up:
149 roar_usleep(25000);
150
151 ret = -1;
152 for (i = 0; ret == -1 && i < NUM_TRIES; i++) {
153  ret = _connect_server(con, buf, type, flags, timeout);
154  if ( ret == -1 && i < NUM_TRIES )
155   roar_sleep(1);
156 }
157
158 if ( ret == -1 )
159  return -1;
160
161 con->flags |= ROAR_CON_FLAGS_TERMINATE;
162
163 return 0;
164}
165#elif !defined(ROAR_TARGET_MICROCONTROLLER)
166static int _start_server_posix(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) {
167 enum mode mode = NORMAL;
168 const char * daemonimage = NULL;
169 int socks[2];
170 int r;
171 char fhstr[12];
172 size_t len;
173
174 if ( __get_daemonimage(&daemonimage, &mode, server) == -1 )
175  return -1;
176
177 len = roar_mm_strlen(daemonimage) + 9; // 9 = '+fork=' + 'X:' + '\0'
178 con->server_name = roar_mm_malloc(len);
179 if ( con->server_name != NULL ) {
180  snprintf(con->server_name, len, "+fork=%s%s", mode == NORMAL ? "d:" : "!", daemonimage);
181 }
182
183 if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) {
184  roar_err_from_errno();
185  return -1;
186 }
187
188 r = roar_fork(NULL);
189
190 if ( r == -1 ) { // error!
191  ROAR_ERR("_start_server(*): Can not fork: %s", roar_error2str(roar_error));
192  close(socks[0]);
193  close(socks[1]);
194  return -1;
195 } else if ( r == 0 ) { // we are the child
196  roar_watchdog_stop();
197
198  close(socks[0]);
199
200  close(ROAR_STDIN ); // we do not want roard to have any standard input
201  close(ROAR_STDOUT); // STDOUT is also not needed, so we close it,
202                      // but STDERR we keep open for error messages.
203
204  snprintf(fhstr, sizeof(fhstr), "%i", socks[1]);
205
206  switch (mode) {
207   case NORMAL:
208     execlp(daemonimage, daemonimage, "--no-listen", "--client-fh", fhstr, (_LIBROAR_GOOD_CAST char*)NULL);
209    break;
210   case SYSTEM:
211     dup2(socks[1], ROAR_STDIN );
212     dup2(socks[1], ROAR_STDOUT);
213     execl(roar_libroar_get_path_static("bin-sh"), roar_libroar_get_path_static("bin-sh"), "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL);
214     execlp("sh", "sh", "-c", daemonimage, (_LIBROAR_GOOD_CAST char*)NULL);
215    break;
216  }
217
218  // we are still alive?
219  ROAR_ERR("_start_server(*): alive after exec(), that's bad!");
220  ROAR_U_EXIT(1);
221 } else { // we are the parent
222  close(socks[1]);
223  if ( roar_vio_open_fh_socket(con->viocon, socks[0]) == -1 ) {
224   close(socks[0]);
225   return -1;
226  } else {
227   con->flags |= ROAR_CON_FLAGS_VIO;
228  }
229  return 0;
230 }
231
232 return -1;
233}
234#endif
235
236static int _start_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) {
237#if defined(ROAR_TARGET_WIN32)
238 return _start_server_win32(con, server, type, flags, timeout);
239#elif !defined(ROAR_TARGET_MICROCONTROLLER)
240 return _start_server_posix(con, server, type, flags, timeout);
241#else
242 ROAR_ERR("_start_server(*): There is no UNIX Domain Socket, download a real OS.");
243 roar_err_set(ROAR_ERROR_NOSYS);
244 return -1;
245#endif
246}
247
248static int _connect_internal(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) {
249 struct roar_libroar_config * config = roar_libroar_get_config();
250 struct roar_vio_calls * vio;
251
252 if ( config->connect_internal != NULL ) {
253  vio = config->connect_internal(con, server, type, flags, timeout);
254  if ( vio == NULL )
255   return -1;
256
257  con->viocon = vio;
258  con->flags |= ROAR_CON_FLAGS_VIO;
259
260  return 0;
261 }
262
263 roar_err_set(ROAR_ERROR_BADHOST);
264 return -1;
265}
266
267static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) {
268#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT)
269 struct stat sockstat;
270#endif
271 const char * obj = NULL;
272 char user_sock[128];
273 int is_decnet = 0;
274 int port = 0;
275 int i = 0;
276 int fh = -1;
277 int err;
278 int ret;
279
280 if ( con == NULL || server == NULL ) {
281  roar_err_set(ROAR_ERROR_FAULT);
282  return -1;
283 }
284
285 if ( !strcmp(server, "+invalid") ) {
286  roar_err_set(ROAR_ERROR_CANCELED);
287  return -1;
288 } else if ( !strncmp(server, "+dstr=", 6) ) {
289  if ( roar_vio_open_dstr_simple(con->viocon, server+6, ROAR_VIOF_READWRITE) == -1 )
290   return -1;
291  con->flags |= ROAR_CON_FLAGS_VIO;
292  con->server_name = roar_mm_strdup(server);
293  return 0;
294 } else if ( !strcmp(server, "+fork") || !strncmp(server, "+fork=", 6) ) {
295  return _start_server(con, server, type, flags, timeout);
296 } else if ( !strcmp(server, "+internal") || !strncmp(server, "+internal=", 10) ) {
297  ret = _connect_internal(con, server, type, flags, timeout);
298  if ( ret == 0 )
299   con->server_name = roar_mm_strdup(server);
300  return ret;
301 }
302
303 strncpy(user_sock, server, sizeof(user_sock)-1);
304 user_sock[sizeof(user_sock)-1] = 0;
305
306
307 if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports
308  for (i = 0; user_sock[i] != 0; i++) {
309   if ( user_sock[i] == ':' ) {
310    if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged
311     is_decnet = 1;
312     obj = &user_sock[i+2];
313     break;
314    }
315
316    port = atoi(&(user_sock[i+1]));
317    user_sock[i] = 0;
318    break;
319   }
320  }
321 }
322
323 if ( is_decnet ) {
324  if ( *user_sock == ':' ) {
325   if ( roar_socket_get_local_nodename() != NULL ) {
326    strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1);
327    user_sock[sizeof(user_sock)-1] = 0;
328    roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1);
329    user_sock[sizeof(user_sock)-1] = 0;
330    obj  = strstr(user_sock, "::");
331    obj += 2;
332   }
333  }
334
335  if ( *obj == 0 ) {
336   roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1);
337   user_sock[sizeof(user_sock)-1] = 0;
338  }
339
340  ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock);
341 }
342
343 if ( port || is_decnet ) {
344  fh = roar_socket_connect(type, user_sock, port);
345  // restore the original string
346  user_sock[i] = ':';
347 } else {
348#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT)
349  if ( user_sock[0] == '/' ) {
350   if ( stat(user_sock, &sockstat) == 0 ) {
351    if ( S_ISCHR(sockstat.st_mode) ) {
352#ifdef O_NOCTTY
353     fh = open(user_sock, O_RDWR|O_NOCTTY, 0666);
354#else
355     fh = open(user_sock, O_RDWR, 0666);
356#endif
357    }
358   }
359  }
360#endif
361  if ( fh == -1 )
362   fh = roar_socket_connect(type, user_sock, ROAR_DEFAULT_PORT);
363 }
364
365 if ( fh == -1 )
366  return -1;
367
368 if ( roar_vio_open_fh_socket(con->viocon, fh) == -1 ) {
369  err = roar_error;
370#ifdef ROAR_TARGET_WIN32
371  closesocket(fh);
372#else
373  close(fh);
374#endif
375  roar_error = err;
376  return -1;
377 } else {
378  con->flags |= ROAR_CON_FLAGS_VIO;
379 }
380
381 con->server_name = roar_mm_strdup(server);
382
383 roar_err_set(ROAR_ERROR_NONE);
384 return 0;
385}
386
387static int roar_connect_raw (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) {
388#ifdef ROAR_HAVE_LIBSLP
389 struct roar_libroar_config * config = roar_libroar_get_config();
390#endif
391#if defined(ROAR_HAVE_UNIX)
392 char user_sock[128];
393 const char * roar_server;
394#endif
395#if defined(ROAR_HAVE_LIBSLP) || !defined(ROAR_TARGET_MICROCONTROLLER)
396 int i = 0;
397#endif
398#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
399 struct passwd * pwd;
400#endif
401#ifdef ROAR_HAVE_LIBDNET
402 struct stat decnet_stat;
403#endif
404#ifdef ROAR_HAVE_LIBX11
405 struct roar_x11_connection * x11con;
406#endif
407#ifdef ROAR_HAVE_LIBSLP
408 struct roar_server * list;
409 int workarounds_store;
410#endif
411
412 roar_err_set(ROAR_ERROR_UNKNOWN);
413
414 if ( timeout != 0 ) {
415  roar_err_set(ROAR_ERROR_INVAL);
416  return -1;
417 }
418
419 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
420  flags |= ROAR_ENUM_FLAG_NONBLOCK;
421
422
423 if ( server == NULL || *server == 0 )
424  server = roar_libroar_get_server();
425
426 if ( server == NULL || *server == 0 )
427  server = roar_env_get("ROAR_SERVER");
428
429#ifdef ROAR_HAVE_LIBX11
430 if ( server == NULL || *server == 0 ) {
431  if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
432   server = roar_x11_get_prop(x11con, "ROAR_SERVER");
433   roar_x11_disconnect(x11con);
434  }
435 }
436#endif
437
438#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
439 if ( (server == NULL || *server == 0) && (i = readlink(roar_libroar_get_path_static("sysconf-roarserver"), user_sock, sizeof(user_sock)-1)) != -1 ) {
440   user_sock[i] = 0;
441   server = user_sock;
442 }
443#endif
444
445 if ( server != NULL && !strcasecmp(server, "+slp") ) {
446  server = roar_slp_find_roard(0);
447  if ( server == NULL ) {
448   return -1;
449  }
450 }
451
452 // "+default" is an alias for NULL.
453 if ( server != NULL && !strcasecmp(server, "+default") ) {
454  server = NULL;
455 }
456
457 if ( server == NULL || *server == 0 ) {
458  /* connect via defaults */
459
460#ifdef ROAR_HAVE_UNIX
461#ifndef ROAR_TARGET_MICROCONTROLLER
462  roar_server = roar_env_get("HOME");
463#else
464  roar_server = NULL;
465#endif
466
467  if ( roar_server == NULL ) {
468#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
469   if ( (pwd = getpwuid(getuid())) == NULL ) {
470    roar_server = roar_libroar_get_path_static("dir-nx-home");
471   } else {
472    roar_server = pwd->pw_dir;
473   }
474#else
475   roar_server = "/WIN32-SUCKS";
476#endif
477  }
478
479  snprintf(user_sock, sizeof(user_sock)-1, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
480  user_sock[sizeof(user_sock)-1] = 0;
481
482  if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
483   return 0;
484
485  if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 )
486   return 0;
487#endif
488
489  if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 )
490   return 0;
491
492#ifdef ROAR_HAVE_LIBDNET
493  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
494   if ( roar_socket_get_local_nodename() ) {
495    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
496    if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 )
497     return 0;
498   }
499  }
500#endif
501
502  if ( _connect_server(con, "+abstract", ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) == 0 )
503   return 0;
504
505#ifdef ROAR_HAVE_LIBSLP
506 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) &&
507      !(flags & ROAR_ENUM_FLAG_NONBLOCK) &&
508      !(flags & ROAR_ENUM_FLAG_LOCALONLY)
509    ) {
510  if ( (server = roar_slp_find_roard(0)) != NULL ) {
511   if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
512    return 0;
513
514   /* in case we can not connect to the server given this may be a cache problem,
515      we do a new lookup with the cache disabled in this case                     */
516   ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
517   if ( (server = roar_slp_find_roard(1)) != NULL )
518    if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 )
519     return 0;
520  }
521 }
522
523 workarounds_store = config->workaround.workarounds;
524 config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
525 list = roar_enum_servers(flags, -1, -1);
526 config->workaround.workarounds = workarounds_store;
527 if ( list != NULL ) {
528  for (i = 0; list[i].server != NULL; i++) {
529   if ( _connect_server(con, list[i].server, ROAR_SOCKET_TYPE_UNKNOWN, 0, 0) == 0 ) {
530    roar_enum_servers_free(list);
531    return 0;
532   }
533  }
534  roar_enum_servers_free(list);
535 }
536#endif
537
538 return -1;
539
540 } else {
541  /* connect via (char*)server */
542  if ( _connect_server(con, server, ROAR_SOCKET_TYPE_UNKNOWN, flags, timeout) != 0 )
543   return -1;
544  return 0;
545 }
546
547 roar_err_set(ROAR_ERROR_NODEV);
548 ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV");
549 return -1;
550}
551
552int roar_connect     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) {
553 struct roar_libroar_config * config = roar_libroar_get_config();
554
555 if ( con == NULL ) {
556  roar_err_set(ROAR_ERROR_FAULT);
557  return -1;
558 }
559
560 if ( roar_connect_none(con) == -1 )
561  return -1;
562
563 roar_err_set(ROAR_ERROR_UNKNOWN);
564 if ( roar_connect_raw(con, server, flags | config->serverflags, timeout) == -1 )
565  return -1;
566
567 return 0;
568}
569
570int roar_connect_none (struct roar_connection * con) {
571 struct roar_libroar_config * config = roar_libroar_get_config();
572
573 if ( con == NULL ) {
574  roar_err_set(ROAR_ERROR_INVAL);
575  return -1;
576 }
577
578 memset(con, 0, sizeof(struct roar_connection));
579 con->refc        = 1;
580 con->flags       = ROAR_CON_FLAGS_ISCLIENT;
581 con->version     = _ROAR_MESSAGE_VERSION;
582 con->cb_userdata = NULL;
583 con->cb          = NULL;
584 con->server_stds = NULL;
585 con->server_name = NULL;
586
587 roar_err_init(&(con->errorframe));
588
589 con->viocon = &(con->viocon_store);
590
591// con->flags |= ROAR_CON_FLAGS_VIO;
592
593 if ( config->protocolversion != -1 )
594  roar_set_connection_version(con, config->protocolversion);
595
596 roar_err_set(ROAR_ERROR_NONE);
597 return 0;
598}
599
600int roar_connect_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
601 if ( con == NULL || vio == NULL ) {
602  roar_err_set(ROAR_ERROR_INVAL);
603  return -1;
604 }
605
606 if ( roar_connect_none(con) == -1 )
607  return -1;
608
609 con->viocon = vio;
610 con->flags |= ROAR_CON_FLAGS_VIO;
611
612 return -1;
613}
614
615int roar_connect_fh (struct roar_connection * con, int fh) {
616
617 if ( con == NULL || fh == -1 ) {
618  roar_err_set(ROAR_ERROR_INVAL);
619  return -1;
620 }
621
622 if ( roar_connect_none(con) == -1 )
623  return -1;
624
625 // special hack to set an illegal value used internally in libroar:
626 if ( fh == -2 )
627  fh = -1;
628
629 if ( roar_vio_open_fh_socket(con->viocon, fh) != -1 ) {
630  con->flags |= ROAR_CON_FLAGS_VIO;
631 } else {
632  return -1;
633 }
634
635 roar_err_set(ROAR_ERROR_NONE);
636 return 0;
637}
638
639int roar_get_connection_fh (struct roar_connection * con) {
640 int fh;
641
642 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
643
644 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
645
646 if ( con == NULL )
647  return -1;
648
649 ROAR_DBG("roar_get_connection_fh(con=%p) = ?", con);
650
651 if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_FH, &fh) == -1 ) {
652#ifdef ROAR_TARGET_WIN32
653  if ( roar_vio_ctl(con->viocon, ROAR_VIO_CTL_GET_SELECT_FH, &fh) == -1 )
654   return -1;
655#else
656  return -1;
657#endif
658 }
659
660 ROAR_DBG("roar_get_connection_fh(con=%p) = %i", con, fh);
661
662 return fh;
663}
664
665struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
666 if ( con == NULL ) {
667  roar_err_set(ROAR_ERROR_FAULT);
668  return NULL;
669 }
670
671 if ( con->flags & ROAR_CON_FLAGS_VIO )
672  return con->viocon;
673
674// TODO: try to open the VIO.
675
676 roar_err_set(ROAR_ERROR_BADFH);
677 return NULL;
678}
679
680const char * roar_get_connection_server(struct roar_connection * con) {
681 if ( con == NULL ) {
682  roar_err_set(ROAR_ERROR_FAULT);
683  return NULL;
684 }
685
686 return con->server_name;
687}
688
689int roar_connectionref(struct roar_connection * con) {
690 if ( con == NULL ) {
691  roar_err_set(ROAR_ERROR_FAULT);
692  return -1;
693 }
694
695 con->refc++;
696
697 return 0;
698}
699
700int roar_connectionunref(struct roar_connection * con) {
701 struct roar_vio_calls * vio;
702 struct roar_message m;
703
704 if ( con == NULL ) {
705  roar_err_set(ROAR_ERROR_FAULT);
706  return -1;
707 }
708
709 con->refc--;
710
711 if ( con->refc )
712  return 0;
713
714 if ( con->flags & ROAR_CON_FLAGS_TERMINATE ) {
715  if ( roar_terminate(con, 1) == -1 ) {
716   ROAR_WARN("roar_connectionunref(con=%p{.server_name='%s'}): Can not terminate server as requested.", con, con->server_name);
717   if ( roar_terminate(con, 0) == -1 ) {
718    ROAR_ERR("roar_connectionunref(con=%p{.server_name='%s'}): Can not exit server. BAD.", con, con->server_name);
719   }
720  }
721 }
722
723 memset(&m, 0, sizeof(m));
724
725 m.datalen =  0;
726 m.stream  = -1;
727 m.pos     =  0;
728 m.cmd     = ROAR_CMD_QUIT;
729
730 roar_req(con, &m, NULL);
731
732 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
733  roar_vio_close(vio);
734 }
735
736 if ( con->server_stds != NULL ) {
737  roar_stds_free(con->server_stds);
738  con->server_stds = NULL;
739 }
740
741 if ( con->server_name != NULL ) {
742  roar_mm_free(con->server_name);
743  con->server_name = NULL;
744 }
745
746 if ( con->flags & ROAR_CON_FLAGS_FREESELF ) {
747  roar_mm_free(con);
748 } else {
749  roar_connect_fh(con, -2);
750 }
751
752 return 0;
753}
754
755int roar_set_connection_callback(struct roar_connection * con,
756                                 void (*cb)(struct roar_connection * con,
757                                            struct roar_message    * mes,
758                                            void                   * data,
759                                            void                   * userdata),
760                                 void * userdata) {
761 if ( con == NULL ) {
762  roar_err_set(ROAR_ERROR_FAULT);
763  return -1;
764 }
765
766 con->cb       = cb;
767 con->cb_userdata = userdata;
768
769 return 0;
770}
771
772int roar_set_connection_version(struct roar_connection * con, int version) {
773 if ( con == NULL ) {
774  roar_err_set(ROAR_ERROR_FAULT);
775  return -1;
776 }
777
778 if ( version == -1 )
779  version = _ROAR_MESSAGE_VERSION;
780
781 if ( version < 0 ) {
782  roar_err_set(ROAR_ERROR_INVAL);
783  return -1;
784 }
785
786 con->version = version;
787
788 if ( version == 2 ) {
789  con->flags |= ROAR_CON_FLAGS_SUPPORT_V2;
790 }
791
792 return 0;
793}
794
795int16_t roar_message_genseq(struct roar_connection * con, int is_client) {
796 static int16_t store = 0;
797 int16_t ret = 0;
798
799 ret = store++;
800
801 if ( ret == 0 )
802  ret = store++;
803
804 if ( is_client == -1 ) {
805  if ( con != NULL ) {
806   is_client = con->flags & ROAR_CON_FLAGS_ISCLIENT ? 1 : 0;
807  }
808 }
809
810 if ( is_client == -1 )
811  is_client = 1;
812
813 ret |= 0x8000U;
814 if ( !is_client )
815  ret -= 0x8000U;
816
817 return ret;
818}
819
820int roar_sync         (struct roar_connection * con) {
821 // wait for any non-client reqs
822 return roar_wait_msg(con, 0x0000, 0x8000);
823}
824
825int roar_wait_msg     (struct roar_connection * con, int16_t seq, int16_t seqmask) {
826 roar_err_set(ROAR_ERROR_NOSYS);
827 return -1;
828}
829
830int roar_noop         (struct roar_connection * con) {
831 struct roar_message mes;
832
833 if ( con == NULL ) {
834  roar_err_set(ROAR_ERROR_FAULT);
835  return -1;
836 }
837
838 memset(&mes, 0, sizeof(mes));
839
840 mes.cmd = ROAR_CMD_NOOP;
841 mes.stream = -1;
842
843 return roar_req3(con, &mes, NULL);
844}
845
846int roar_identify   (struct roar_connection * con, const char * name) {
847 struct roar_message mes;
848 uint32_t pid;
849 int max_len;
850
851 roar_err_set(ROAR_ERROR_UNKNOWN);
852
853 ROAR_DBG("roar_identify(*): try to identify myself...");
854
855 memset(&mes, 0, sizeof(mes));
856
857 mes.cmd    = ROAR_CMD_IDENTIFY;
858 mes.stream = -1;
859 mes.pos    =  0;
860
861 ROAR_DBG("roar_identify(*): name=%p", name);
862
863 if ( name == NULL )
864  name = "libroar client";
865
866 ROAR_DBG("roar_identify(*): name=%p", name);
867
868 max_len = roar_mm_strlen(name);
869 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
870
871 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
872  max_len = LIBROAR_BUFFER_MSGDATA - 5;
873
874 mes.datalen = 5 + max_len;
875 mes.data[0] = 1;
876
877 pid = getpid();
878 mes.data[1] = (pid & 0xFF000000UL) >> 24;
879 mes.data[2] = (pid & 0x00FF0000UL) >> 16;
880 mes.data[3] = (pid & 0x0000FF00UL) >>  8;
881 mes.data[4] = (pid & 0x000000FFUL) >>  0;
882 ROAR_DBG("roar_identify(*): pid = %i", (int)pid);
883
884 strncpy(mes.data+5, name, max_len);
885
886 return roar_req3(con, &mes, NULL);
887}
888
889//ll
Note: See TracBrowser for help on using the repository browser.