setcap-error-message.patch 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. From: Zhi Li <lizhi1215@gmail.com>
  2. Date: Wed, 20 Jul 2011 09:35:48 +0800
  3. Subject: refine setcap output on errors (Closes: #633075)
  4. The error message of setcap is a little confused. From the error message it is
  5. hard to detect what is wrong. I removed the last 'usage' function call,
  6. because at that code point the 'usage' can not provide any useful information.
  7. Instead, I added a function 'mystrerror' which shall provide more information.
  8. Last-Update: 2016-02-07
  9. ---
  10. progs/setcap.c | 22 +++++++++++++++++++++-
  11. 1 file changed, 21 insertions(+), 1 deletion(-)
  12. diff --git a/progs/setcap.c b/progs/setcap.c
  13. index 7304343..4df1597 100644
  14. --- a/progs/setcap.c
  15. +++ b/progs/setcap.c
  16. @@ -22,6 +22,25 @@ static void usage(void)
  17. exit(1);
  18. }
  19. +static const char * mystrerror(int n)
  20. +{
  21. + struct my_error {
  22. + int num;
  23. + const char *desp;
  24. + }db[] = {
  25. + { EINVAL, "The value of the capability argument is not permitted for a file. Or the file is not a regular (non-symlink) file" },
  26. + { ENODATA, "Are you removing capabilities from a file? That file does not have any capability."},
  27. + {0, ""}
  28. + };
  29. +
  30. + struct my_error *p = &db[0];
  31. + while (p->num) {
  32. + if ( n == p->num ) return p->desp;
  33. + p++;
  34. + }
  35. + return "";
  36. +}
  37. +
  38. #define MAXCAP 2048
  39. static int read_caps(int quiet, const char *filename, char *buffer)
  40. @@ -196,7 +215,8 @@ int main(int argc, char **argv)
  41. "Failed to set capabilities on file `%s' (%s)\n",
  42. argv[0], strerror(oerrno));
  43. if (!explained) {
  44. - usage();
  45. + fprintf(stderr, "%s\n", mystrerror(errno));
  46. + exit(1);
  47. }
  48. }
  49. }