mardi 30 mars 2021

How to get error from popen/system script?

I would like to be able to enable the bash script in a C ++ program and catch any error number from it. Exactly what I mean is the "pon" script that manages pppd. Below I am pasting its contents:

#!/bin/sh

PPP_ON_BOOT=/etc/ppp/ppp_on_boot

case "$1" in
  -*) echo "
Usage: pon [provider] [arguments]

If pon is invoked without arguments, $PPP_ON_BOOT file will be
run, presuming it exists and is executable. Otherwise, a PPP connection
will be started using settings from /etc/ppp/peers/provider.
If you specify one argument, a PPP connection will be started using
settings from the appropriate file in the /etc/ppp/peers/ directory, and
any additional arguments supplied will be passed as extra arguments to
pppd.
"
      exit 0
      ;;
esac

if [ -z "$1" -a -x "$PPP_ON_BOOT" ]; then
  exec "$PPP_ON_BOOT"
fi

if [ -z "$1" -a ! -f /etc/ppp/peers/provider ]; then
  echo "
Please configure /etc/ppp/peers/provider or use a command line argument to
use another file in /etc/ppp/peers/ directory.
"
  exit 1
fi

if [ "$1" -a ! -f "/etc/ppp/peers/$1" ]; then
  echo "
The file /etc/ppp/peers/$1 does not exist.
"
  exit 1
fi

exec /usr/sbin/pppd call ${@:-provider}

It starts the pppd daemon which, according to the documentation (https://linux.die.net/man/8/pppd), returns a series of errors, such as:

1 Pppd has detached, or otherwise the connection was successfully established and terminated at the peer's request.
2 An immediately fatal error of some kind occurred, such as an essential system call failing, or running out of virtual memory.
3 An error was detected in processing the options given, such as two mutually exclusive options being used.
4 Pppd is not setuid-root and the invoking user is not root. 

I would like my application to run the "pon" script and then if there is an error, hook it to an int variable. Then, a separate function with the switch converts the error into a message.

How could I do this on Linux? Will "popen" or "system" be better? Do I have to add in the "pon" script to read the value from the pppd error so that my application can get it?

Aucun commentaire:

Enregistrer un commentaire