3 * Job client low-level magic
5 * (c) 2019 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the distorted.org.uk chroot maintenance tools.
12 * distorted-chroot is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * distorted-chroot is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with distorted-chroot. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
34 #include <sys/types.h>
38 #define PY_SSIZE_T_CLEAN
41 /*----- Static variables --------------------------------------------------*/
43 static volatile int sigfd
= -1;
45 /*----- Main code ---------------------------------------------------------*/
47 static void chld(int sig
)
48 { int fd
= sigfd
; if (fd
!= -1) { close(fd
); sigfd
= -1; } }
50 static PyObject
*pymeth_jobclient(PyObject
*me
, PyObject
*arg
)
53 struct sigaction sa
, oldsa
;
54 sigset_t mask
, oldmask
;
61 /* Parse the arguments. */
62 if (!PyArg_ParseTuple(arg
, "i", &fd
)) goto end
;
64 /* Prepare to be able to block `SIGCHLD'. */
65 sigemptyset(&mask
); sigaddset(&mask
, SIGCHLD
);
66 if (sigprocmask(SIG_SETMASK
, 0, &oldmask
)) goto oserr
;
68 /* Establish the signal handler. */
70 sigemptyset(&sa
.sa_mask
); sigaddset(&sa
.sa_mask
, SIGCHLD
);
71 sa
.sa_flags
= SA_NOCLDSTOP
;
72 if (sigaction(SIGCHLD
, &sa
, &oldsa
)) goto oserr
;
75 /* Establish a copy of the file descriptor. If a signal occurs, we'll
76 * clobber this descriptor: see below. If we come back here, we'll
77 * probably need to reinstate this, but check just in case: we don't want a
81 if (sigprocmask(SIG_BLOCK
, &mask
, 0)) goto oserr
;
84 sfd
= dup(fd
); if (sfd
== -1) goto oserr
;
87 if (sigprocmask(SIG_UNBLOCK
, &mask
, 0)) goto oserr
;
89 /* Check for child processes. If a child exit somehow failed to be
90 * spotted earlier, this is our chance to notice and do something about it.
92 kid
= waitpid(-1, &st
, WNOHANG
);
93 if (kid
== -1 && errno
!= ECHILD
) goto oserr
;
95 { rc
= Py_BuildValue("(Oii)", Py_None
, kid
, st
); goto end
; }
97 /* Read from the pipe. If a child exits between the `waitpid' above and
98 * the `read' here, the signal handler will pop and close the descriptor,
99 * so `read' will report `EBADF'. If a child exits while we're waiting for
100 * a byte on the descriptor, then `read' will report `EINTR'. If anything
101 * like this happens, then we go back and try reaping children again.
103 Py_BEGIN_ALLOW_THREADS
104 n
= read(sigfd
, &ch
, 1);
106 if (n
== 1) rc
= Py_BuildValue("(cOO)", ch
, Py_None
, Py_None
);
107 else if (!n
) rc
= Py_BuildValue("(sOO)", "", Py_None
, Py_None
);
109 PyErr_SetString(PyExc_SystemError
, "impossible return from read");
111 } else if (errno
== EINTR
|| errno
== EBADF
) goto again
;
118 /* Report a system error. */
119 PyErr_SetFromErrnoWithFilename(PyExc_OSError
, 0); goto end
;
122 /* Clean up and return. */
124 sigprocmask(SIG_SETMASK
, &oldmask
, 0);
125 sigaction(SIGCHLD
, &oldsa
, 0);
127 sfd
= sigfd
; if (sfd
!= -1) { close(sfd
); sigfd
= -1; }
131 static PyMethodDef methods
[] = {
132 { "jobclient", pymeth_jobclient
, METH_VARARGS
,
133 "jobclient(FD) -> (TOKEN, PID, STATUS)" },
137 PyMODINIT_FUNC
initjobclient(void)
138 { Py_InitModule("jobclient", methods
); }
140 /*----- That's all, folks -------------------------------------------------*/