--- freem/src/jobtab.c 2025/04/10 01:24:38 1.6
+++ freem/src/jobtab.c 2025/05/14 12:22:04 1.11
@@ -1,5 +1,5 @@
/*
- * $Id: jobtab.c,v 1.6 2025/04/10 01:24:38 snw Exp $
+ * $Id: jobtab.c,v 1.11 2025/05/14 12:22:04 snw Exp $
* job table implementation
*
*
@@ -24,6 +24,21 @@
* along with FreeM. If not, see .
*
* $Log: jobtab.c,v $
+ * Revision 1.11 2025/05/14 12:22:04 snw
+ * Further work on shared memory
+ *
+ * Revision 1.10 2025/05/12 18:18:00 snw
+ * Further work on shared memory
+ *
+ * Revision 1.9 2025/05/01 21:02:31 snw
+ * Documentation updates
+ *
+ * Revision 1.8 2025/04/30 20:03:09 snw
+ * Work on entryref parser
+ *
+ * Revision 1.7 2025/04/17 00:34:04 snw
+ * More logging improvements
+ *
* Revision 1.6 2025/04/10 01:24:38 snw
* Remove C++ style comments
*
@@ -41,10 +56,12 @@
#include
#include
#include
+#include
#include "mpsdef.h"
#include "shmmgr.h"
#include "jobtab.h"
+#include "log.h"
#if !defined(__OpenBSD__) && !defined(__APPLE__) && !defined(__OS2__)
union semun {
@@ -69,20 +86,18 @@ void jobtab_init(void)
semid_jobtab = semget (jt_sk, 1, 0666 | IPC_CREAT);
if (semid_jobtab == -1) {
- fprintf (stderr, "jobtab_init: failed to create job table semaphore\r\n");
- exit (1);
+ logprintf (FM_LOG_FATAL, "jobtab_init: failed to create job table semaphore (error code %d [%s])", errno, strerror (errno));
}
else {
- fprintf (stderr, "jobtab_init: job table semaphore created with semid %d\r\n", semid_jobtab);
+ logprintf (FM_LOG_INFO, "jobtab_init: job table semaphore created with semid %d", semid_jobtab);
}
arg.val = 1;
if (semctl (semid_jobtab, 0, SETVAL, arg) == -1) {
- fprintf (stderr, "jobtab_init: failed to initialize job table semaphore\r\n");
- exit (1);
+ logprintf (FM_LOG_FATAL, "jobtab_init: failed to initialize job table semaphore (error code %d [%s])", errno, strerror (errno));
}
else {
- fprintf (stderr, "jobtab_init: job table semaphore initialized\r\n");
+ logprintf (FM_LOG_INFO, "jobtab_init: job table semaphore initialized");
}
}
@@ -90,8 +105,10 @@ void jobtab_init(void)
semid_jobtab = semget (jt_sk, 1, 0);
if (semid_jobtab == -1) {
- fprintf (stderr, "jobtab_init: could not attach to job table semaphore\r\n");
- exit (1);
+ logprintf (FM_LOG_FATAL, "jobtab_init: could not attach to job table semaphore (error code %d [%s])", errno, strerror (errno));
+ }
+ else {
+ logprintf (FM_LOG_INFO, "jobtab_init: attached to job table semaphore (semid %d)", semid_jobtab);
}
}
@@ -104,24 +121,27 @@ short jobtab_get_sem(void)
int tries;
struct sembuf s = {0, -1, IPC_NOWAIT};
+ logprintf (FM_LOG_DEBUG, "jobtab_get_sem: attempting to acquire job table semaphore");
+
if (have_jobtab_sem) {
+ logprintf (FM_LOG_DEBUG, "jobtab_get_sem: acquired semaphore by virtue of already owning it");
return TRUE;
}
for (tries = 0; tries < 5; tries++) {
if (semop (semid_jobtab, &s, 1) != -1) {
+ logprintf (FM_LOG_DEBUG, "jobtab_get_sem: acquired semaphore by virtue of calling semop on it");
have_jobtab_sem = TRUE;
return TRUE;
}
-
- /* fprintf (stderr, "jobtab_get_sem: sleeping for retry [tries = %d]\r\n", tries); */
-
+
+ logprintf (FM_LOG_INFO, "jobtab_get_sem: sleeping for retry [tries = %d; errno = %d; error = %s]", tries, errno, strerror (errno));
sleep (1);
}
- fprintf (stderr, "jobtab_get_sem: fail\r\n");
-
+ logprintf (FM_LOG_ERROR, "jobtab_get_sem: failed to acquire job table semaphore");
+
have_jobtab_sem = FALSE;
return FALSE;
}
@@ -130,9 +150,14 @@ void jobtab_release_sem(void)
{
struct sembuf s = {0, 1, 0};
- semop (semid_jobtab, &s, 1);
-
- have_jobtab_sem = FALSE;
+ logprintf (FM_LOG_DEBUG, "jobtab_release_sem: trying to release job table semaphore");
+ if (semop (semid_jobtab, &s, 1) != -1) {
+ logprintf (FM_LOG_DEBUG, "jobtab_release_sem: released job table semaphore by means of semop");
+ have_jobtab_sem = FALSE;
+ }
+ else {
+ logprintf (FM_LOG_FATAL, "jobtab_release_sem: failed to release job table semaphore (error code %d [%s])", errno, strerror (errno));
+ }
}
@@ -142,11 +167,10 @@ job_slot_t *job_init(short is_fmadm)
job_slot_t *s;
if (jobtab_get_sem () == FALSE) {
- fprintf (stderr, "job_init: failed to get job table semaphore\r\n");
- exit (1);
+ logprintf (FM_LOG_FATAL, "job_init: failed to get job table semaphore");
}
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if (((s->flags & JFLG_DEFUNCT) == JFLG_DEFUNCT) || (s->pid == pid)) {
goto skip_alloc;
@@ -178,8 +202,8 @@ skip_alloc:
}
}
- s->next = shm_config->hdr->jobtab_head;
- shm_config->hdr->jobtab_head = s;
+ s->next = SBM(shm_config->hdr->jobtab_head);
+ shm_config->hdr->jobtab_head = SBM(s);
jobtab_release_sem ();
@@ -190,17 +214,16 @@ skip_alloc:
void job_remove(const pid_t pid)
{
- job_slot_t *t = shm_config->hdr->jobtab_head;
+ job_slot_t *t = SOM(shm_config->hdr->jobtab_head);
job_slot_t *p = NULL;
- if (jobtab_get_sem() == FALSE) {
- fprintf (stderr, "job_remove: failed to get job table semaphore\r\n");
- exit (1);
+ if (jobtab_get_sem () == FALSE) {
+ logprintf (FM_LOG_FATAL, "job_remove: failed to get job table semaphore");
}
if ((t != (job_slot_t *) NULL) && (t->pid == pid)) {
- shm_config->hdr->jobtab_head = t->next;
+ shm_config->hdr->jobtab_head = SBM(t->next);
shm_free (t);
jobtab_release_sem ();
@@ -211,7 +234,7 @@ void job_remove(const pid_t pid)
while ((t != NULL) && (t->pid != pid)) {
p = t;
- t = t->next;
+ t = SOA(t->next);
}
if (t == NULL) {
@@ -219,11 +242,12 @@ void job_remove(const pid_t pid)
return;
}
- p->next = t->next;
+ p->next = SBA(t->next);
shm_free (t);
jobtab_release_sem ();
+ locktab_unlock_all_by_pid (pid);
}
void job_request_stop(const pid_t target_pid)
@@ -232,11 +256,10 @@ void job_request_stop(const pid_t target
job_slot_t *s;
if (jobtab_get_sem() == FALSE) {
- fprintf (stderr, "job_request_stop: failed to get job table semaphore\r\n");
- exit (1);
+ logprintf (FM_LOG_FATAL, "job_request_stop: failed to get job table semaphore");
}
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if (s->pid == target_pid) {
s->stop_requested = pid;
@@ -258,12 +281,11 @@ void job_set_ecode(const pid_t target_pi
job_slot_t *s;
- if (jobtab_get_sem() == FALSE) {
- fprintf (stderr, "job_set_ecode: failed to get job table semaphore\r\n");
- exit (1);
+ if (jobtab_get_sem () == FALSE) {
+ logprintf (FM_LOG_FATAL, "job_set_ecode: failed to get job table semaphore");
}
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if (s->pid == target_pid) {
strncpy (s->last_ecode, ecode, 20);
@@ -281,9 +303,8 @@ pid_t job_stop_requested (const pid_t ta
{
job_slot_t *s;
-
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if (s->pid == target_pid) {
return s->stop_requested;
}
@@ -299,7 +320,7 @@ void job_request_all_stop(void)
job_slot_t *s;
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if ((s->flags & JFLG_DAEMON) != JFLG_DAEMON) {
job_request_stop (s->pid);
@@ -315,7 +336,7 @@ void job_signal_all(const int sig)
job_slot_t *s;
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if ((s->flags & JFLG_DAEMON) != JFLG_DAEMON) {
kill (s->pid, sig);
@@ -332,7 +353,7 @@ int job_count(void)
job_slot_t *s;
int ct = 0;
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if ((s->flags & JFLG_DEFUNCT) != JFLG_DEFUNCT) {
ct++;
@@ -349,12 +370,11 @@ job_slot_t *job_set_status(const pid_t t
job_slot_t *s;
- if (jobtab_get_sem() == FALSE) {
- fprintf (stderr, "job_set_status: failed to get job table semaphore\r\n");
- exit (1);
+ if (jobtab_get_sem () == FALSE) {
+ logprintf (FM_LOG_FATAL, "job_set_status: failed to get job table semaphore");
}
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if (s->pid == target_pid) {
s->status = status;
@@ -374,31 +394,43 @@ void job_gc_mark(void)
{
job_slot_t *s;
+ int jobstat;
if (jobtab_get_sem () == FALSE) {
- fprintf (stderr, "job_gc_mark: failed to get job table semaphore\r\n");
- exit (1);
+ logprintf (FM_LOG_FATAL, "job_gc_mark: failed to get job table semaphore");
}
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
-
- if (kill (s->pid, 0) != 0) {
- fprintf (stderr, "job_gc_mark: marking pid %ld DEFUNCT\r\n", (long) s->pid);
- s->flags = JFLG_DEFUNCT;
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
+ logprintf (FM_LOG_WARNING, "job_gc_mark looking at pid %ld", s->pid);
+ if ((jobstat = kill (s->pid, 0)) != 0) {
+
+ switch (jobstat) {
+
+ case EPERM:
+ logprintf (FM_LOG_WARNING, "job_gc_mark: environment daemon lacks permissions to pid %ld", (long) s->pid);
+ break;
+
+ case ESRCH:
+ logprintf (FM_LOG_INFO, "job_gc_mark: marking pid %ld DEFUNCT", (long) s->pid);
+ s->flags = JFLG_DEFUNCT;
+ break;
+
+ }
+
}
if ((s->flags & JFLG_NEW) == JFLG_NEW) {
if ((s->flags & JFLG_DAEMON) == JFLG_DAEMON) {
- fprintf (stderr, "job_gc_mark: registering new daemon %ld\r\n", (long) s->pid);
+ logprintf (FM_LOG_INFO, "job_gc_mark: registering new daemon %ld", (long) s->pid);
s->flags = JFLG_ALIVE | JFLG_DAEMON;
}
else {
if ((s->flags & JFLG_FMADM) == JFLG_FMADM) {
- fprintf (stderr, "job_gc_mark: registering new fmadm process %ld\r\n", (long) s->pid);
+ logprintf (FM_LOG_INFO, "job_gc_mark: registering new fmadm process %ld", (long) s->pid);
s->flags = JFLG_ALIVE | JFLG_FMADM;
}
else {
- fprintf (stderr, "job_gc_mark: registering new interpreter process %ld\r\n", (long) s->pid);
+ logprintf (FM_LOG_INFO, "job_gc_mark: registering new interpreter process %ld", (long) s->pid);
s->flags = JFLG_ALIVE;
}
}
@@ -418,16 +450,15 @@ void job_gc_sweep(void)
NULLPTRCHK(k_buf,"job_gc_sweep");
if (jobtab_get_sem () == FALSE) {
- fprintf (stderr, "job_gc_sweep: failed to get job table semaphore\r\n");
- exit (1);
+ logprintf (FM_LOG_FATAL, "job_gc_sweep: failed to get job table semaphore");
}
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if ((s->flags & JFLG_DEFUNCT) == JFLG_DEFUNCT) {
- fprintf (stderr, "job_gc_sweep: sweeping DEFUNCT pid %ld\r\n", (long) s->pid);
+ logprintf (FM_LOG_INFO, "job_gc_sweep: sweeping DEFUNCT pid %ld", (long) s->pid);
snprintf (k_buf, STRLEN - 1, "^$JOB\202%d\201", s->pid);
@@ -458,13 +489,12 @@ ipc_slot_t *job_send_ipc(const pid_t rec
ipc_slot_t *s = (ipc_slot_t *) shm_alloc (sizeof (ipc_slot_t));
NULLPTRCHK(s,"job_send_ipc");
- if (jobtab_get_sem() == FALSE) {
- fprintf (stderr, "job_send_ipc: failed to get job table semaphore\r\n");
- exit (1);
+ if (jobtab_get_sem () == FALSE) {
+ logprintf (FM_LOG_FATAL, "job_send_ipc: failed to get job table semaphore");
}
- for (j = shm_config->hdr->jobtab_head; j != NULL; j = j->next) {
+ for (j = SOA(shm_config->hdr->jobtab_head); j != NULL; j = SOA(j->next)) {
if (j->pid == receiver_pid) {
@@ -496,7 +526,7 @@ job_slot_t *job_get(const pid_t target_p
job_slot_t *s;
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
if (s->pid == target_pid) return s;
}
@@ -518,7 +548,7 @@ void job_dump(void)
printf ("%-10s%-15s%-20s%-22s%s\r\n", "PID", "STATUS", "LAST ECODE", "STARTED", "FLAGS");
printf ("%-10s%-15s%-20s%-22s%s\r\n", "---", "------", "----------", "-------", "-----");
- for (s = shm_config->hdr->jobtab_head; s != NULL; s = s->next) {
+ for (s = SOA(shm_config->hdr->jobtab_head); s != NULL; s = SOA(s->next)) {
strftime (time_buf, 20, "%Y-%m-%d %H:%M:%S", localtime (&(s->start_time)));