Beispiel #1
0
/* Pop up a message window similar to the confirm one but keep it open
   until agent_popup_message_stop has been called.  It is crucial for
   the caller to make sure that the stop function gets called as soon
   as the message is not anymore required because the message is
   system modal and all other attempts to use the pinentry will fail
   (after a timeout). */
int 
agent_popup_message_start (ctrl_t ctrl, const char *desc, const char *ok_btn)
{
  int rc;
  char line[ASSUAN_LINELENGTH];
  pth_attr_t tattr;

  rc = start_pinentry (ctrl);
  if (rc)
    return rc;

  if (desc)
    snprintf (line, DIM(line)-1, "SETDESC %s", desc);
  else
    snprintf (line, DIM(line)-1, "RESET");
  line[DIM(line)-1] = 0;
  rc = assuan_transact (entry_ctx, line, NULL, NULL, NULL, NULL, NULL, NULL);
  if (rc)
    return unlock_pinentry (rc);

  if (ok_btn)
    {
      snprintf (line, DIM(line)-1, "SETOK %s", ok_btn);
      line[DIM(line)-1] = 0;
      rc = assuan_transact (entry_ctx, line, NULL,NULL,NULL,NULL,NULL,NULL);
      if (rc)
        return unlock_pinentry (rc);
    }

  tattr = pth_attr_new();
  pth_attr_set (tattr, PTH_ATTR_JOINABLE, 1);
  pth_attr_set (tattr, PTH_ATTR_STACK_SIZE, 256*1024);
  pth_attr_set (tattr, PTH_ATTR_NAME, "popup-message");

  popup_finished = 0;
  popup_tid = pth_spawn (tattr, popup_message_thread, NULL);
  if (!popup_tid)
    {
      rc = gpg_error_from_syserror ();
      log_error ("error spawning popup message handler: %s\n",
                 strerror (errno) );
      pth_attr_destroy (tattr);
      return unlock_pinentry (rc);
    }
  pth_attr_destroy (tattr);

  return 0;
}
Beispiel #2
0
int
ldap_int_thread_destroy( void )
{
	pth_attr_destroy(detach_attr);
	pth_kill();
	return 0;
}
Beispiel #3
0
static void myexit(int sig)
{
    close(s);
    pth_attr_destroy(attr);
    pth_kill();
    fprintf(stderr, "**Break\n");
    exit(0);
}
Beispiel #4
0
void
Thread::Start ()
{
  if (tid)
    {
      pth_attr_t a = pth_attr_of (tid);
      int state;
      pth_attr_get (a, PTH_ATTR_STATE, &state);
      pth_attr_destroy (a);
      if (state != PTH_STATE_DEAD)
	return;
      Stop ();
    }
  pth_attr_t attr = pth_attr_new ();
  pth_attr_set (attr, PTH_ATTR_PRIO, prio);
  tid = pth_spawn (attr, &ThreadWrapper, this);
  pth_attr_destroy (attr);
}
Beispiel #5
0
bool Thread::isFinished ()
{
    if (tid ==0)
        return true;
    pth_attr_t a = pth_attr_of (tid);
    int state;
    pth_attr_get (a, PTH_ATTR_STATE, &state);
    pth_attr_destroy (a);
    return (state == PTH_STATE_DEAD);
}
Beispiel #6
0
void
Thread::StopDelete ()
{
    autodel = true;
    pth_sem_inc (&should_stop, FALSE);
    if (!tid)
        return;
    pth_attr_t at = pth_attr_of (tid);
    pth_attr_set (at, PTH_ATTR_JOINABLE, FALSE);
    pth_attr_destroy (at);
}
int
pth_attri_destroy (pth_attri_t *attri) {
	int rt = -1;
	if (attri != (pth_attri_t *)NULL) {
		rt = pth_attr_destroy(attri);
		if (rt == 0) {
			pth_attri_delete (attri);
			return CAF_OK;
		}
	}
	return CAF_ERROR_SUB;
}
Beispiel #8
0
void
Thread::Start (bool detach)
{
    if (joinable && tid)
    {
        pth_attr_t a = pth_attr_of (tid);
        int state;
        pth_attr_get (a, PTH_ATTR_STATE, &state);
        pth_attr_destroy (a);
        if (state != PTH_STATE_DEAD)
            return;
        Stop ();
    }
    pth_sem_init (&should_stop);
    pth_attr_t attr = pth_attr_new ();
    pth_attr_set (attr, PTH_ATTR_PRIO, prio);
    joinable = !detach;
    if (detach)
        pth_attr_set (attr, PTH_ATTR_JOINABLE, FALSE);
    tid = pth_spawn (attr, &ThreadWrapper, this);
    pth_attr_destroy (attr);
}
Beispiel #9
0
TASK_DLLEXPORT Task *
Task_new(const char *name, long stacksize, void *data, void *(*run) (void *data), void (*destroy) (void *data))
{
   Task *task = NEW(Task);

   pth_t ptask;

   pth_attr_t attr;

#ifdef _LDEBUG
   printf("F=%s,L=%d\n", __FILE__, __LINE__);
#endif
   if (pth_inited == 0)
   {
      if (pth_init() == 0)
	 return NULL;
      pth_inited = 1;
   }

   if (stacksize < TASK_STACK_MIN)
      stacksize = TASK_STACK_MIN;

   ++seqNo;
   task->id = seqNo;
   task->stacklen = stacksize;
   task->name = strdup(name);
   task->data = data;
   task->run = run;
   task->destroy = destroy;

   attr = pth_attr_new();
   pth_attr_set(attr, PTH_ATTR_NAME, name);
   pth_attr_set(attr, PTH_ATTR_STACK_SIZE, (unsigned int) stacksize);
   ptask = pth_spawn(attr, task_pth_run, task);
   pth_attr_destroy(attr);
   if (ptask == NULL)
   {
      free(task);
      return NULL;
   }
   task->ref = (void *) ptask;

#ifdef _LDEBUG
   printf("	open ptask=%p,task=%p,name=%s,%p\n", (void *) ptask, (void *) task, task->name, task->name);
   printf("F=%s,L=%d\n", __FILE__, __LINE__);
#endif
   return task;
}
Beispiel #10
0
/* Fire up a thread to copy data between STREAM and a pipe's
   descriptor FD.  With DIRECTION set to true the copy takes place
   from the stream to the pipe, otherwise from the pipe to the
   stream.  */
static gpg_error_t
start_feeder (estream_t stream, HANDLE hd, int direction)
{
#ifdef USE_NPTH
  gpg_error_t err;
  struct feeder_thread_parms *parm;
  pth_attr_t tattr;

  parm = xtrymalloc (sizeof *parm);
  if (!parm)
    return gpg_error_from_syserror ();
  parm->stream = stream;
  parm->stream_valid = 1;
  parm->hd = hd;
  parm->direction = direction;

  if (es_onclose (stream, 1, feeder_onclose_notification, parm))
    {
      err = gpg_error_from_syserror ();
      xfree (parm);
      return err;
    }

  tattr = pth_attr_new ();
  pth_attr_set (tattr, PTH_ATTR_JOINABLE, 0);
  pth_attr_set (tattr, PTH_ATTR_STACK_SIZE, 64*1024);
  pth_attr_set (tattr, PTH_ATTR_NAME, "exec-feeder");

  log_debug ("spawning new feeder(%p, %p, %d)\n", stream, hd, direction);
  if(!pth_spawn (tattr, feeder_thread, parm))
    {
      err = gpg_error_from_syserror ();
      es_onclose (stream, 0, feeder_onclose_notification, parm);
      xfree (parm);
    }
  else
    err = 0;
  pth_attr_destroy (tattr);

  return err;
#else
  (void)stream;
  (void)hd;
  (void)direction;
  return gpg_error (GPG_ERR_NOT_IMPLEMENTED);  /* No Pth.  */
#endif
}
Beispiel #11
0
int main(int argc, char *argv[])
{
    pth_attr_t attr;
    sigset_t sigs;

    pth_init();

    fprintf(stderr, "This is TEST_SIG, a Pth test using signals.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Hit CTRL-C three times to stop this test.\n");
    fprintf(stderr, "But only after all threads were terminated.\n");
    fprintf(stderr, "\n");

    fprintf(stderr, "main: init\n");

    /* block signals */
    pth_sigmask(SIG_SETMASK, NULL, &sigs);
    sigaddset(&sigs, SIGUSR1);
    sigaddset(&sigs, SIGUSR2);
    sigaddset(&sigs, SIGINT);
    pth_sigmask(SIG_SETMASK, &sigs, NULL);

    /* spawn childs */
    attr = pth_attr_new();
    pth_attr_set(attr, PTH_ATTR_NAME, "child1");
    child1 = pth_spawn(attr, child, (void *)"child1");
    pth_attr_set(attr, PTH_ATTR_NAME, "child2");
    child2 = pth_spawn(attr, child, (void *)"child2");
    pth_attr_set(attr, PTH_ATTR_NAME, "inthandler");
    pth_spawn(attr, inthandler, (void *)"inthandler");
    pth_attr_destroy(attr);

    /* wait until childs are finished */
    while (pth_join(NULL, NULL));

    fprintf(stderr, "main: exit\n");
    pth_kill();
    return 0;
}
Beispiel #12
0
int main(int argc, char *argv[])
{
    char caLine[MAXLINELEN];
    pth_event_t ev = NULL;
    pth_event_t evt = NULL;
    pth_t t_worker = NULL;
    pth_t t_ticker = NULL;
    pth_attr_t t_attr;
    pth_msgport_t mp = NULL;
    pth_msgport_t mp_worker = NULL;
    struct query *q = NULL;
    int n;

    if (!pth_init()) {
        perror("pth_init");
        exit(1);
    }

    /* murray added for tmp debug */
/* 
    pth_time_t intval, former;

    printf("-------------------------\n");
    pth_time_set(&former, PTH_TIME_NOW);
    pth_usleep(300);
    pth_time_set(&intval, PTH_TIME_NOW);
    pth_time_sub(&intval, &former);
    double val = pth_time_t2d(&intval);
    printf("the intval is [%f]\n", val);
    pth_debug2("the intval is [%f]\n", val);
    return 0;
*/


    fprintf(stderr, "This is TEST_MP, a Pth test using message ports.\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Lines on stdin are send to a worker thread via message\n");
    fprintf(stderr, "ports, translated to upper case by the worker thread and\n");
    fprintf(stderr, "send back to the main thread via message ports.\n");
    fprintf(stderr, "Additionally a useless ticker thread awakens every 5s.\n");
    fprintf(stderr, "Enter \"quit\" on stdin for stopping this test.\n");
    fprintf(stderr, "\n");

    t_attr = pth_attr_new();
    pth_attr_set(t_attr, PTH_ATTR_NAME, "worker");
    pth_attr_set(t_attr, PTH_ATTR_JOINABLE, TRUE);
    pth_attr_set(t_attr, PTH_ATTR_STACK_SIZE, 16*1024);
    t_worker = pth_spawn(t_attr, worker, NULL);
    pth_attr_set(t_attr, PTH_ATTR_NAME, "ticker");
    t_ticker = pth_spawn(t_attr, ticker, NULL);
    pth_attr_destroy(t_attr);
    pth_yield(NULL);

    mp_worker = pth_msgport_find("worker");
    mp = pth_msgport_create("main");
    q = (struct query *)malloc(sizeof(struct query));
    ev = pth_event(PTH_EVENT_MSG, mp);

    evt = NULL;
    for (;;) {
        if (evt == NULL)
            evt = pth_event(PTH_EVENT_TIME, pth_timeout(20,0));
        else
            evt = pth_event(PTH_EVENT_TIME|PTH_MODE_REUSE, evt, pth_timeout(20,0));
        n = pth_readline_ev(STDIN_FILENO, caLine, MAXLINELEN, evt);
        if (n == -1 && pth_event_status(evt) == PTH_STATUS_OCCURRED) {
            fprintf(stderr, "main: Hey, what are you waiting for? Type in something!\n");
            continue;
        }
        if (n < 0) {
            fprintf(stderr, "main: I/O read error on stdin\n");
            break;
        }
        if (n == 0) {
            fprintf(stderr, "main: EOF on stdin\n");
            break;
        }
        caLine[n-1] = NUL;
        if (strcmp(caLine, "quit") == 0) {
            fprintf(stderr, "main: quit\n");
            break;
        }
        fprintf(stderr, "main: out --> <%s>\n", caLine);
        q->string = caLine;
        q->head.m_replyport = mp;
        pth_msgport_put(mp_worker, (pth_message_t *)q);
        pth_wait(ev);
        q = (struct query *)pth_msgport_get(mp);
        fprintf(stderr, "main: in <-- <%s>\n", q->string);
    }

    free(q);
    pth_event_free(ev, PTH_FREE_THIS);
    pth_event_free(evt, PTH_FREE_THIS);
    pth_msgport_destroy(mp);
    pth_cancel(t_worker);
    pth_join(t_worker, NULL);
    pth_cancel(t_ticker);
    pth_join(t_ticker, NULL);
    pth_kill();
    return 0;
}
Beispiel #13
0
/* UNIX listener */
static void listener(IOCHAN h, int event)
{
    COMSTACK line = (COMSTACK) iochan_getdata(h);
    int res;

    if (event == EVENT_INPUT)
    {
        COMSTACK new_line;
        if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
                                   control_block.daemon_name)) < 0)
        {
            yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
            return;
        }
        else if (res == 1)
        {
            yaz_log(YLOG_WARN, "cs_listen incomplete");
            return;
        }
        new_line = cs_accept(line);
        if (!new_line)
        {
            yaz_log(YLOG_FATAL, "Accept failed.");
            iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
            return;
        }

        yaz_log(log_sessiondetail, "Connect from %s", cs_addrstr(new_line));

        no_sessions++;
        if (control_block.dynamic)
        {
            if ((res = fork()) < 0)
            {
                yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
                iochan_destroy(h);
                return;
            }
            else if (res == 0) /* child */
            {
                char nbuf[100];
                IOCHAN pp;

                for (pp = pListener; pp; pp = iochan_getnext(pp))
                {
                    COMSTACK l = (COMSTACK)iochan_getdata(pp);
                    cs_close(l);
                    iochan_destroy(pp);
                }
                sprintf(nbuf, "%s(%d)", me, no_sessions);
                yaz_log_init_prefix(nbuf);
                /* ensure that bend_stop is not called when each child exits -
                   only for the main process ..  */
                control_block.bend_stop = 0;
            }
            else /* parent */
            {
                cs_close(new_line);
                return;
            }
        }

        if (control_block.threads)
        {
#if YAZ_POSIX_THREADS
            pthread_t child_thread;
            pthread_create(&child_thread, 0, new_session, new_line);
            pthread_detach(child_thread);
#elif YAZ_GNU_THREADS
            pth_attr_t attr;
            pth_t child_thread;

            attr = pth_attr_new();
            pth_attr_set(attr, PTH_ATTR_JOINABLE, FALSE);
            pth_attr_set(attr, PTH_ATTR_STACK_SIZE, 32*1024);
            pth_attr_set(attr, PTH_ATTR_NAME, "session");
            yaz_log(YLOG_DEBUG, "pth_spawn begin");
            child_thread = pth_spawn(attr, new_session, new_line);
            yaz_log(YLOG_DEBUG, "pth_spawn finish");
            pth_attr_destroy(attr);
#else
            new_session(new_line);
#endif
        }
        else
            new_session(new_line);
    }
    else if (event == EVENT_TIMEOUT)
    {
        yaz_log(log_server, "Shutting down listener.");
        iochan_destroy(h);
    }
    else
    {
        yaz_log(YLOG_FATAL, "Bad event on listener.");
        iochan_destroy(h);
    }
}