From: "plaisthos (Code Review)" Date: Thu, 9 Jan 2025 14:42:44 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <857ead877a63a88afb17be062d04275cc49d1a51-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="PowZIPkgqtc="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Improve error reporting from AF_UNIX tun/tap support List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --PowZIPkgqtc= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/855?usp=3Demail to review the following change. Change subject: Improve error reporting from AF_UNIX tun/tap support ...................................................................... Improve error reporting from AF_UNIX tun/tap support When having a non-existent lwipovpn binary or similar problems, the error reporting would often only report read error that were harder to identify the real problem. Add the openvpn_waitpid_check method that checks for error conditions and reports a better message in cases of problems. Change-Id: I81cbecd19018290d85c6c77fba7769f040d66233 Signed-off-by: Arne Schwabe --- M src/openvpn/run_command.c M src/openvpn/run_command.h M src/openvpn/tun_afunix.c 3 files changed, 62 insertions(+), 8 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/55/855/1 diff --git a/src/openvpn/run_command.c b/src/openvpn/run_command.c index d757823..7f2cfb3 100644 --- a/src/openvpn/run_command.c +++ b/src/openvpn/run_command.c @@ -107,6 +107,35 @@ } bool +openvpn_waitpid_check(pid_t pid, const char *error_message, int msglevel) +{ + if (pid =3D=3D 0) + { + return false; + } + int status; + pid_t pidret =3D waitpid(pid, &status, WNOHANG); + if (pidret !=3D pid) + { + return true; + } + + if (WIFEXITED(status)) + { + msg(msglevel, "%sexternal program exited with error status: %d", + error_message, WEXITSTATUS(status)); + + } + else if (WIFSIGNALED(status)) + { + msg(msglevel, "%sexternal program received signal %d", + error_message, WTERMSIG(status)); + } + + return false; +} + +bool openvpn_execve_allowed(const unsigned int flags) { if (flags & S_SCRIPT) diff --git a/src/openvpn/run_command.h b/src/openvpn/run_command.h index c92edbc..366acaa 100644 --- a/src/openvpn/run_command.h +++ b/src/openvpn/run_command.h @@ -59,6 +59,19 @@ int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_messa= ge); + +/** Checks if a running process is still running. This is mainly useful + * for processes started with \c S_NOWAITPID + * @param pid pid of the process to be checked + * @param error_message prefixed of the message that be printed + * @param msglevel msglevel of the messages to be printed + * @return true if the process is still running, false if + * an error condition occurred + */ +bool +openvpn_waitpid_check(pid_t pid, const char *error_message, + int msglevel); + /** * Will run a script and return the exit code of the script if between * 0 and 255, -1 otherwise diff --git a/src/openvpn/tun_afunix.c b/src/openvpn/tun_afunix.c index 6b6c159..c626993 100644 --- a/src/openvpn/tun_afunix.c +++ b/src/openvpn/tun_afunix.c @@ -47,9 +47,12 @@ #include #include + + static void tun_afunix_exec_child(const char *dev_node, struct tuntap *tt, struct env_= set *env) { + const char *msgprefix =3D "ERROR: failure executing process for tun:"; struct argv argv =3D argv_new(); /* since we know that dev-node starts with unix: we can just skip that @@ -58,10 +61,12 @@ argv_printf(&argv, "%s", program); - argv_msg(M_INFO, &argv); tt->afunix.childprocess =3D openvpn_execve_check(&argv, env, S_NOWAITP= ID, - "ERROR: failure executi= ng " - "process for tun"); + msgprefix); + if (!openvpn_waitpid_check(tt->afunix.childprocess, msgprefix, M_WARN)) + { + tt->afunix.childprocess =3D 0; + } argv_free(&argv); } @@ -138,20 +143,27 @@ ssize_t write_tun_afunix(struct tuntap *tt, uint8_t *buf, int len) { - int ret; - pid_t pidret =3D waitpid(tt->afunix.childprocess, &ret, WNOHANG); - if (pidret =3D=3D tt->afunix.childprocess) + const char *msg =3D "ERROR: failure during write to AF_UNIX socket: "; + if (!openvpn_waitpid_check(tt->afunix.childprocess, msg, M_WARN)) { - msg(M_INFO, "Child process PID %d for afunix dead? Return code: %d= ", - tt->afunix.childprocess, ret); + tt->afunix.childprocess =3D 0; return -ENXIO; } + return write(tt->fd, buf, len); } ssize_t read_tun_afunix(struct tuntap *tt, uint8_t *buf, int len) { + const char *msg =3D "ERROR: failure during read from AF_UNIX socket: "; + if (!openvpn_waitpid_check(tt->afunix.childprocess, msg, M_WARN)) + { + tt->afunix.childprocess =3D 0; + } + /* do an actual read on the file descriptor even in the error case sin= ce + * we otherwise loop on this on this from select and spam the console + * with error messages */ return read(tt->fd, buf, len); } #else /* ifndef WIN32 */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/855?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I81cbecd19018290d85c6c77fba7769f040d66233 Gerrit-Change-Number: 855 Gerrit-PatchSet: 1 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --PowZIPkgqtc= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos would like flichtenheld to review this change.

Vi= ew Change

Improve error reporting from AF_UNIX tun/tap support

W= hen having a non-existent lwipovpn binary or similar problems, the
error= reporting would often only report read error that were harder to
identi= fy the real problem. Add the openvpn_waitpid_check method
that checks f= or error conditions and reports a better message in cases
of problems.
Change-Id: I81cbecd19018290d85c6c77fba7769f040d66233
Signed-off-by= : Arne Schwabe <arne@...1227...>
---
M src/openvpn/run_command.= c
M src/openvpn/run_command.h
M src/openvpn/tun_afunix.c
3 files c= hanged, 62 insertions(+), 8 deletions(-)

git pull ssh://gerrit.op=
envpn.net:29418/openvpn refs/changes/55/855/1
diff --git a/src/open=
vpn/run_command.c b/src/openvpn/run_command.c
index d757823= ..7f2cfb3 100644
--- a/src/openvpn/run_command.c
= +++ b/src/openvpn/run_command.c
@@ -107,6 +107,35 @@<= /span>
}

bool
+openvpn_waitpid_check(pid_t pid, con= st char *error_message, int msglevel)
+{
= + if (pid =3D=3D 0)
+ ret= urn false;
+ }
+ int status;<= br>+ pid_t pidret =3D waitpi= d(pid, &status, WNOHANG);
+ if (pidret !=3D pid)
+ {
= + return true;
+ }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (WIFEXITED(status))
+ {
+ msg(msglevel, "%sexternal= program exited with error status: %d",
+ error_message, WEXITSTATUS(status));
+
+ }
+ else if (WIFSIGNALED(status))
+ {
+ msg(msglevel, "%sexternal program receive= d signal %d",
+ = error_message, WTERMSIG(status));
+ }
+
+ retu= rn false;
+}+
+bool
openvpn_execve_allowed(co= nst unsigned int flags)
{
if (flags &= amp; S_SCRIPT)
diff --git a/src/openvpn/run_command.h b/src= /openvpn/run_command.h
index c92edbc..366acaa 100644=
--- a/src/openvpn/run_command.h
+++ b/src/openvpn= /run_command.h
@@ -59,6 +59,19 @@
int open= vpn_execve_check(const struct argv *a, const struct env_set *es,
= const unsigned int flags, const char *error= _message);

+
+/** Checks= if a running process is still running. This is mainly useful
+ * for processes started with \c = S_NOWAITPID

+ * @para= m pid pid of the process to be checked
+ * @param error_message prefixed of t= he message that be printed
+ * @param msglevel msglevel of the messages to be printed
+ * @return = true if the process is still running, false if
+ * an error cond= ition occurred
+ */
+bool
+openvpn_waitpid_check(pid_t pid, con= st char *error_message,
+ int msglevel);
+
/**
* Will run a = script and return the exit code of the script if between
= * 0 and 255, -1 otherwise
diff --git a/src/openvpn/tun_afun= ix.c b/src/openvpn/tun_afunix.c
index 6b6c159..c626993 1006= 44
--- a/src/openvpn/tun_afunix.c
+++ b/src= /openvpn/tun_afunix.c
@@ -47,9 +47,12 @@
#= include <signal.h>
#include <stdlib.h><= br>
++
static voi= d
tun_afunix_exec_child(const char *dev_node, struct tunta= p *tt, struct env_set *env)
{
+ const char *msgprefix =3D "ERROR: failu= re executing process for tun:";
struct argv argv = =3D argv_new();

/* since we know tha= t dev-node starts with unix: we can just skip that
@@ -58,1= 0 +61,12 @@

argv_printf(&argv, &= quot;%s", program);

- argv_msg(M_INFO, &argv);
= tt->afunix.childprocess =3D openvpn_execve_check(&argv, env, S_NOWAI= TPID,
- = "ERROR: failure executing "
- = "process for tun");
+ = msgprefix);
+ if (!openvpn_waitpid_check(tt->afunix.childprocess, msgprefix, = M_WARN))
+ {
+ tt->afunix.chi= ldprocess =3D 0;
+ = }
argv_free(&argv);
}
=
@@ -138,20 +143,27 @@
ssize_t
write_tun_afunix(struct tuntap *tt, uint8_t *buf, int len)
{
- in= t ret;
- pid_t pidre= t =3D waitpid(tt->afunix.childprocess, &ret, WNOHANG);
- if (pidret =3D=3D tt->afunix= .childprocess)

+ c= onst char *msg =3D "ERROR: failure during write to AF_UNIX socket: &qu= ot;;
+ if (!openvp= n_waitpid_check(tt->afunix.childprocess, msg, M_WARN))
= {
- msg(M_IN= FO, "Child process PID %d for afunix dead? Return code: %d",
- tt->afunix.= childprocess, ret);
+= tt->afunix.childprocess =3D 0;
return -= ENXIO;
}
+
return write(tt->fd, buf, len); }

ssize_t
read= _tun_afunix(struct tuntap *tt, uint8_t *buf, int len)
{
+ const char *msg = =3D "ERROR: failure during read from AF_UNIX socket: ";+ if (!openvpn_waitpid_chec= k(tt->afunix.childprocess, msg, M_WARN))
+ {
+ tt->afunix.childprocess =3D 0;
+ }
+ /* do an actual read on the file descriptor even in= the error case since
+ * we otherwise loop on this on this from select and spam the console=
+ * with error m= essages */
return read(tt->fd, buf, len); }
#else /* ifndef WIN32 */

To view, visit change 855. To unsubscribe, or for help writing mai= l filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I81cbecd19018290d85c6c77fba7= 769f040d66233
Gerrit-Change-Number: 855
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--PowZIPkgqtc=--