From: "cron2 (Code Review)" Date: Sat, 1 Feb 2025 18:37:51 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <10977ca928d0eeb596226c3209f0d3b0891406c5-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="MWycCLLWmoo="; 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: plaisthos , flichtenheld Cc: openvpn-devel --MWycCLLWmoo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#4) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/855?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 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 Acked-by: Gert Doering Message-Id: <20250201122006.32098-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30= 782.html Signed-off-by: Gert Doering --- M src/openvpn/run_command.c M src/openvpn/run_command.h M src/openvpn/tun_afunix.c 3 files changed, 81 insertions(+), 8 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/55/855/4 diff --git a/src/openvpn/run_command.c b/src/openvpn/run_command.c index d757823..192c8e6 100644 --- a/src/openvpn/run_command.c +++ b/src/openvpn/run_command.c @@ -106,6 +106,47 @@ return (const char *)out.data; } +#ifndef WIN32 +bool +openvpn_waitpid_check(pid_t pid, const char *msg_prefix, 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)) + { + int exitcode =3D WEXITSTATUS(status); + + if (exitcode =3D=3D OPENVPN_EXECVE_FAILURE) + { + msg(msglevel, "%scould not execute external program (exit code= 127)", + msg_prefix); + } + else + { + msg(msglevel, "%sexternal program exited with error status: %d= ", + msg_prefix, exitcode); + } + + } + else if (WIFSIGNALED(status)) + { + msg(msglevel, "%sexternal program received signal %d", + msg_prefix, WTERMSIG(status)); + } + + return false; +} +#endif /* ifndef WIN32 */ + bool openvpn_execve_allowed(const unsigned int flags) { diff --git a/src/openvpn/run_command.h b/src/openvpn/run_command.h index c92edbc..b0b51c3 100644 --- a/src/openvpn/run_command.h +++ b/src/openvpn/run_command.h @@ -59,6 +59,26 @@ int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_messa= ge); + +#ifndef WIN32 +/** Checks if a running process is still running. This is mainly useful + * for processes started with \c S_NOWAITPID + * + * This function is currently not implemented for Windows as the helper + * macros used by this function are not available. + * + * @param pid pid of the process to be checked + * @param msg_prefix 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 *msg_prefix, + int msglevel); + +#endif + /** * 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: 4 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --MWycCLLWmoo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #4 to the change originally cr= eated by plaisthos.

View Change

The following approvals got outdated= and were removed: Code-Review+2 by cron2

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<= br>identify the real problem. Add the openvpn_waitpid_check method
that= checks for error conditions and reports a better message in cases
of pr= oblems.

Change-Id: I81cbecd19018290d85c6c77fba7769f040d66233
Sign= ed-off-by: Arne Schwabe <arne@...1227...>
Acked-by: Gert Doering &= lt;gert@...1296...>
Message-Id: <20250201122006.32098-1-gert@...1711...= 96...>
URL: https://www.mail-archive.com/openvpn-devel@...1357...= rge.net/msg30782.html
Signed-off-by: Gert Doering <gert@...1296...>= ;
---
M src/openvpn/run_command.c
M src/openvpn/run_command.h
M= src/openvpn/tun_afunix.c
3 files changed, 81 insertions(+), 8 deletions= (-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/=
55/855/4
diff --git a/src/openvpn/run_command.c b/src/openvpn/run_c=
ommand.c
index d757823..192c8e6 100644
--- = a/src/openvpn/run_command.c
+++ b/src/openvpn/run_command.c=
@@ -106,6 +106,47 @@
return (const ch= ar *)out.data;
}

+#ifndef WIN32
+bool
+openvpn_waitpid_check(pid_t pid, const char *msg_prefix, int m= sglevel)
+{+ if (pid =3D=3D 0)<= br>+ {
+ return false;
+ }
+ int status;
+ pid_t pidret =3D waitpid(pid, &status, WNOHANG);<= /span>
+ if (pidret !=3D = pid)
+ {+ return true;+ }
+
+ if (WIFEXITED(status))
+ {
+ int exitcode =3D WEXITSTATUS(status);
+
+ if (exitcode =3D=3D OPENVPN_EXECVE_FAILURE)+ {
+ msg(msglevel, "%scoul= d not execute external program (exit code 127)",
+ msg_prefix);
+ }

+ else
+ {
+ msg(msglevel, "%sexternal program exited = with error status: %d",
+ msg_prefix, exitcode);
+ }
+
+ = }
+ else if (WI= FSIGNALED(status))
+ = {
+ msg(msg= level, "%sexternal program received signal %d",
+ msg_prefix, WTERMSIG(stat= us));
+ }+
+ return false;
+}
+#endif /* ifndef WIN32 */
+
bool
openvpn_execve_allowe= d(const unsigned int flags)
{
diff --git a= /src/openvpn/run_command.h b/src/openvpn/run_command.h
inde= x c92edbc..b0b51c3 100644
--- a/src/openvpn/run_command.h
+++ b/src/openvpn/run_command.h
@@ -59,6 +59= ,26 @@
int openvpn_execve_check(const struct argv *a, cons= t struct env_set *es,
const unsig= ned int flags, const char *error_message);

+

+#ifndef WIN32
+/** Checks if a running process is still running. This is m= ainly useful
+ * for = processes started with \c S_NOWAITPID
+ *
= + * This function is currently not implemented for Windows as the helper
+ * macros used by this= function are not available.
+ *
+ * @para= m pid pid of the process to be checked
+ * @param msg_prefix 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 conditi= on occurred
+ */
+bool
+openvpn_waitpid_check(pid_t pid, const = char *msg_prefix,
+ = int msglevel);
+
+#end= if
+
= /**
* 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
<= br>index 6b6c159..c626993 100644
--- a/src/openvpn/tu= n_afunix.c
+++ b/src/openvpn/tun_afunix.c
@= @ -47,9 +47,12 @@
#include <signal.h>
#include <stdlib.h>


+
+
static void
tun_afunix_exec_chil= d(const char *dev_node, struct tuntap *tt, struct env_set *env)
<= span> {

+ const ch= ar *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 jus= t skip that
@@ -58,10 +61,12 @@
argv_printf(&argv, "%s", program);

- argv_msg(M_INF= O, &argv);
tt->afunix.childprocess =3D openvpn_= execve_check(&argv, env, S_NOWAITPID,
- &quo= t;ERROR: failure executing "
- "proces= s for tun");
+ = msgprefix);
+ if (!openvpn_waitpid_check(tt-= >afunix.childprocess, msgprefix, M_WARN))

+ {
+ tt->afunix.childprocess =3D 0;
+ }
argv_free(&= ;argv);
}

@@ -138,20 +14= 3,27 @@
ssize_t
write_tun_afunix(struct t= untap *tt, uint8_t *buf, int len)
{
- int ret;
- pid_t pidret =3D waitpid(tt->afunix.childproc= ess, &ret, WNOHANG);
+ const char *msg =3D "ERROR: fail= ure during write to AF_UNIX socket: ";
+ if (!openvpn_waitpid_check(tt->afunix.childp= rocess, msg, M_WARN))
{
- msg(M_INFO, "Child process PID %d for a= funix dead? Return code: %d",
- tt->afunix.childprocess, ret);
+ tt->afunix.childprocess = =3D 0;
return -ENXIO;
}
+
ret= urn write(tt->fd, buf, len);
}
=
ssize_t
read_tun_afunix(struct tuntap *tt, uint= 8_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))
+ {<= br>+ tt->afunix.child= process =3D 0;
+ }=
+ /* do an actual= read on the file descriptor even in the error case since
+ * we otherwise loop on this on t= his from select and spam the console
+ * with error messages */
return = read(tt->fd, buf, len);
}
#else /* if= ndef WIN32 */

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

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I81cbecd19018290d85c6c77fba7= 769f040d66233
Gerrit-Change-Number: 855
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--MWycCLLWmoo=--