INTERNAL void
agentman(EVENT_AGENTD_REQ *eagent) {
	int        pid, exitstat;
	int        me;
	int        rc;
	AGENT_REQ *areq;

	if (Debug)
	   printf("agentman: starting\n");

	/* Copy arg data into active Agent list & unblock parent */
	mp_lock(AgentLock);

printf("Allocating agent control block\n");
	areq = agent_alloc();
	if (!areq) {
		fprintf(stderr, "agentman: can't start %s\n", eagent->module);
		mp_exit(0);
	}
printf("Initializing agent control block\\n");

	memcpy(&areq->ereq, eagent, sizeof(EVENT_AGENTD_REQ));

	mp_unlock(AgentLock);

	/* Unblock parent thread */
	mp_incsema(AgentInit);

	if (Debug)
	   printf("agentman: starting as thread %d.%d\n", getpid(), mp_gettid());

	areq->pid    = 0;
	areq->status = AGENTD_AGENT_INIT;
	areq->start  = event_clock();

	if (areq->ereq.log[0] && areq->ereq.log[0] != ' ')
	    rc = start_logger(areq, areq->ereq.log, areq->ereq.module, areq->ereq.args, &pid);
	 else
	    rc = start_agent(areq, areq->ereq.module, areq->ereq.args, &pid);

	if (Debug)
	   printf("agentman: thread (%d.%d) done and exiting\n",
		   getpid(), mp_gettid());

	/* Copy to obiturary list */
	if (++Nrip == MAX_RIP) Nrip = 0;
	memcpy(Rip, areq, sizeof(AGENT_REQ));

	/* Remove entry for this agent */
	mp_lock(AgentLock);
	agent_free(areq);
	mp_unlock(AgentLock);

	mp_exit(0);
}
int main (int argc, char **argv) {
    /* Local Vars */
    int ret;
    int i;
    unsigned int len;
    gnutls_x509_crt_t cert[1];
    char *subject = NULL;
    size_t subject_len;
    time_t expiration_time, activation_time;


    /* Set signal handling and alarm */
    if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Init GnuTLS */
    gnutls_global_init();

    for(i = 0; i < cert_files; i++) {
        if (mp_verbose)
            printf("Cert: %s\n", cert_file[i]);

        /* Read the Cert */
        gnutls_datum_t data = { NULL, 0 };
        ret = mp_slurp(cert_file[i], &(data.data));
        data.size = ret;
        if (ret <= 0) {
            set_critical("Error loading cert file '%s'.", cert_file[i]);
            continue;
        }

        /* Load the Cert to a list. */
        len = 1;
        ret = gnutls_x509_crt_list_import(cert, &len, &data,
                                          GNUTLS_X509_FMT_PEM,
                                          GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED);
        if (ret < 0) {
            set_critical("%s error: %s", cert_file[i], gnutls_strerror(ret));
            continue;
        };

        /* Read der Cert CN */
        if (subject == NULL) {
            subject = mp_malloc(128);
            subject_len = 128;
        }
        ret = gnutls_x509_crt_get_dn_by_oid(cert[0],
                                            GNUTLS_OID_X520_COMMON_NAME, 0, 0,
                                            subject, &subject_len);
        if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
            subject_len+=1;
            subject = mp_realloc(subject, subject_len);
            ret = gnutls_x509_crt_get_dn_by_oid(cert[0],
                                                GNUTLS_OID_X520_COMMON_NAME, 0, 0,
                                                subject, &subject_len);
        }
        if (ret != 0) {
            set_critical("%s error: %s", cert_file[i], gnutls_strerror(ret));
            continue;
        }
        if (mp_verbose) {
            printf(" * Subject: %s\n", subject);
        }

        /* Check expire time */
        expiration_time = gnutls_x509_crt_get_expiration_time (cert[0]);
        activation_time = gnutls_x509_crt_get_activation_time (cert[0]);

        if (mp_verbose) {
            printf (" * Certificate is valid since: %s", ctime (&activation_time));
            printf (" * Certificate expires: %s", ctime (&expiration_time));
        }

        int days = (int)difftime(expiration_time, time(0))/86400;
        switch (get_status((expiration_time-time(0)), expire_thresholds)) {
        case STATE_OK:
            set_ok(cert_file[i]);
            break;
        case STATE_WARNING:
            set_warning("%s expires in %d day%s", cert_file[i], days, days==1?"":"s");
            break;
        case STATE_CRITICAL:
            set_critical("%s expires in %d day%s", cert_file[i], days, days==1?"":"s");
            break;
        }

        if (activation_time > time(0)) {
            int days = (int)difftime(activation_time, time(0))/86400;
            set_critical("%s activates in %d day%s", cert_file[i], days, days==1?"":"s");
        }
    }

    // Dissconnect
    gnutls_global_deinit ();

    mp_exit("X509");
}
INTERNAL void
reader(EVENT_AGENTD_LOG *etmp) {
	EVENT_AGENTD_LOG elogsaved, *elog;
	int  fd, to;
	int  eof, checkexit;
	int  i, rc;
	char c;

	memcpy(&elogsaved, etmp, sizeof(EVENT_AGENTD_LOG));

	mp_incsema(ReadInit);

	elog = &elogsaved;

	fd = elog->flags;
	to = elog->head.to;
/****
	on_sigpipe(fd, ignore_sigpipe);
 ****/

	elog->msg[0] = '\0';

	if (Verbose)
	  printf("reader: proc %d.%d monitoring %d.%s on fd %d, sending to client %d\n",
		   getpid(), mp_gettid(),
		   elog->pid,
		   elog->type==AGENTD_LOG_STDERR?"stderr":"stdout",
		   fd, to);

	eof = checkexit = 0;
	FOREVER {
		for (i = 0; i < AGENTD_MAXMSG-1; ) {
			rc = read(fd, &c, 1);
			if (rc < 0) {
				if (Exit != elog->pid) {
					fprintf(stderr, "reader: error reading fd %d\n", rc);
					perror("reader");
				}
				eof = 1;
				break;
			}
			if (rc == 0) {
				if (Debug) printf("reader: eof\n");
				eof = 1;
				break;
			}
			if (c == EofStr[0] && Exit == elog->pid) {
				eof = 1;
				break;
			}
			elog->msg[i++] = c;

			if (c == '\n') break;
			else checkexit = 0;
		}
		if (eof) break;

		elog->msg[i] = '\0';

		if (Verbose) {
			printf("%s [%d]: %s",
				elog->type==AGENTD_LOG_STDERR?"stderr":"stdout",
				fd, elog->msg);
			fflush(stdout);
		}
		event_send(to, ET_AGENTD_LOG, (EVENT *)elog, sizeof(*elog));
		i = 0;
	}

	/* Send any partial message */
	if (i != 0) {
		elog->msg[i] = '\0';

		if (Verbose) {
			printf("%s [%d]: %s",
				elog->type==AGENTD_LOG_STDERR?"stderr":"stdout",
				fd, elog->msg);
			fflush(stdout);
		}
		event_send(to, ET_AGENTD_LOG, (EVENT *)elog, sizeof(*elog));
	}

	if (Verbose)
	  printf("reader: close and exit fd %d on %s\n", fd,
		 (Exit == elog->pid)?"Exit PID":"eof");

	mp_incsema(Exitwait);

	close(fd);
	mp_exit(0);
}