--- freem/src/jobtab.c 2025/05/12 18:18:00 1.10 +++ freem/src/jobtab.c 2025/05/14 12:22:04 1.11 @@ -1,5 +1,5 @@ /* - * $Id: jobtab.c,v 1.10 2025/05/12 18:18:00 snw Exp $ + * $Id: jobtab.c,v 1.11 2025/05/14 12:22:04 snw Exp $ * job table implementation * * @@ -24,6 +24,9 @@ * 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 * @@ -118,23 +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; } - - logprintf (FM_LOG_INFO, "jobtab_get_sem: sleeping for retry [tries = %d]", tries); + + logprintf (FM_LOG_INFO, "jobtab_get_sem: sleeping for retry [tries = %d; errno = %d; error = %s]", tries, errno, strerror (errno)); sleep (1); } logprintf (FM_LOG_ERROR, "jobtab_get_sem: failed to acquire job table semaphore"); - + have_jobtab_sem = FALSE; return FALSE; } @@ -143,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)); + } } @@ -222,7 +234,7 @@ void job_remove(const pid_t pid) while ((t != NULL) && (t->pid != pid)) { p = t; - t = SBA(t->next); + t = SOA(t->next); } if (t == NULL) { @@ -273,7 +285,7 @@ void job_set_ecode(const pid_t target_pi 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); @@ -292,7 +304,7 @@ 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; } @@ -308,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); @@ -324,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); @@ -341,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++; @@ -362,7 +374,7 @@ job_slot_t *job_set_status(const pid_t t 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; @@ -388,8 +400,8 @@ void job_gc_mark(void) 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) { - + 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) { @@ -442,7 +454,7 @@ void job_gc_sweep(void) } - 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) { @@ -482,7 +494,7 @@ ipc_slot_t *job_send_ipc(const pid_t rec } - 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) { @@ -514,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; } @@ -536,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)));