Skip to content
Browse files

Consistent return values from procket executable

Remove macros and consistently return error codes.
  • Loading branch information...
1 parent 9266617 commit d4c76dd534538383616bc3dbf9683cc0024ab9ed @msantos msantos committed May 14, 2011
Showing with 42 additions and 41 deletions.
  1. +0 −26 c_src/procket.h
  2. +42 −15 c_src/procket_cmd.c
View
26 c_src/procket.h
@@ -62,32 +62,6 @@
#define MAXATOMLEN 255 /* ei.h: does not include terminating NULL */
-#define IS_ERR(x) do { \
- if ((x) == -1) return (-1); \
-} while (0)
-
-#define IS_NULL(x) do { \
- if ((x) == NULL) \
- errx(EXIT_FAILURE, "%s", #x); \
-} while (0);
-
-#define IS_FALSE(x) do { \
- if ((x) != 0) \
- errx(EXIT_FAILURE, "%s", #x); \
-} while (0);
-
-#define IS_LTZERO(x) do { \
- if ((x) < 0) \
- errx(EXIT_FAILURE, "%s", #x); \
-} while (0);
-
-#define VERBOSE(x, ...) do { \
- if (ep->verbose >= x) { \
- (void)fprintf (stderr, __VA_ARGS__); \
- } \
-} while (0)
-
-
extern char *__progname;
View
57 c_src/procket_cmd.c
@@ -42,6 +42,7 @@ int procket_pipe(PROCKET_STATE *ps);
int procket_open_socket(PROCKET_STATE *ps);
int procket_open_dev(PROCKET_STATE *ps);
int procket_open_char_dev(char *dev);
+void error_result(PROCKET_STATE *ps, int err);
void usage(PROCKET_STATE *ep);
enum {
@@ -57,7 +58,10 @@ main(int argc, char *argv[])
int ch = 0;
- IS_NULL(ps = calloc(1, sizeof(PROCKET_STATE)));
+ ps = calloc(1, sizeof(PROCKET_STATE));
+
+ if (ps == NULL)
+ error_result(ps, errno);
ps->ip = INADDR_ANY;
ps->backlog = BACKLOG;
@@ -77,7 +81,11 @@ main(int argc, char *argv[])
ps->family = atoi(optarg);
break;
case 'p': /* path to pipe */
- IS_NULL(ps->path = strdup(optarg));
+ ps->path = strdup(optarg);
+
+ if (ps->path == NULL)
+ error_result(ps, errno);
+
if (strlen(ps->path) >= UNIX_PATH_MAX)
usage(ps);
break;
@@ -88,10 +96,16 @@ main(int argc, char *argv[])
ps->type = atoi(optarg);
break;
case 'I': /* Interface name */
- IS_NULL(ps->ifname = strdup(optarg));
+ ps->ifname = strdup(optarg);
+
+ if (ps->ifname == NULL)
+ error_result(ps, errno);
break;
case 'd': { /* Open a character device */
- IS_NULL(ps->dev = strdup(optarg));
+ ps->dev = strdup(optarg);
+
+ if (ps->dev == NULL)
+ error_result(ps, errno);
if (procket_check_devname(ps->dev, 32) < 0)
usage(ps);
@@ -114,22 +128,23 @@ main(int argc, char *argv[])
if (ps->path == NULL)
usage(ps);
- if (argc > 0)
- IS_NULL(ps->address = strdup(argv[0]));
+ if (argc > 0) {
+ ps->address = strdup(argv[0]);
- if (procket_open_fd(ps) < 0) {
- (void)fprintf(stderr, "%s", strerror(errno));
- exit (-errno);
+ if (ps->address == NULL)
+ error_result(ps, errno);
}
- if (setgid(getgid()) == -1)
- err(EXIT_FAILURE, "setgid");
- if (setuid(getuid()) == -1)
- err(EXIT_FAILURE, "setuid");
+ if (procket_open_fd(ps) < 0)
+ error_result(ps, errno);
+
+ if ( (setgid(getgid()) < 0) || (setuid(getuid()) < 0))
+ error_result(ps, errno);
- IS_LTZERO(procket_pipe(ps));
+ if (procket_pipe(ps) < 0)
+ error_result(ps, errno);
- exit (0);
+ exit(0);
}
@@ -349,6 +364,18 @@ procket_open_char_dev(char *dev)
void
+error_result(PROCKET_STATE *ps, int err)
+{
+ if (ps->verbose > 0)
+ (void)fprintf(stderr, "%s", strerror(err));
+ else
+ (void)fprintf(stderr, "%d", err);
+
+ exit(-err);
+}
+
+
+ void
usage(PROCKET_STATE *ps)
{
(void)fprintf(stderr, "%s, %s\n", __progname, PROCKET_VERSION);

0 comments on commit d4c76dd

Please sign in to comment.
Something went wrong with that request. Please try again.