Mailinglist Archive: yast-devel (105 mails)

< Previous Next >
[yast-devel] [PATCH] yast2 exit codes
  • From: Stanislav Visnovsky <visnov@xxxxxxx>
  • Date: Thu, 25 Sep 2008 13:25:33 +0200
  • Message-id: <200809251325.33499.visnov@xxxxxxx>
Hi!

This is a long standing issue: the YCP clients cannot set the exit code for
the yast2 process. This makes a big piece of command line functionality
unusable.

Attached patch implements the following behavior:

Exit codes:
0 - everything fine
1 - too few arguments
5 - error in arguments
16 - generic client error
16 + x - client returned 'x' as exit code

This is not exactly nice, but it keeps backward compatibility with the already
returning exit codes.

Basically, if error code is >0 and < 16, this is parameter handling error.
if >= 16, you get the exit code by subtracting 16.
0 means no errors.

Clients can return only 2 types of exit codes:

boolean:
true - exit code 0
false - exit code 16

integer:
16 + value

Examples:

return nil; => exit code 0
return; => exit code 0
return true; => exit code 0
return false; => exit code 16
return "true"; => exit code 0 (it's string!)
return 2; => exit code 18
return -15; => exit code 1
return -100; => exit code 172 (process exit code is unsigned)

If there are no objections, I will commit the patch + documenting the behavior
in man page and in the generated docs.

Stano
Index: liby2/src/genericfrontend.cc
===================================================================
--- liby2/src/genericfrontend.cc (revision 51499)
+++ liby2/src/genericfrontend.cc (working copy)
@@ -48,7 +48,12 @@
using std::string;
ExecutionEnvironment ee;

-static const int YCP_ERROR = 16;
+enum error_codes {
+ YAST_OK = 0,
+ YAST_FEWARGUMENTS = 1,
+ YAST_OPTIONERROR = 5,
+ YAST_YCPERROR = 16
+};

static const char *progname = "genericfrontend";

@@ -262,7 +267,7 @@

if (!argv[arg]) {
print_usage ();
- exit (1);
+ exit (YAST_FEWARGUMENTS);
}

client_name = argv[arg];
@@ -299,13 +304,13 @@
if (option.isNull())
{
print_error ("Client option -s: Couldn't parse valid YCP value
from stdin");
- exit (5);
+ exit (YAST_OPTIONERROR);
}

if (!option->isList())
{
print_error ("Client option -s: Parsed YCP value is NOT a
YCPList");
- exit (5);
+ exit (YAST_OPTIONERROR);
}

arglist = option->asList(); // the option read _IS_ arglist
@@ -387,13 +392,13 @@
{
fprintf(stderr, "No server module given\n");
print_usage ();
- exit (5);
+ exit (YAST_OPTIONERROR);
}

// now create server
if (!argv[arg]) {
print_usage ();
- exit (1);
+ exit (YAST_FEWARGUMENTS);
}

server_name = argv[arg];
@@ -477,7 +482,7 @@
if (!argv[0])
{
fprintf (stderr, "Missing argv[0]. It is a NULL pointer.");
- exit (5);
+ exit (YAST_OPTIONERROR);
}

progname = basename (argv[0]); // get program name
@@ -501,12 +506,12 @@
if (argc < 2) {
fprintf (stderr, "\nToo few arguments");
print_usage();
- exit (1);
+ exit (YAST_FEWARGUMENTS);
}

if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
print_help ();
- exit (0);
+ exit (YAST_OK);
}

// client _AND_ server must be given
@@ -591,7 +596,7 @@
if (pos == NULL)
{
print_error ("Option %s argument must be in format
namespace=component", argv[arg-1]);
- exit (5);
+ exit (YAST_OPTIONERROR);
}
*pos = 0;
Y2ComponentBroker::registerNamespaceException (argv[arg], pos+1);
@@ -689,12 +694,12 @@
fprintf (stderr, " %s\n", i->c_str());

print_usage ();
- exit (5);
+ exit (YAST_OPTIONERROR);
}
if (dynamic_cast<Y2ErrorComponent *>(client))
{
print_error ("Error while creating client module %s", client_name);
- exit (5);
+ exit (YAST_OPTIONERROR);
}


@@ -729,10 +734,21 @@
// might be useful in tracking segmentation faults
y2milestone ("Finished YaST2 component '%s'", progname);

- if( !result.isNull () && result->isBoolean() )
- exit( result->asBoolean()->value() ? 0 : YCP_ERROR );
+ if( result.isNull () )
+ exit (YAST_OK);

- exit (EXIT_SUCCESS);
+ y2milestone( "Exiting with client return value '%s'", result->toString
().c_str ());
+
+ if( result->isBoolean () )
+ {
+ exit( result->asBoolean()->value() ? YAST_OK : YAST_YCPERROR );
+ }
+
+ if( result->isInteger () )
+ exit( YAST_YCPERROR + result->asInteger ()->value () );
+
+ // all other values
+ exit (YAST_OK);
}


< Previous Next >