Esempio n. 1
0
static shproc_pool_t *shproc_pool_init(void)
{
  shproc_pool_t *pool;
  struct rlimit rlim;
  char buf[256];

  pool = (shproc_pool_t *)calloc(1, sizeof(shproc_pool_t));
  if (!pool)
    return (NULL);

  /* soft -> hard fd max / process */
  memset(&rlim, 0, sizeof(rlim));
  getrlimit(RLIMIT_NOFILE, &rlim);
  rlim.rlim_cur = MAX(rlim.rlim_cur, rlim.rlim_max);
  if (rlim.rlim_cur > 0)
    setrlimit(RLIMIT_NOFILE, &rlim);

  /* allocate enough slots for spawned workers */
  getrlimit(RLIMIT_NOFILE, &rlim);
  rlim.rlim_cur = MAX(rlim.rlim_cur, 1024);
  pool->pool_lim = rlim.rlim_cur;
  pool->proc = (shproc_t *)calloc(pool->pool_lim, sizeof(shproc_pool_t));

  /* maximum number of processes spawned at once. */
  pool->pool_max = SHPROC_POOL_DEFAULT_SIZE; /* default */

  /* set spawned process with same priority by default */
  pool->pool_prio = getpriority(PRIO_PROCESS, 0);

  sprintf(buf, "shproc_pool_init: initialized new pool #%x (max %d, limit %d).\n", pool, pool->pool_max, pool->pool_lim);
  shinfo(buf);

  return (pool);
}
Esempio n. 2
0
int shdev_usb_init(shdev_t *c_dev)
{
#ifdef USE_USB
  struct libusb_device *dev = NULL;
  struct libusb_config_descriptor *conf_desc;
  libusb_device_handle *hndl = NULL;
  char ebuf[1024];
  char buf[4096];
  char rbuf[1024];
  uint16_t max_len;
  unsigned int r_len;
  unsigned int len;
  int nb_ifaces;
  int val;
  int code;
  int iface;
  int err;
  int i;

  hndl = libusb_open_device_with_vid_pid(NULL, c_dev->def->arg1, c_dev->def->arg2);
  if (!hndl)
    return (SHERR_IO);

#ifdef __linux__
  /* detach kernel from device. */
  libusb_detach_kernel_driver(hndl, 0);
#endif

  /* Claim the device if it lists one or more interfaces available. */
  dev = libusb_get_device(hndl);
  libusb_get_config_descriptor(dev, 0, &conf_desc);
  nb_ifaces = conf_desc->bNumInterfaces;
  if (nb_ifaces) {
    for (iface = 0; iface < nb_ifaces; iface++) {
      err = libusb_claim_interface(hndl, iface);
      if (err != LIBUSB_SUCCESS)
        continue; /* in use */

      sprintf(ebuf, "claimed usb v%d:p%d (iface #%d).", c_dev->def->arg1, c_dev->def->arg2, iface);
      shinfo(ebuf);
fprintf(stderr, "DEBUG: %s\n", ebuf);
      break;
    }
    if (iface == nb_ifaces) {
      /* no slots left */
      return (SHERR_AGAIN);
    }
  }

  c_dev->usb = hndl;
  c_dev->iface = iface;
#endif
  return (0);
}
Esempio n. 3
0
void shlog_rinfo(void)
{
#if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRUSAGE)
  struct rusage rusage;
  char rinfo_buf[256];

  memset(&rusage, 0, sizeof(rusage));
  getrusage(RUSAGE_SELF, &rusage);

  sprintf(rinfo_buf,
      "PROCESS [cpu(user:%d.%-6.6ds sys:%d.%-6.6ds) maxrss(%uk) flt(%uk) swaps(%uk) in-ops(%uk) out-ops(%uk)]",
      rusage.ru_utime.tv_sec, rusage.ru_utime.tv_usec,
      rusage.ru_stime.tv_sec, rusage.ru_stime.tv_usec,
      rusage.ru_maxrss, rusage.ru_majflt, rusage.ru_nswap,
      rusage.ru_inblock, rusage.ru_oublock);

  shinfo(rinfo_buf);
#endif
}