token.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (C) 2025 Didi Technology Co., Tao Chen */
  3. #ifndef _GNU_SOURCE
  4. #define _GNU_SOURCE
  5. #endif
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <stdbool.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <mntent.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include "json_writer.h"
  17. #include "main.h"
  18. #define MOUNTS_FILE "/proc/mounts"
  19. static struct {
  20. const char *header;
  21. const char *key;
  22. } sets[] = {
  23. {"allowed_cmds", "delegate_cmds"},
  24. {"allowed_maps", "delegate_maps"},
  25. {"allowed_progs", "delegate_progs"},
  26. {"allowed_attachs", "delegate_attachs"},
  27. };
  28. static bool has_delegate_options(const char *mnt_ops)
  29. {
  30. return strstr(mnt_ops, "delegate_cmds") ||
  31. strstr(mnt_ops, "delegate_maps") ||
  32. strstr(mnt_ops, "delegate_progs") ||
  33. strstr(mnt_ops, "delegate_attachs");
  34. }
  35. static char *get_delegate_value(char *opts, const char *key)
  36. {
  37. char *token, *rest, *ret = NULL;
  38. if (!opts)
  39. return NULL;
  40. for (token = strtok_r(opts, ",", &rest); token;
  41. token = strtok_r(NULL, ",", &rest)) {
  42. if (strncmp(token, key, strlen(key)) == 0 &&
  43. token[strlen(key)] == '=') {
  44. ret = token + strlen(key) + 1;
  45. break;
  46. }
  47. }
  48. return ret;
  49. }
  50. static void print_items_per_line(char *input, int items_per_line)
  51. {
  52. char *str, *rest;
  53. int cnt = 0;
  54. if (!input)
  55. return;
  56. for (str = strtok_r(input, ":", &rest); str;
  57. str = strtok_r(NULL, ":", &rest)) {
  58. if (cnt % items_per_line == 0)
  59. printf("\n\t ");
  60. printf("%-20s", str);
  61. cnt++;
  62. }
  63. }
  64. #define ITEMS_PER_LINE 4
  65. static void show_token_info_plain(struct mntent *mntent)
  66. {
  67. size_t i;
  68. printf("token_info %s", mntent->mnt_dir);
  69. for (i = 0; i < ARRAY_SIZE(sets); i++) {
  70. char *opts, *value;
  71. printf("\n\t%s:", sets[i].header);
  72. opts = strdup(mntent->mnt_opts);
  73. value = get_delegate_value(opts, sets[i].key);
  74. print_items_per_line(value, ITEMS_PER_LINE);
  75. free(opts);
  76. }
  77. printf("\n");
  78. }
  79. static void split_json_array_str(char *input)
  80. {
  81. char *str, *rest;
  82. if (!input) {
  83. jsonw_start_array(json_wtr);
  84. jsonw_end_array(json_wtr);
  85. return;
  86. }
  87. jsonw_start_array(json_wtr);
  88. for (str = strtok_r(input, ":", &rest); str;
  89. str = strtok_r(NULL, ":", &rest)) {
  90. jsonw_string(json_wtr, str);
  91. }
  92. jsonw_end_array(json_wtr);
  93. }
  94. static void show_token_info_json(struct mntent *mntent)
  95. {
  96. size_t i;
  97. jsonw_start_object(json_wtr);
  98. jsonw_string_field(json_wtr, "token_info", mntent->mnt_dir);
  99. for (i = 0; i < ARRAY_SIZE(sets); i++) {
  100. char *opts, *value;
  101. jsonw_name(json_wtr, sets[i].header);
  102. opts = strdup(mntent->mnt_opts);
  103. value = get_delegate_value(opts, sets[i].key);
  104. split_json_array_str(value);
  105. free(opts);
  106. }
  107. jsonw_end_object(json_wtr);
  108. }
  109. static int __show_token_info(struct mntent *mntent)
  110. {
  111. if (json_output)
  112. show_token_info_json(mntent);
  113. else
  114. show_token_info_plain(mntent);
  115. return 0;
  116. }
  117. static int show_token_info(void)
  118. {
  119. FILE *fp;
  120. struct mntent *ent;
  121. fp = setmntent(MOUNTS_FILE, "r");
  122. if (!fp) {
  123. p_err("Failed to open: %s", MOUNTS_FILE);
  124. return -1;
  125. }
  126. if (json_output)
  127. jsonw_start_array(json_wtr);
  128. while ((ent = getmntent(fp)) != NULL) {
  129. if (strncmp(ent->mnt_type, "bpf", 3) == 0) {
  130. if (has_delegate_options(ent->mnt_opts))
  131. __show_token_info(ent);
  132. }
  133. }
  134. if (json_output)
  135. jsonw_end_array(json_wtr);
  136. endmntent(fp);
  137. return 0;
  138. }
  139. static int do_show(int argc, char **argv)
  140. {
  141. if (argc)
  142. return BAD_ARG();
  143. return show_token_info();
  144. }
  145. static int do_help(int argc, char **argv)
  146. {
  147. if (json_output) {
  148. jsonw_null(json_wtr);
  149. return 0;
  150. }
  151. fprintf(stderr,
  152. "Usage: %1$s %2$s { show | list }\n"
  153. " %1$s %2$s help\n"
  154. " " HELP_SPEC_OPTIONS " }\n"
  155. "\n"
  156. "",
  157. bin_name, argv[-2]);
  158. return 0;
  159. }
  160. static const struct cmd cmds[] = {
  161. { "show", do_show },
  162. { "list", do_show },
  163. { "help", do_help },
  164. { 0 }
  165. };
  166. int do_token(int argc, char **argv)
  167. {
  168. return cmd_select(cmds, argc, argv, do_help);
  169. }