2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
14 #include "native_client/src/public/imc_syscalls.h"
15 #include "native_client/src/public/name_service.h"
16 #include "native_client/src/shared/srpc/nacl_srpc.h"
20 * The newlib toolchain does not define dprintf.
21 * https://code.google.com/p/chromium/issues/detail?id=403825
23 #if !defined(__GLIBC__)
24 int dprintf(int, const char*, ...);
28 * Lock to guard name service channel. Both the local data structure and
29 * the channel itself can only be used for one request at a time. So we
30 * serialize requests. We could revisit this and do something that scales
31 * to multithreaded use better if there's demand for that later.
33 static pthread_mutex_t name_service_mutex = PTHREAD_MUTEX_INITIALIZER;
34 static int ns_channel_initialized = 0;
35 static struct NaClSrpcChannel ns_channel;
37 static int prepare_nameservice(void) {
42 if (ns_channel_initialized)
44 nacl_nameservice(&ns);
47 dprintf(2, "IRT: nacl_nameservice failed: %s\n", strerror(error));
51 connected_socket = imc_connect(ns);
54 if (-1 == connected_socket) {
55 dprintf(2, "IRT: imc_connect to nameservice failed: %s\n", strerror(error));
59 if (!NaClSrpcClientCtor(&ns_channel, connected_socket)) {
60 dprintf(2, "IRT: NaClSrpcClientCtor failed for nameservice\n");
64 ns_channel_initialized = 1;
69 * Look up a name in the nameservice. Returns a NACL_NAME_SERVICE_* code
70 * or -1 for an SRPC error. On returning NACL_NAME_SERVICE_SUCCESS,
71 * *out_fd has the descriptor for the service found.
73 int irt_nameservice_lookup(const char *name, int oflag, int *out_fd) {
77 pthread_mutex_lock(&name_service_mutex);
79 error = prepare_nameservice();
81 if (NACL_SRPC_RESULT_OK != NaClSrpcInvokeBySignature(
82 &ns_channel, NACL_NAME_SERVICE_LOOKUP, name, oflag,
84 dprintf(2, "IRT: SRPC failure for NACL_NAME_SERVICE_LOOKUP: %s\n",
89 pthread_mutex_unlock(&name_service_mutex);