switch 0x100 constants to enums

This should make the code easier to maintain without having to remember
what the exact 0x100 constant means.
This commit is contained in:
Mike Frysinger 2022-03-05 14:17:14 -05:00
parent 5400dcc509
commit d796310456
2 changed files with 44 additions and 16 deletions

View File

@ -69,18 +69,32 @@ static struct pam_conv conv = { NULL, NULL};
#include "_usage.h" #include "_usage.h"
#include "helpers.h" #include "helpers.h"
/* Use long option value that is out of range for 8 bit getopt values.
* The exact enum value is internal and can freely change, so we keep the
* options sorted.
*/
enum {
/* This has to come first so following values stay in the 0x100+ range. */
LONGOPT_BASE = 0x100,
LONGOPT_CAPABILITIES,
LONGOPT_OOM_SCORE_ADJ,
LONGOPT_NO_NEW_PRIVS,
LONGOPT_SECBITS,
};
const char *applet = NULL; const char *applet = NULL;
const char *extraopts = NULL; const char *extraopts = NULL;
const char getoptstring[] = "I:KN:PR:Sa:bc:d:e:g:ik:mn:op:s:tu:r:w:x:1:2:3:4:" \ const char getoptstring[] = "I:KN:PR:Sa:bc:d:e:g:ik:mn:op:s:tu:r:w:x:1:2:3:4:" \
getoptstring_COMMON; getoptstring_COMMON;
const struct option longopts[] = { const struct option longopts[] = {
{ "capabilities", 1, NULL, 0x100}, { "capabilities", 1, NULL, LONGOPT_CAPABILITIES},
{ "secbits", 1, NULL, 0x101}, { "secbits", 1, NULL, LONGOPT_SECBITS},
{ "no-new-privs", 0, NULL, 0x102}, { "no-new-privs", 0, NULL, LONGOPT_NO_NEW_PRIVS},
{ "ionice", 1, NULL, 'I'}, { "ionice", 1, NULL, 'I'},
{ "stop", 0, NULL, 'K'}, { "stop", 0, NULL, 'K'},
{ "nicelevel", 1, NULL, 'N'}, { "nicelevel", 1, NULL, 'N'},
{ "oom-score-adj",1, NULL, 0x103}, { "oom-score-adj",1, NULL, LONGOPT_OOM_SCORE_ADJ},
{ "retry", 1, NULL, 'R'}, { "retry", 1, NULL, 'R'},
{ "start", 0, NULL, 'S'}, { "start", 0, NULL, 'S'},
{ "startas", 1, NULL, 'a'}, { "startas", 1, NULL, 'a'},
@ -371,7 +385,7 @@ int main(int argc, char **argv)
while ((opt = getopt_long(argc, argv, getoptstring, longopts, while ((opt = getopt_long(argc, argv, getoptstring, longopts,
(int *) 0)) != -1) (int *) 0)) != -1)
switch (opt) { switch (opt) {
case 0x100: case LONGOPT_CAPABILITIES:
#ifdef HAVE_CAP #ifdef HAVE_CAP
cap_iab = cap_iab_from_text(optarg); cap_iab = cap_iab_from_text(optarg);
if (cap_iab == NULL) if (cap_iab == NULL)
@ -381,7 +395,7 @@ int main(int argc, char **argv)
#endif #endif
break; break;
case 0x101: case LONGOPT_SECBITS:
#ifdef HAVE_CAP #ifdef HAVE_CAP
if (*optarg == '\0') if (*optarg == '\0')
eerrorx("Secbits are empty"); eerrorx("Secbits are empty");
@ -395,7 +409,7 @@ int main(int argc, char **argv)
#endif #endif
break; break;
case 0x102: case LONGOPT_NO_NEW_PRIVS:
#ifdef PR_SET_NO_NEW_PRIVS #ifdef PR_SET_NO_NEW_PRIVS
no_new_privs = true; no_new_privs = true;
#else #else
@ -424,7 +438,7 @@ int main(int argc, char **argv)
applet, optarg); applet, optarg);
break; break;
case 0x103: /* --oom-score-adj */ case LONGOPT_OOM_SCORE_ADJ: /* --oom-score-adj */
if (sscanf(optarg, "%d", &oom_score_adj) != 1) if (sscanf(optarg, "%d", &oom_score_adj) != 1)
eerrorx("%s: invalid oom-score-adj `%s'", eerrorx("%s: invalid oom-score-adj `%s'",
applet, optarg); applet, optarg);

View File

@ -71,6 +71,20 @@ static struct pam_conv conv = { NULL, NULL};
#include "_usage.h" #include "_usage.h"
#include "helpers.h" #include "helpers.h"
/* Use long option value that is out of range for 8 bit getopt values.
* The exact enum value is internal and can freely change, so we keep the
* options sorted.
*/
enum {
/* This has to come first so following values stay in the 0x100+ range. */
LONGOPT_BASE = 0x100,
LONGOPT_CAPABILITIES,
LONGOPT_OOM_SCORE_ADJ,
LONGOPT_NO_NEW_PRIVS,
LONGOPT_SECBITS,
};
const char *applet = NULL; const char *applet = NULL;
const char *extraopts = NULL; const char *extraopts = NULL;
const char getoptstring[] = "A:a:D:d:e:g:H:I:Kk:m:N:p:R:r:s:Su:1:2:3" \ const char getoptstring[] = "A:a:D:d:e:g:H:I:Kk:m:N:p:R:r:s:Su:1:2:3" \
@ -78,9 +92,9 @@ const char getoptstring[] = "A:a:D:d:e:g:H:I:Kk:m:N:p:R:r:s:Su:1:2:3" \
const struct option longopts[] = { const struct option longopts[] = {
{ "healthcheck-timer", 1, NULL, 'a'}, { "healthcheck-timer", 1, NULL, 'a'},
{ "healthcheck-delay", 1, NULL, 'A'}, { "healthcheck-delay", 1, NULL, 'A'},
{ "capabilities", 1, NULL, 0x100}, { "capabilities", 1, NULL, LONGOPT_CAPABILITIES},
{ "secbits", 1, NULL, 0x101}, { "secbits", 1, NULL, LONGOPT_SECBITS},
{ "no-new-privs", 0, NULL, 0x102}, { "no-new-privs", 0, NULL, LONGOPT_NO_NEW_PRIVS},
{ "respawn-delay", 1, NULL, 'D'}, { "respawn-delay", 1, NULL, 'D'},
{ "chdir", 1, NULL, 'd'}, { "chdir", 1, NULL, 'd'},
{ "env", 1, NULL, 'e'}, { "env", 1, NULL, 'e'},
@ -90,7 +104,7 @@ const struct option longopts[] = {
{ "umask", 1, NULL, 'k'}, { "umask", 1, NULL, 'k'},
{ "respawn-max", 1, NULL, 'm'}, { "respawn-max", 1, NULL, 'm'},
{ "nicelevel", 1, NULL, 'N'}, { "nicelevel", 1, NULL, 'N'},
{ "oom-score-adj",1, NULL, 0x103}, { "oom-score-adj",1, NULL, LONGOPT_OOM_SCORE_ADJ},
{ "pidfile", 1, NULL, 'p'}, { "pidfile", 1, NULL, 'p'},
{ "respawn-period", 1, NULL, 'P'}, { "respawn-period", 1, NULL, 'P'},
{ "retry", 1, NULL, 'R'}, { "retry", 1, NULL, 'R'},
@ -843,7 +857,7 @@ int main(int argc, char **argv)
eerrorx("%s: invalid health check delay %s", applet, optarg); eerrorx("%s: invalid health check delay %s", applet, optarg);
break; break;
case 0x100: case LONGOPT_CAPABILITIES:
#ifdef HAVE_CAP #ifdef HAVE_CAP
cap_iab = cap_iab_from_text(optarg); cap_iab = cap_iab_from_text(optarg);
if (cap_iab == NULL) if (cap_iab == NULL)
@ -853,7 +867,7 @@ int main(int argc, char **argv)
#endif #endif
break; break;
case 0x101: case LONGOPT_SECBITS:
#ifdef HAVE_CAP #ifdef HAVE_CAP
if (*optarg == '\0') if (*optarg == '\0')
eerrorx("Secbits are empty"); eerrorx("Secbits are empty");
@ -867,7 +881,7 @@ int main(int argc, char **argv)
#endif #endif
break; break;
case 0x102: case LONGOPT_NO_NEW_PRIVS:
#ifdef PR_SET_NO_NEW_PRIVS #ifdef PR_SET_NO_NEW_PRIVS
no_new_privs = true; no_new_privs = true;
#else #else
@ -902,7 +916,7 @@ int main(int argc, char **argv)
applet, optarg); applet, optarg);
break; break;
case 0x103: /* --oom-score-adj */ case LONGOPT_OOM_SCORE_ADJ: /* --oom-score-adj */
if (sscanf(optarg, "%d", &oom_score_adj) != 1) if (sscanf(optarg, "%d", &oom_score_adj) != 1)
eerrorx("%s: invalid oom-score-adj `%s'", eerrorx("%s: invalid oom-score-adj `%s'",
applet, optarg); applet, optarg);