Ejemplo n.º 1
0
int main(int argc, char **argv)
{
  int bufsize = 1024*1024;
  char buffer[bufsize], *bstate;
  int n;
  Net_timeout_t dt;
  NetStream_t *ns;
  char cmd[512];
  char *host;
  int port = 6714;
  int timeout = 15;


  if (argc < 2) {
     printf("get_version -a | host [port timeout]\n");
     printf("   -a   -Use the local host and default port\n");
     return(0);
  }

  if (strcmp(argv[1], "-a") == 0) {
    host = (char *)malloc(1024);
    gethostname(host, 1023);
  } else {
    host = argv[1];
  }

  if (argc > 2) port = atoi(argv[2]);
  if (argc == 4) timeout = atoi(argv[3]);
  set_net_timeout(&dt, timeout, 0);

  sprintf(cmd, "1 4 5 %d\n", timeout);  // IBP_ST_VERSION command

  assert(apr_initialize() == APR_SUCCESS);

  dns_cache_init(10);

  ns = cmd_send(host, port, cmd, &bstate, timeout);
  if (ns == NULL) return(-1);
  if (bstate != NULL) free(bstate);

  //** Read the result.  Termination occurs when the line "END" is read.
  //** Note that readline_netstream strips the "\n" from the end of the line
  n = NS_OK;
  while (n == NS_OK) {
     n = readline_netstream(ns, buffer, bufsize, dt);
     if (n == NS_OK) {
        if (strcmp(buffer, "END") == 0) {
           n = NS_OK+1;
        } else {
           printf("%s\n", buffer);
        }
     }
  }

  //** Close the connection
  close_netstream(ns);

  apr_terminate();
  return(0);
}
Ejemplo n.º 2
0
int _read_netstream_block(NetStream_t *ns, apr_time_t end_time, tbuffer_t *buffer, int pos, int size, int dolock)
{
    int nleft, nbytes, err;

    Net_timeout_t dt;

    set_net_timeout(&dt, 1, 0);
    nleft = size;
    nbytes = -100;
    err = NS_OK;
    while ((nleft > 0) && (err == NS_OK)) {
        nbytes = _read_netstream(ns, buffer, pos, nleft, dt, dolock);
        log_printf(15, "read_netstream_block: ns=%d size=%d nleft=%d nbytes=%d pos=%d time=" TT "\n",
                   ns_getid(ns), size, nleft, nbytes, pos, apr_time_now());

        if (apr_time_now() > end_time) {
            log_printf(15, "read_netstream_block: ns=%d Command timed out! to=" TT " ct=" TT " \n", ns_getid(ns), end_time, apr_time_now());
            err = NS_TIMEOUT;
        }

        if (nbytes < 0) {
            err = nbytes;   //** Error with write
        } else if (nbytes > 0) {   //** Normal write
            pos = pos + nbytes;
            nleft = nleft - nbytes;
            err = NS_OK;
        }
    }

    log_printf(15, "read_netstream_block: END ns=%d size=%d nleft=%d nbytes=%d pos=%d\n", ns_getid(ns), size, nleft, nbytes, pos);

    return(err);
}
Ejemplo n.º 3
0
net_sock_t *sock_accept(net_sock_t *nsock)
{
    int err;
    Net_timeout_t tm;
    network_sock_t *psock = (network_sock_t *)nsock;

    network_sock_t *sock = (network_sock_t *)malloc(sizeof(network_sock_t));
    assert(sock != NULL);
    memset(sock, 0, sizeof(network_sock_t));

    if (apr_pool_create(&(sock->mpool), NULL) != APR_SUCCESS) {
        free(sock);
        return(NULL);
    }


    sock->tcpsize = psock->tcpsize;

    err = apr_socket_accept(&(sock->fd), psock->fd, sock->mpool);
    if (err != APR_SUCCESS) {
        apr_pool_destroy(sock->mpool);
        free(sock);
        sock = NULL;
        log_printf(0, "ERROR with apr_socket_accept err=%d\n", err);
    } else {
        apr_thread_mutex_create(&(sock->lock), APR_THREAD_MUTEX_DEFAULT,sock->mpool);

        //** Set the with a minimal timeout of 10ms
        set_net_timeout(&tm, 0, SOCK_DEFAULT_TIMEOUT);
        apr_socket_timeout_set(sock->fd, tm);
    }

    return(sock);
}
Ejemplo n.º 4
0
int sock_connect(net_sock_t *nsock, const char *hostname, int port, Net_timeout_t timeout)
{
    int err;
    Net_timeout_t tm;

    network_sock_t *sock = (network_sock_t *)nsock;

    if (sock == NULL) return(-1);   //** If NULL exit

    if (sock->fd != NULL) apr_socket_close(sock->fd);

    sock->fd = NULL;
    sock->sa = NULL;

    log_printf(0, " sock_connect: hostname=%s:%d to=" TT "\n", hostname, port, timeout);
//   err = apr_sockaddr_info_get(&(sock->sa), hostname, APR_INET, port, APR_IPV4_ADDR_OK, sock->mpool);
    err = apr_sockaddr_info_get(&(sock->sa), hostname, APR_INET, port, 0, sock->mpool);
//log_printf(0, "sock_connect: apr_sockaddr_info_get: err=%d\n", err);
//if (sock->sa == NULL) log_printf(0, "sock_connect: apr_sockaddr_info_get: sock->sa == NULL\n");

    if (err != APR_SUCCESS) return(err);

    err = apr_socket_create(&(sock->fd), APR_INET, SOCK_STREAM, APR_PROTO_TCP, sock->mpool);
//log_printf(0, "sock_connect: apr_sockcreate: err=%d\n", err);
    if (err != APR_SUCCESS) return(err);


////   apr_socket_opt_set(sock->fd, APR_SO_NONBLCK, 1);
    apr_socket_timeout_set(sock->fd, timeout);
    if (sock->tcpsize > 0) {
        apr_socket_opt_set(sock->fd, APR_SO_SNDBUF, sock->tcpsize);
        apr_socket_opt_set(sock->fd, APR_SO_RCVBUF, sock->tcpsize);
    }

    err = apr_socket_connect(sock->fd, sock->sa);
//log_printf(0, "sock_connect: apr_socket_connect: err=%d\n", err); flush_log();

    //** Set a default timeout
    set_net_timeout(&tm, 0, SOCK_DEFAULT_TIMEOUT);
    apr_socket_timeout_set(sock->fd, tm);

    return(err);
}
Ejemplo n.º 5
0
int parse_config(GKeyFile *keyfile, Config_t *cfg) {
//  GKeyFileFlags flags;
//  GError *error = NULL;
  Server_t *server;
  char *str;
  int val, k;
  gsize n;
  pMount_t *pm, *pmarray;

  // *** Initialize the data structure to default values ***
  server = &(cfg->server);
  server->max_threads = 64;
  server->max_pending = 16;
  server->min_idle = 60;
  server->stats_size = 5000;
  set_net_timeout(&(server->timeout), 1, 0);  //**Wait 1sec
  server->timeout_secs = 1;
  server->logfile = "ibp.log";
  server->log_overwrite = 0;
  server->log_level = 0;
  server->log_maxsize = 100 * 1024 * 1024;
  server->debug_level = 0;
  server->enable_timestamps = 1;
  server->timestamp_interval = 60;
  server->password = DEFAULT_PASSWORD;
  server->lazy_allocate = 1;
  server->alog_name = "ibp_activity.log";
  server->alog_max_size = 100*1024*1024;
  server->alog_max_history = 1;
  server->alog_host = NULL;
  server->alog_port = 0;
  cfg->dbenv_loc = "/tmp/ibp_dbenv";
  cfg->db_mem = 256;
  cfg->tmpdir = "/etc/ibp";
  cfg->force_resource_rebuild = 0;
  cfg->truncate_expiration = 0;

  // *** Parse the Server settings ***
  char **list = g_key_file_get_string_list(keyfile, "server", "interfaces",  &n, NULL);
  interface_t *iface;
  if (list == NULL) {
     server->iface = (interface_t *)malloc(sizeof(interface_t));
     assert(server->iface != NULL);
     server->n_iface = 1;
     iface = server->iface;
     assert((iface->hostname = (char *)malloc(512)) != NULL);  iface->hostname[511] = '\0';
     gethostname(iface->hostname, 511);
     iface->port = IBP_PORT;
  } else {  //** Cycle through the hosts
     server->iface = (interface_t *)malloc(sizeof(interface_t)*n);
     assert(server->iface != NULL);
     server->n_iface = n;
     char *bstate;
     int fin;
     for (k=0; k<n; k++) {
        iface = &(server->iface[k]);
        iface->hostname = string_token(list[k], ":", &bstate, &fin);
        if (sscanf(string_token(NULL, " ", &bstate, &fin), "%d", &(iface->port)) != 1) {
           iface->port = IBP_PORT;
        }     
     }
  }

//  str = g_key_file_get_string(keyfile, "server", "address", NULL);
//  if (str != NULL) { free(server->hostname); server->hostname = str; }
//  val = g_key_file_get_integer(keyfile, "server", "port", NULL);
//  if (val > 0) server->port = val;
  val = g_key_file_get_integer(keyfile, "server", "threads", NULL);
  if (val > 0) server->max_threads = val;
  val = g_key_file_get_integer(keyfile, "server", "max_pending", NULL);
  if (val > 0) server->max_pending = val;
  val = g_key_file_get_integer(keyfile, "server", "min_idle", NULL);
  if (val > 0) server->min_idle = val;
  val = g_key_file_get_integer(keyfile, "server", "max_network_wait_ms", NULL);
  if (val > 0) {
     int sec = val/1000;
     int us = val - 1000*sec;
     us = us * 1000;  //** Convert from ms->us
     server->timeout_secs = sec;
     set_net_timeout(&(server->timeout), sec, us);  //**Convert it from ms->us
  }
  val = g_key_file_get_integer(keyfile, "server", "stats_size", NULL);
  if (val > 0) server->stats_size = val;
  val = g_key_file_get_integer(keyfile, "server", "timestamp_interval", NULL);
  if (val > 0) server->timestamp_interval = val;
  str = g_key_file_get_string(keyfile, "server", "password", NULL);
  if (str != NULL) server->password = str;
  str = g_key_file_get_string(keyfile, "server", "log_file", NULL);
  if (str != NULL) server->logfile = str;
  val = g_key_file_get_integer(keyfile, "server", "log_level", NULL);
  if (val > 0) server->log_level = val;
  val = g_key_file_get_integer(keyfile, "server", "enable_timestamps", NULL);
  if (val > 0) server->enable_timestamps = val;
  val = g_key_file_get_integer(keyfile, "server", "log_maxsize", NULL);
  if (val > 0) server->log_maxsize = val * 1024 * 1024;
  val = g_key_file_get_integer(keyfile, "server", "debug_level", NULL);
  if (val > 0) server->debug_level = val;
  cfg->tmpdir = g_key_file_get_string(keyfile, "server", "tmpdir", NULL);
  str = g_key_file_get_string(keyfile, "server", "db_env_loc", NULL);
  if (str != NULL) cfg->dbenv_loc = str;
  val = g_key_file_get_integer(keyfile, "server", "db_mem", NULL);
  if (val > 0) cfg->db_mem = val;
  val = g_key_file_get_integer(keyfile, "server", "lazy_allocate", NULL);
  if (val > 0) server->lazy_allocate = val;

  str = g_key_file_get_string(keyfile, "server", "activity_file", NULL);
  if (str != NULL) server->alog_name = str;
  val = g_key_file_get_integer(keyfile, "server", "activity_maxsize", NULL);
  if (val > 0) {
     server->alog_max_size = val * 1024 * 1024;
  } else if (val == -1) {
     server->alog_max_size = -1;
  }

  str = g_key_file_get_string(keyfile, "server", "activity_host", NULL);
  if (str != NULL) server->alog_host = str;
  val = g_key_file_get_integer(keyfile, "server", "activity_max_history", NULL);
  if (val > 0) server->alog_max_history = val;
  val = g_key_file_get_integer(keyfile, "server", "activity_port", NULL);
  if (val > 0) server->alog_port = val;

  val = g_key_file_get_integer(keyfile, "server", "force_resource_rebuild", NULL);
  if (val > 0) cfg->force_resource_rebuild = val;
  val = g_key_file_get_integer(keyfile, "server", "truncate_duration", NULL);
  if (val > 0) cfg->truncate_expiration = val;

  //*** Do some initial config of the log and debugging info ***
  open_log(cfg->server.logfile);            
  set_log_level(cfg->server.log_level);
  set_debug_level(cfg->server.debug_level);
  set_log_maxsize(cfg->server.log_maxsize);

  // *** Now iterate through each resource which is assumed to be all groups beginning with "resource" ***      
  cfg->dbenv = create_db_env(cfg->dbenv_loc, cfg->db_mem, cfg->force_resource_rebuild);
  gsize ngroups, i;
  char **group = g_key_file_get_groups(keyfile, &ngroups);
  cfg->n_resources = 0;
  assert((cfg->res = (Resource_t *)malloc(sizeof(Resource_t)*(ngroups-1))) != NULL);
  assert((pmarray = (pMount_t *)malloc(sizeof(pMount_t)*(ngroups-1))) != NULL);
  for (i=0; i<ngroups; i++) {
      if (strncmp("resource", group[i], 8) == 0) {
         pm = &(pmarray[cfg->n_resources]);
         pm->res = &(cfg->res[cfg->n_resources]);
         pm->res->rl_index = cfg->n_resources;
         pm->keyfile = keyfile;
         pm->group = group[i];
         pm->dbenv = cfg->dbenv;
         pm->force_resource_rebuild = cfg->force_resource_rebuild;
  
         pthread_create(&(pm->thread_id), NULL, parallel_mount_resource, (void *)pm);
//         mount_resource(&(cfg->res[cfg->n_resources]), keyfile, group[i], cfg->dbenv, cfg->force_resource_rebuild);

         cfg->n_resources++;
         
      }
  }  

  //** Wait for all the threads to join **
  void *dummy;
  for (i=0; i<cfg->n_resources; i++) {
     pthread_join(pmarray[i].thread_id, &dummy);
  }

  free(pmarray);

  if (cfg->n_resources < 0) {
     printf("parse_config:  No resources defined!!!!\n");
     abort();
  }

  return(0);
}
Ejemplo n.º 6
0
int phoebus_connect(net_sock_t *nsock, const char *hostname, int port, Net_timeout_t timeout)
{
   network_phoebus_t *sock = (network_phoebus_t *)nsock;

   char in_addr[16];
   int sfd, err;
   int i;
   fd_set wfd;
   time_t endtime;
   Net_timeout_t to;
   char dest[LSL_DEPOTID_LEN];

   log_printf(5, "phoebus_connect: Trying to make connection to Hostname: %s  Port: %d\n", hostname, port);
   snprintf(dest, LSL_DEPOTID_LEN, "%s/%d", hostname, port);

   if (sock == NULL) return(1);

   sock->fd = -1;

   // Get ip address
   if (lookup_host(hostname, in_addr) != 0) {
      log_printf(0, "phoebus_connect: lookup_host failed.  Hostname: %s  Port: %d\n", hostname, port);
      return(1);
   }

   // Store the host for phoebus_set_peer ---Needs work to be ipv6 compatible
   sock->family = AF_INET;
   i = (sock->family == AF_INET) ? 4 : 16;
   memcpy(&(sock->address[0]), in_addr, i);
   
   if (sock->p_path != NULL) { 
      sock->sess = lsl_session();

      if (sock->tcpsize > 0) {
         log_printf(10, "phoebus_connect: Setting tcpbufsize=%d\n", sock->tcpsize);
         if (lsl_setsockopt(sock->sess, SOL_SOCKET, SO_SNDBUF, (char*)&(sock->tcpsize), sizeof(sock->tcpsize)) != 0) {
            log_printf(0, "phoebus_connect: Can't configure SO_SNDBUF to %d!\n", sock->tcpsize);
         }
         if (lsl_setsockopt(sock->sess, SOL_SOCKET, SO_RCVBUF, (char*)&(sock->tcpsize), sizeof(sock->tcpsize)) != 0) {
            log_printf(0, "phoebus_connect: Can't configure SO_RCVBUF to %d!\n", sock->tcpsize);
         }
      }
      
      for (i=0; i<sock->p_path->p_count; i++) {
            log_printf(15, "phoebus_connect: hop %d : %s\n", i, sock->p_path->path[i]);
	    lsl_sess_appendchild(sock->sess, sock->p_path->path[i], LSL_DEPOT_NATIVE);
      }
      
      lsl_sess_appendchild(sock->sess, dest, 0);
      
      if (lsl_connect(sock->sess)) {
	 log_printf(0, "phoebus_connect: could not connect to %s\n", dest);
	 return (1);
      }
      
      sfd = lsl_get_session_socket(sock->sess);      
   }
   else {
      log_printf(0, "phoebus_connect: called without valid phoebus path!\n");
      return(1);
   }

   // Configure it correctly
   int flag=1;
   if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) != 0) {
      log_printf(0, "phoebus_connect: Can't configure SO_REUSEADDR!\n");
   }


   //Configure the socket for non-blocking I/O
   if ((err = fcntl(sfd, F_SETFL, O_NONBLOCK)) == -1) {  
      log_printf(0, "phoebus_connect: Can't configure connection for non-blocking I/O!");
   }

   log_printf(20, "phoebus_connect: Before select timeout=%lu time=" TT "\n", timeout.tv_sec, time(NULL));
   endtime = time(NULL) + 5;
   do {
      FD_ZERO(&wfd);
      FD_SET(sfd, &wfd);
      set_net_timeout(&to, 1, 0);
      err = select(sfd+1, NULL, &wfd, NULL, &to);
      log_printf(20, "phoebus_connect: After select\n");
      if (err != 1) {
         if (errno != EINPROGRESS) {
            log_printf(0, "phoeubs_connect: select failed.  Hostname: %s  Port: %d select=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno));
            return(1);
         } else {
            log_printf(10, "phoebus_connect: In progress.....  time=" TT " Hostname: %s  Port: %d select=%d errno: %d error: %s\n", time(NULL), hostname, port, err, errno, strerror(errno));
         }
      }
   } while ((err != 1) && (time(NULL) < endtime));

   sock->fd = sfd;

   return(0);
}
Ejemplo n.º 7
0
NetStream_t *cmd_send(char *host, int port, char *cmd, char **res_buffer, int timeout)
{
  int bufsize = 10*1024;
  char buffer[bufsize];
  char *bstate;
  int err, failed;
  int n, retry;
  double swait;
  Net_timeout_t dt;
  NetStream_t *ns;
  apr_time_t await, sec, usec;

  *res_buffer = NULL;

//set_log_level(20);
//int loop =0;
  do {
//loop++;
//printf("cmd_send: loop=%d\n", loop);

     failed = 0;
     //** Make the host connection
     retry = 3;
     do {
        ns = new_netstream();
        ns_config_sock(ns, -1);
        set_net_timeout(&dt, 60, 0);

        err = net_connect(ns, host, port, dt);
        if (err != 0) {
           destroy_netstream(ns);
           printf("get_version: Can't connect to host!  host=%s port=%d  err=%d\n", host, port, err);
           if (retry <= 0) {
              printf("cmd_send: Aborting!\n");
              return(NULL);
           } else {
              printf("cmd_send: Sleeping for 1 second and retrying(%d).\n", retry);
              sleep(1);
              retry--;
           }
        }
     } while ((err != 0)  && (retry > -1));

     //** Send the command
     dt = apr_time_now() + apr_time_make(timeout,0);
     n = write_netstream_block(ns, dt, cmd, strlen(cmd));

     //** Get the result line
     set_net_timeout(&dt, timeout, 0);
     n = readline_netstream(ns, buffer, bufsize, dt);
     if (n == NS_OK) {
        n = atoi(string_token(buffer, " ", &bstate, &err));
        if (n != IBP_OK) {
           if (n == IBP_E_OUT_OF_SOCKETS) {
              swait = atof(string_token(NULL, " ", &bstate, &err));
              printf("Depot is busy. Sleeping for %lf sec and retrying.\n", swait);
              destroy_netstream(ns);
              failed = 1;
              sec = swait; usec = (swait-sec) * 1000000;
              await = apr_time_make(sec, usec);
//              printf("sec=" TT " usec=" TT "  await=" TT "\n", sec, usec,await);
              apr_sleep(await);
           } else {
              printf("Error %d returned!\n", n);
              destroy_netstream(ns);
              return(NULL);
           }
        }
     } else {
//printf("cmd_send: readline_netstream=%d\n", n);
       failed = 1;
     }
  } while (failed == 1);

  *res_buffer = strdup(bstate);

  return(ns);
}
Ejemplo n.º 8
0
int fd_connect(int *fd, const char *hostname, int port, int tcpsize, Net_timeout_t timeout)
{
   struct sockaddr_in addr;
   char in_addr[6];
   int sfd, err;
   fd_set wfd;
   time_t endtime;
   Net_timeout_t to;

   log_printf(15, "fd_connect: Trying to make connection to Hostname: %s  Port: %d\n", hostname, port);

   *fd = -1;
   if (hostname == NULL) {
      log_printf(15, "fd_connect: lookup_host failed.  Missing hostname!  Port: %d\n",port);
      return(1);
   }

   // Get ip address
   if (lookup_host(hostname, in_addr) != 0) {
      log_printf(15, "fd_connect: lookup_host failed.  Hostname: %s  Port: %d\n", hostname, port);
      return(1);
   }
   
   // get the socket
   sfd = socket(PF_INET, SOCK_STREAM, 0);
   *fd = sfd;
   if (sfd == -1) {
      log_printf(10, "fd_connect: socket open failed.  Hostname: %s  Port: %d\n", hostname, port);
      return(1);
   }

   // Configure it correctly
   int flag=1;
   if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) != 0) {
      log_printf(0, "fd_connect: Can't configure SO_REUSEADDR!\n");
   }

   if (tcpsize > 0) {
      log_printf(10, "fd_connect: Setting tcpbufsize=%d\n", tcpsize);
      if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (char*)&tcpsize, sizeof(tcpsize)) != 0) {
         log_printf(0, "fd_connect: Can't configure SO_SNDBUF to %d!\n", tcpsize);
      }
      if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (char*)&tcpsize, sizeof(tcpsize)) != 0) {
         log_printf(0, "fd_connect: Can't configure SO_RCVBUF to %d!\n", tcpsize);
      }
   }

   memset(&addr, 0, sizeof(addr));
   addr.sin_family = AF_INET;
   addr.sin_port = htons(port);
   memcpy(&(addr.sin_addr), &in_addr, 4);
//   memset(&(addr.sin_zero), 0, 8);

   // Connect it
   err = connect(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
   if (err != 0) {
      if (errno != EINPROGRESS) {
         log_printf(0, "fd_connect: connect failed.  Hostname: %s  Port: %d err=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno));     
         return(1);
      }
//      err = 0;
   }

   //Configure the socket for non-blocking I/O
   if ((err = fcntl(sfd, F_SETFL, O_NONBLOCK)) == -1) {  
      log_printf(0, "fd_connect: Can't configure connection for non-blocking I/O!");
   }


   log_printf(20, "fd_connect: Before select timeout=%lu time=" TT "\n", timeout.tv_sec, time(NULL));
   endtime = time(NULL) + 5;
   do {
      FD_ZERO(&wfd);
      FD_SET(sfd, &wfd);
      set_net_timeout(&to, 1, 0);
      err = select(sfd+1, NULL, &wfd, NULL, &to);
      log_printf(20, "fd_connect: After select err=%d\n", err);
      if (err != 1) {
         if (errno != EINPROGRESS) {
            log_printf(0, "fd_connect: select failed.  Hostname: %s  Port: %d select=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno));
            return(1);
         } else {
            log_printf(10, "fd_connect: In progress.....  time=" TT " Hostname: %s  Port: %d select=%d errno: %d error: %s\n", time(NULL), hostname, port, err, errno, strerror(errno));
         }
      }
   } while ((err != 1) && (time(NULL) < endtime));

   if (err != 1) {   //** There were problems so return with an error
      log_printf(0, "fd_connect: Couldn\'t make connection.  Hostname: %s  Port: %d select=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno));
      close(sfd);
      return(1);
   }

   *fd = sfd;

   return(0);
}
Ejemplo n.º 9
0
Net_timeout_t *convert_epoch_time2net(Net_timeout_t *tm, int epoch_secs)
{
    int dt = epoch_secs - time(NULL);
    if (dt < 0) dt = 5;  //** Even if it's timed out give it a little time
    return(set_net_timeout(tm, dt, 0));
}
Ejemplo n.º 10
0
int read_command(ibp_task_t *task)
{
    NetStream_t *ns = task->ns;
    Cmd_state_t *cmd = &(task->cmd);

    int bufsize = 10*1024;
    char buffer[bufsize];
    char *bstate;
    int  nbytes, status, offset, count;
    int err, fin;
    time_t endtime;
    command_t *mycmd;

//   pthread_mutex_unlock(&(task->lock));

    log_printf(10, "read_command: ns=%d initial tid=" LU " START--------------------------\n", task->ns->id, task->tid);

    cmd->state = CMD_STATE_CMD;
    task->child = NULL;
    task->parent = NULL;

    clear_stat(&(task->stat));
    memset(task->stat.address, 0, sizeof(task->stat.address));
    if (task->stat.address != 0) {
        strncpy(task->stat.address, task->ns->peer_address, sizeof(task->stat.address));
    }

    Net_timeout_t dt;
//   int wt = global_config->server.min_idle/3;
    set_net_timeout(&dt, 1, 0);
    offset = 0;
    count = 0;
    endtime = time(NULL) + 10; //** Wait a max of 10 sec
    do {
        nbytes = readline_netstream_raw(ns, &(buffer[offset]), sizeof(buffer) - offset, dt, &status);
        count++;
        offset = offset + nbytes;
    } while ((offset > 0) && (status == 0) && (time(NULL) <= endtime));
    nbytes = offset;

    log_printf(10, "read_command: ns=%d tid=" LU " Command: %s\n", task->ns->id, task->tid, buffer);
    log_printf(10, "read_command: ns=%d nbytes:=  %d\n", task->ns->id, nbytes);
    flush_log();

    if (nbytes == -2) {  //** Not enough buffer space to flag an internal error
        send_cmd_result(task, IBP_E_INTERNAL);
        cmd->state = CMD_STATE_FINISHED;
        cmd->command = IBP_NOP;
        return(-1);
    } else if (nbytes == -1) {
        return(-1);
    } else if (nbytes == 0) {
        if (status == -1) {
            return(-1);
        } else {
            return(1);
        }
    }

    //** Looks like we have actual data so inc the tid **
    pthread_mutex_lock(&task_count_lock);
    task->tid = task_count;
    task_count++;
    if (task_count > 1000000000) task_count = 0;
    pthread_mutex_unlock(&task_count_lock);

    cmd->version = -1;
    cmd->command = -1;
    sscanf(string_token(buffer, " ", &bstate, &fin), "%d", &(cmd->version));
    log_printf(10, "read_command: version=%d\n", cmd->version);
    flush_log();
    sscanf(string_token(NULL, " ", &bstate, &fin), "%d", &(cmd->command));

    log_printf(10, "read_command: ns=%d version = %d tid=" LU "* Command = %d\n", task->ns->id, cmd->version, task->tid, cmd->command);
    flush_log();

    if (cmd->version == -1) {
        send_cmd_result(task, IBP_E_BAD_FORMAT);
        cmd->state = CMD_STATE_FINISHED;
        return(-1);
    }

    //** Rest of arguments depends on the command **
    err = 0;
    if ((cmd->command<0) || (cmd->command > COMMAND_TABLE_MAX)) {
        log_printf(10, "read_command:  Unknown command! ns=%d\n", task->ns->id);
        send_cmd_result(task, IBP_E_BAD_FORMAT);
        cmd->state = CMD_STATE_FINISHED;
        err = -1;
    } else {
        mycmd = &(global_config->command[cmd->command]);
        if (mycmd->read != NULL) err = mycmd->read(task, &bstate);

        if (task->command_acl[cmd->command] == 0) {  //** Not allowed so err out
            log_printf(10, "read_command:  Can't execute command due to ACL restriction! ns=%d cmd=%d\n", task->ns->id, cmd->command);
            send_cmd_result(task, IBP_E_UNKNOWN_FUNCTION);
            cmd->state = CMD_STATE_FINISHED;
            err = -1;
        }
    }

    log_printf(10, "read_command: end of routine ns=%d tid=" LU " err = %d\n", task->ns->id, task->tid, err);

    return(err);

}
Ejemplo n.º 11
0
int main(int argc, char **argv)
{
  int bufsize = 1024*1024;
  char buffer[bufsize], *bstate;
  int err;
  int n;
  Net_timeout_t dt;


  if (argc < 3) {
     printf("get_version host port [timeout]\n");
     return(0);
  }

  char cmd[512];
  char *host = argv[1];
  int port = atoi(argv[2]);
  int timeout = 15;

  if (argc == 4) timeout = atoi(argv[3]);

  sprintf(cmd, "1 4 5 %d\n", timeout);  // IBP_ST_VERSION command

  dns_cache_init(10);

  NetStream_t *ns = new_netstream();
  ns_config_sock(ns, -1, 0);
  set_net_timeout(&dt, 5, 0);

  //** Make the host connection
  err = net_connect(ns, host, port, dt);
  if (err != 0) {
     printf("get_version:  Can't connect to host!  host=%s port=%d  err=%d\n", host, port, err);
     return(err);
  }


  //** Send the command
  n = write_netstream(ns, cmd, strlen(cmd), dt);
  n = readline_netstream(ns, buffer, bufsize, dt);
  if (n == IBP_OK) {  
     n = atoi(string_token(buffer, " ", &bstate, &err));
     if (n != IBP_OK) {
        printf("Error %d returned!\n", n);
        close_netstream(ns);
        return(n);
     }
  }

  //** Read the result.  Termination occurs when the line "END" is read.
  //** Note that readline_netstream strips the "\n" from the end of the line
  n = 1;
  while (n > 0) {
     n = readline_netstream(ns, buffer, bufsize, dt);
     if (n > 0) {     
        if (strcmp(buffer, "END") == 0) {
           n = 0;
        } else {  
           printf("%s\n", buffer);
        }
     }
  }

  //** Close the connection
  close_netstream(ns);

  return(0);
}
Ejemplo n.º 12
0
int main(int argc, char **argv)
{
  int bufsize = 1024*1024;
  char buffer[bufsize], *bstate;
  int err, i, mode;
  Net_timeout_t dt;
  time_t t;
  osd_id_t id;
  char print_time[128];
  uint64_t bytes, total_bytes;
  double fb1, fb2;
  uint64_t base_unit;
  double units;

  if (argc < 7) {
     printf("expire_list host port RID mode time count\n");
     printf("\n");
     printf("  mode  - abs or rel\n");
     printf("  time  - Future time with format of days:hours:min:sec\n");
     printf("  count - Number of allocations to retreive\n");
     printf("\n");
     return(0);
  }

  base_unit = 1024 * 1024;
  units = base_unit;
  units = 1.0 / units;

  i = 1;
  char *host = argv[i]; i++;
  int port = atoi(argv[i]); i++;
  char *rid = argv[i]; i++;
  mode = 0;
  if (strcmp(argv[i], "abs") == 0) mode = 1; 
  i++;
  t = parse_time(argv[i]); i++;
  int count = atoi(argv[i]);

  dns_cache_init(10);

  NetStream_t *ns = new_netstream();
  ns_config_sock(ns, -1, 0);

  set_net_timeout(&dt, 5, 0);

  err = net_connect(ns, host, port, dt);
  if (err != 0) {
     printf("get_alloc:  Can't connect to host!  host=%s port=%d  err=%d\n", host, port, err);
     return(err);
  }

  sprintf(buffer, "1 %d %s %d " TT " %d 10\n", INTERNAL_EXPIRE_LIST, rid, mode, t, count);
  err = write_netstream(ns, buffer, strlen(buffer), dt);
  err = readline_netstream(ns, buffer, bufsize, dt);
  if (err > 0) {  
     err = atoi(string_token(buffer, " ", &bstate, &err));
     if (err != IBP_OK) {
        printf("Error %d returned!\n", err);
        close_netstream(ns);
        return(err);
     }
  }

  //** Cycle through the data **
  total_bytes = 0;

  printf("n Time date ID  mb_max total_max_mb\n");
  printf("------------------------------------------------------------------------------------------------------\n");
  err = 0;  
  i = 0;
  while (err != -1) {
     buffer[0] = '\0';
     err = readline_netstream(ns, buffer, bufsize, dt);
//printf("err=%d buf=%s\n", err, buffer);
     if (err > 0) {     
        if (strcmp("END", buffer) == 0) { //** Finished
           err = -1;
        } else {
          parse_line(buffer, &t, &id, &bytes);
          ctime_r(&t, print_time); print_time[strlen(print_time)-1] = '\0';
          total_bytes = total_bytes + bytes;
          fb1 = bytes * units;
          fb2 = total_bytes  * units;

          printf("%4d " TT " * %s * " LU " * %lf * %lf\n", i, t, print_time, id, fb1, fb2);
        }
     }

     i++;
  }

  close_netstream(ns);

  printf("\n");

  return(0);
}