--- freem/src/jobtab.c 2025/01/19 02:04:04 1.1.1.1 +++ freem/src/jobtab.c 2025/05/01 21:02:31 1.9 @@ -1,23 +1,11 @@ /* - * * - * * * - * * * - * *************** - * * * * * - * * MUMPS * - * * * * * - * *************** - * * * - * * * - * * - * - * jobtab.c + * $Id: jobtab.c,v 1.9 2025/05/01 21:02:31 snw Exp $ * job table implementation * * - * Author: Serena Willis + * Author: Serena Willis * Copyright (C) 1998 MUG Deutschland - * Copyright (C) 2021, 2023 Coherent Logic Development LLC + * Copyright (C) 2021, 2023, 2025 Coherent Logic Development LLC * * * This file is part of FreeM. @@ -35,17 +23,41 @@ * You should have received a copy of the GNU Affero Public License * along with FreeM. If not, see . * + * $Log: jobtab.c,v $ + * 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 + * + * Revision 1.5 2025/03/24 02:57:49 snw + * Shared memory compatibility fixes for OS/2 + * + * Revision 1.4 2025/03/09 19:14:25 snw + * First phase of REUSE compliance and header reformat + * + * + * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC + * SPDX-License-Identifier: AGPL-3.0-or-later **/ #include #include #include #include +#include #include "mpsdef.h" #include "shmmgr.h" #include "jobtab.h" +#include "log.h" -#if !defined(__OpenBSD__) && !defined(__APPLE__) +#if !defined(__OpenBSD__) && !defined(__APPLE__) && !defined(__OS2__) union semun { int val; /* Value for SETVAL */ struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ @@ -68,20 +80,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"); } } @@ -89,8 +99,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,7 +116,6 @@ short jobtab_get_sem(void) struct sembuf s = {0, -1, IPC_NOWAIT}; if (have_jobtab_sem) { -// fprintf (stderr, "jobtab_get_sem: this process already owns the job table semaphore\r\n"); return TRUE; } @@ -115,12 +126,11 @@ short jobtab_get_sem(void) 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]", tries); 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; @@ -142,8 +152,7 @@ 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) { @@ -193,8 +202,8 @@ void job_remove(const pid_t pid) job_slot_t *t = 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"); + if (jobtab_get_sem () == FALSE) { + logprintf (FM_LOG_FATAL, "job_remove: failed to get job table semaphore"); exit (1); } @@ -224,6 +233,7 @@ void job_remove(const pid_t pid) jobtab_release_sem (); + locktab_unlock_all_by_pid (pid); } void job_request_stop(const pid_t target_pid) @@ -232,8 +242,7 @@ 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) { @@ -258,9 +267,8 @@ 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) { @@ -281,7 +289,6 @@ 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) { if (s->pid == target_pid) { @@ -349,9 +356,8 @@ 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) { @@ -374,31 +380,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 %d DEFUNCT\r\n", s->pid); - s->flags = JFLG_DEFUNCT; + 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 %d\r\n", 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 %d\r\n", 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 %d\r\n", s->pid); + logprintf (FM_LOG_INFO, "job_gc_mark: registering new interpreter process %ld", (long) s->pid); s->flags = JFLG_ALIVE; } } @@ -418,8 +436,7 @@ 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"); } @@ -427,7 +444,7 @@ void job_gc_sweep(void) if ((s->flags & JFLG_DEFUNCT) == JFLG_DEFUNCT) { - fprintf (stderr, "job_gc_sweep: sweeping DEFUNCT pid %ld\r\n", 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,9 +475,8 @@ 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"); }