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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| #include <grp.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <pwd.h>
typedef struct { gid_t gid; char name[256]; int is_member; } group_info_t;
void show_group_list(group_info_t *groups, int count) { printf("组列表 (%d 个组):\n", count); printf("%-8s %-20s %-10s\n", "GID", "组名", "成员状态"); printf("----------------------------------------\n"); for (int i = 0; i < count; i++) { printf("%-8d %-20s %-10s\n", groups[i].gid, groups[i].name, groups[i].is_member ? "是" : "否"); } printf("\n"); }
int get_current_groups(gid_t **groups) { int count = getgroups(0, NULL); if (count <= 0) { return count; } *groups = malloc(count * sizeof(gid_t)); if (!*groups) { return -1; } count = getgroups(count, *groups); return count; }
int is_group_in_list(gid_t gid, gid_t *groups, int count) { for (int i = 0; i < count; i++) { if (groups[i] == gid) { return 1; } } return 0; }
int demo_group_manager() { gid_t *current_groups = NULL; int current_count = 0; struct group *grp; group_info_t test_groups[10]; int test_count = 0; printf("=== 组管理工具演示 ===\n"); current_count = get_current_groups(¤t_groups); if (current_count < 0) { printf("获取当前附加组失败: %s\n", strerror(errno)); return -1; } printf("当前进程附加组数量: %d\n", current_count); if (current_count > 0) { printf("当前附加组ID: "); for (int i = 0; i < current_count; i++) { printf("%d ", current_groups[i]); } printf("\n\n"); } printf("准备测试组列表:\n"); const char *group_names[] = {"daemon", "sys", "adm", "tty", "disk", "audio", NULL}; for (int i = 0; group_names[i] && test_count < 10; i++) { grp = getgrnam(group_names[i]); if (grp) { test_groups[test_count].gid = grp->gr_gid; strncpy(test_groups[test_count].name, grp->gr_name, sizeof(test_groups[test_count].name) - 1); test_groups[test_count].name[sizeof(test_groups[test_count].name) - 1] = '\0'; test_groups[test_count].is_member = is_group_in_list(grp->gr_gid, current_groups, current_count); test_count++; printf(" 添加组: %s (ID: %d) %s\n", grp->gr_name, grp->gr_gid, test_groups[test_count-1].is_member ? "[当前成员]" : ""); } } if (test_count < 3) { for (int i = test_count; i < 3 && i < 10; i++) { test_groups[i].gid = 1000 + i; snprintf(test_groups[i].name, sizeof(test_groups[i].name), "group_%d", 1000 + i); test_groups[i].is_member = 0; test_count++; } } printf("\n"); show_group_list(test_groups, test_count); printf("尝试设置新的附加组列表:\n"); gid_t new_groups[5]; int new_count = 0; for (int i = 0; i < test_count && new_count < 5 && i < 3; i++) { new_groups[new_count++] = test_groups[i].gid; printf(" 添加组: %s (ID: %d)\n", test_groups[i].name, test_groups[i].gid); } int result = setgroups(new_count, new_groups); if (result == 0) { printf("✓ 附加组设置成功\n"); printf("\n验证设置结果:\n"); gid_t *verify_groups = NULL; int verify_count = get_current_groups(&verify_groups); if (verify_count > 0) { printf(" 验证组数量: %d\n", verify_count); printf(" 验证组ID: "); for (int i = 0; i < verify_count; i++) { printf("%d ", verify_groups[i]); } printf("\n"); } if (verify_groups) { free(verify_groups); } } else { printf("✗ 附加组设置失败: %s\n", strerror(errno)); if (errno == EPERM) { printf(" 需要root权限或CAP_SETGID能力\n"); } } if (current_groups) { free(current_groups); } return 0; }
int main() { return demo_group_manager(); }
|