1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
/*
* capnames.c - Translate capability mask names
*
* Copyright © 2024 Craig Small <csmall@dropbear.xyz>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
//#include <unistd.h>
//#//include <signal.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
//#include <limits.h>
//#include <stdlib.h>
#include <string.h>
//#include <fcntl.h>
//#include <dirent.h>
//#include <ctype.h>
//#include <pwd.h>
#include <inttypes.h>
//#include <sys/types.h>
//#include <sys/wait.h>
//#include <sys/types.h>
//#include <sys/stat.h>
//#include <sys/utsname.h>
#include "procps-private.h"
#include "escape.h"
#include "capnames.h"
#include "misc.h"
#define FULL_CAP "full"
#define FULL_CAP_MASK 0x1ffffffffff
static bool capability_isset(const uint64_t mask, const int cnum)
{
return (mask & ((uint64_t) 1 << (cnum)));
}
PROCPS_EXPORT int procps_capmask_names(
char *str,
size_t size,
const char *capmask)
{
unsigned int i;
char *c = str;
uint64_t mask_in;
// buffer must be at least 2 for "-\0"
if (str == NULL || capmask == NULL || size < 2)
return -EINVAL;
if (1 != sscanf(capmask, "%" PRIx64, &mask_in))
return -EINVAL;
if (mask_in == 0) {
strcpy(str, "-");
return 1;
}
if (mask_in == FULL_CAP_MASK) {
size_t namelen;
namelen = strlen(FULL_CAP);
if (namelen+1 >= size) {
strcpy(c, "+");
return 1;
}
strcpy(c,FULL_CAP);
return namelen;
}
for (i=0; i <= (CAPABILITY_COUNT) ; i++)
{
if (capability_isset(mask_in, i)) {
if (cap_names[i] != NULL) { // We have a name for this capability
size_t namelen;
namelen = strlen(cap_names[i]);
if (namelen+1 >= size) {
strcpy(c, "+");
size -= 1;
c += 1;
break;
} else {
namelen = snprintf(c, size, (c==str)?"%s":",%s",
cap_names[i]);
size -= namelen;
c += namelen;
}
}
}
}
return (int) (c-str);
}
|