fchown/fchownat系統調用及示例

55. fchmod - 通過文件描述符改變文件權限

函數介紹

fchmod是一個Linux系統調用,用于通過文件描述符來改變文件的訪問權限。它是chmod函數的文件描述符版本,避免了路徑名解析。

函數原型

#include <sys/stat.h>
#include <unistd.h>int fchmod(int fd, mode_t mode);

功能

通過文件描述符改變文件的訪問權限(讀、寫、執行權限)。

參數

  • int fd: 已打開文件的文件描述符
  • mode_t mode: 新的文件權限模式
    • 八進制表示:如0644, 0755, 0600
    • 符號常量組合:如S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH

返回值

  • 成功時返回0
  • 失敗時返回-1,并設置errno

特殊限制

  • 需要對文件有適當的權限
  • 某些文件系統可能不支持
  • 只能修改調用者擁有的文件(除非是root)

相似函數

  • chmod(): 通過路徑名改變文件權限
  • fchmodat(): 相對路徑版本
  • chmodat(): 已廢棄的相對路徑版本

示例代碼

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>// 將權限模式轉換為可讀字符串
void print_permissions(mode_t mode) {char perms[11];strcpy(perms, "----------");// 用戶權限if (mode & S_IRUSR) perms[1] = 'r';if (mode & S_IWUSR) perms[2] = 'w';if (mode & S_IXUSR) perms[3] = 'x';// 組權限if (mode & S_IRGRP) perms[4] = 'r';if (mode & S_IWGRP) perms[5] = 'w';if (mode & S_IXGRP) perms[6] = 'x';// 其他用戶權限if (mode & S_IROTH) perms[7] = 'r';if (mode & S_IWOTH) perms[8] = 'w';if (mode & S_IXOTH) perms[9] = 'x';// 特殊位if (mode & S_ISUID) perms[3] = (perms[3] == 'x') ? 's' : 'S';if (mode & S_ISGID) perms[6] = (perms[6] == 'x') ? 's' : 'S';if (mode & S_ISVTX) perms[9] = (perms[9] == 'x') ? 't' : 'T';printf("%s", perms);
}// 獲取文件詳細信息
void print_file_info(const char* filename, int fd) {struct stat sb;if (fstat(fd, &sb) == -1) {perror("fstat失敗");return;}printf("文件 '%s' 的信息:\n", filename);printf("  inode: %ld\n", sb.st_ino);printf("  權限: ");print_permissions(sb.st_mode);printf(" (八進制: %o)\n", sb.st_mode & 0777);printf("  大小: %ld 字節\n", sb.st_size);printf("  所有者: %d", sb.st_uid);struct passwd *pw = getpwuid(sb.st_uid);if (pw) {printf(" (%s)", pw->pw_name);}printf("\n");printf("  所屬組: %d", sb.st_gid);struct group *gr = getgrgid(sb.st_gid);if (gr) {printf(" (%s)", gr->gr_name);}printf("\n\n");
}int main() {int fd;int result;printf("=== Fchmod 函數示例 ===\n");printf("當前用戶 UID: %d\n", getuid());printf("當前有效 UID: %d\n", geteuid());// 示例1: 基本使用printf("\n示例1: 基本使用\n");// 創建測試文件fd = open("test_fchmod.txt", O_CREAT | O_RDWR | O_TRUNC, 0666);if (fd == -1) {perror("創建測試文件失敗");exit(EXIT_FAILURE);}printf("創建測試文件: test_fchmod.txt\n");// 寫入測試數據const char* test_data = "This is test data for fchmod demonstration.\n";write(fd, test_data, strlen(test_data));// 顯示初始權限print_file_info("test_fchmod.txt", fd);// 示例2: 改變文件權限printf("示例2: 改變文件權限\n");// 設置為只讀模式 (0444)result = fchmod(fd, 0444);if (result == -1) {perror("設置只讀權限失敗");} else {printf("成功設置只讀權限 (0444)\n");print_file_info("test_fchmod.txt", fd);}// 嘗試寫入(應該失敗)const char* more_data = "More data";ssize_t bytes_written = write(fd, more_data, strlen(more_data));if (bytes_written == -1) {printf("寫入失敗(預期): %s\n", strerror(errno));}// 設置為讀寫模式 (0644)result = fchmod(fd, 0644);if (result == -1) {perror("設置讀寫權限失敗");} else {printf("成功設置讀寫權限 (0644)\n");print_file_info("test_fchmod.txt", fd);}// 現在應該可以寫入bytes_written = write(fd, more_data, strlen(more_data));if (bytes_written != -1) {printf("寫入成功,寫入 %ld 字節\n", bytes_written);}// 設置為可執行模式 (0755)result = fchmod(fd, 0755);if (result == -1) {perror("設置可執行權限失敗");} else {printf("成功設置可執行權限 (0755)\n");print_file_info("test_fchmod.txt", fd);}// 示例3: 使用符號常量printf("\n示例3: 使用符號常量\n");// 設置為用戶讀寫,組和其他用戶只讀mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;result = fchmod(fd, mode);if (result == -1) {perror("使用符號常量設置權限失敗");} else {printf("使用符號常量設置權限成功\n");printf("  模式: S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH\n");print_file_info("test_fchmod.txt", fd);}// 設置特殊位mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_ISUID;result = fchmod(fd, mode);if (result == -1) {perror("設置特殊位失敗");} else {printf("設置setuid位成功\n");print_file_info("test_fchmod.txt", fd);}// 示例4: 錯誤處理演示printf("\n示例4: 錯誤處理演示\n");// 使用無效的文件描述符result = fchmod(999, 0644);if (result == -1) {if (errno == EBADF) {printf("無效文件描述符錯誤處理正確: %s\n", strerror(errno));}}// 權限不足的情況(需要非root用戶測試)if (geteuid() != 0) {printf("非root用戶權限測試:\n");// 創建一個文件并關閉它,然后嘗試修改(應該失敗)int temp_fd = open("temp_file.txt", O_CREAT | O_WRONLY, 0644);if (temp_fd != -1) {close(temp_fd);// 重新以只讀方式打開temp_fd = open("temp_file.txt", O_RDONLY);if (temp_fd != -1) {result = fchmod(temp_fd, 0777);if (result == -1) {if (errno == EPERM || errno == EACCES) {printf("權限不足錯誤處理正確: %s\n", strerror(errno));}}close(temp_fd);}unlink("temp_file.txt");}}// 示例5: 與chmod的對比printf("\n示例5: 與chmod的對比\n");printf("fchmod vs chmod 對比:\n");printf("chmod(\"file.txt\", 0644):\n");printf("  - 通過路徑名指定文件\n");printf("  - 需要路徑解析\n");printf("  - 可能受符號鏈接影響\n\n");printf("fchmod(fd, 0644):\n");printf("  - 通過文件描述符指定文件\n");printf("  - 直接操作已打開的文件\n");printf("  - 不受路徑變化影響\n");printf("  - 更高效,避免路徑解析\n\n");// 示例6: 實際應用場景printf("示例6: 實際應用場景\n");printf("安全文件創建模式:\n");printf("int create_secure_file(const char* filename) {\n");printf("    int fd = open(filename, O_CREAT | O_WRONLY, 0600);\n");printf("    if (fd == -1) return -1;\n");printf("    \n");printf("    // 確保權限正確設置\n");printf("    if (fchmod(fd, 0600) == -1) {\n");printf("        close(fd);\n");printf("        return -1;\n");printf("    }\n");printf("    \n");printf("    return fd;\n");printf("}\n\n");printf("臨時文件權限管理:\n");printf("int create_temp_file() {\n");printf("    int fd = open(\"temp.dat\", O_CREAT | O_RDWR, 0600);\n");printf("    if (fd == -1) return -1;\n");printf("    \n");printf("    // 使用過程中保持私密權限\n");printf("    // ... 處理敏感數據 ...\n");printf("    \n");printf("    // 完成后調整權限\n");printf("    fchmod(fd, 0444);  // 只讀\n");printf("    \n");printf("    return fd;\n");printf("}\n\n");// 示例7: 原子性優勢printf("示例7: 原子性優勢\n");printf("fchmod的原子性優勢:\n");printf("1. 直接通過文件描述符操作\n");printf("2. 避免路徑解析過程中的競態條件\n");printf("3. 不受文件重命名影響\n");printf("4. 在文件移動后仍然有效\n\n");// 演示原子性優勢printf("原子性演示:\n");int atomic_fd = open("atomic_test.txt", O_CREAT | O_RDWR, 0666);if (atomic_fd != -1) {write(atomic_fd, "atomic test", 11);printf("創建文件并獲取文件描述符: %d\n", atomic_fd);// 重命名文件if (rename("atomic_test.txt", "atomic_test_renamed.txt") == 0) {printf("文件已重命名為: atomic_test_renamed.txt\n");// 仍然可以通過原來的文件描述符修改權限if (fchmod(atomic_fd, 0400) == 0) {printf("通過原文件描述符成功修改權限\n");print_file_info("atomic_test_renamed.txt", atomic_fd);}}close(atomic_fd);unlink("atomic_test_renamed.txt");}// 示例8: 權限安全最佳實踐printf("示例8: 權限安全最佳實踐\n");printf("文件權限安全建議:\n");printf("1. 私密文件使用 0600 (rw-------)\n");printf("2. 日志文件使用 0640 (rw-r-----)\n");printf("3. 配置文件使用 0644 (rw-r--r--)\n");printf("4. 可執行文件使用 0755 (rwxr-xr-x)\n");printf("5. 目錄使用 0755 (rwxr-xr-x)\n");printf("6. 臨時文件使用 0600 (rw-------)\n\n");printf("使用fchmod的安全模式:\n");printf("1. 創建時設置保守權限\n");printf("2. 根據需要調整權限\n");printf("3. 完成后收緊權限\n");printf("4. 驗證權限更改結果\n");printf("5. 及時處理錯誤情況\n\n");// 示例9: 性能考慮printf("示例9: 性能考慮\n");printf("fchmod性能優勢:\n");printf("1. 避免路徑解析開銷\n");printf("2. 直接操作內核文件結構\n");printf("3. 減少系統調用次數\n");printf("4. 在循環中修改多個文件時更高效\n\n");// 示例10: 錯誤恢復printf("示例10: 錯誤恢復\n");printf("健壯的權限管理:\n");printf("int safe_chmod(int fd, mode_t new_mode) {\n");printf("    struct stat old_stat;\n");printf("    if (fstat(fd, &old_stat) == -1) {\n");printf("        return -1;\n");printf("    }\n");printf("    \n");printf("    mode_t old_mode = old_stat.st_mode;\n");printf("    if (fchmod(fd, new_mode) == -1) {\n");printf("        // 可以選擇恢復原權限\n");printf("        // fchmod(fd, old_mode);\n");printf("        return -1;\n");printf("    }\n");printf("    \n");printf("    return 0;\n");printf("}\n\n");// 清理資源close(fd);unlink("test_fchmod.txt");printf("總結:\n");printf("fchmod是通過文件描述符修改文件權限的函數\n");printf("相比chmod具有更好的性能和原子性\n");printf("避免了路徑解析和符號鏈接問題\n");printf("適用于需要頻繁權限管理的場景\n");printf("是安全文件操作的重要工具\n");return 0;
}

56. fchmodat - 相對路徑改變文件權限

函數介紹

fchmodat是一個Linux系統調用,用于相對于指定目錄文件描述符改變文件的訪問權限。它是chmodfchmod的擴展版本。

函數原型

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags);

功能

相對于目錄文件描述符改變文件的訪問權限,支持相對路徑和額外標志。

參數

  • int dirfd: 目錄文件描述符
    • AT_FDCWD: 使用當前工作目錄
  • const char *pathname: 文件路徑名(相對或絕對)
  • mode_t mode: 新的文件權限模式
  • int flags: 控制標志
    • 0: 基本行為
    • AT_SYMLINK_NOFOLLOW: 不跟隨符號鏈接

返回值

  • 成功時返回0
  • 失敗時返回-1,并設置errno

特殊限制

  • 需要Linux 2.6.16以上內核支持
  • 某些標志需要特定內核版本
  • 需要適當的文件權限

相似函數

  • chmod(): 基礎版本(通過路徑名)
  • fchmod(): 文件描述符版本
  • chmodat(): 已廢棄的版本

示例代碼

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>// 創建測試環境
int setup_test_environment() {// 創建測試目錄if (mkdir("fchmodat_test", 0755) == -1 && errno != EEXIST) {perror("創建測試目錄失敗");return -1;}printf("創建測試目錄: fchmodat_test\n");// 在目錄中創建測試文件int fd = open("fchmodat_test/file1.txt", O_CREAT | O_WRONLY, 0666);if (fd != -1) {write(fd, "test file 1", 11);close(fd);printf("創建測試文件: fchmodat_test/file1.txt\n");}fd = open("fchmodat_test/file2.txt", O_CREAT | O_WRONLY, 0666);if (fd != -1) {write(fd, "test file 2", 11);close(fd);printf("創建測試文件: fchmodat_test/file2.txt\n");}// 創建子目錄if (mkdir("fchmodat_test/subdir", 0755) == -1 && errno != EEXIST) {perror("創建子目錄失敗");} else {printf("創建子目錄: fchmodat_test/subdir\n");fd = open("fchmodat_test/subdir/file3.txt", O_CREAT | O_WRONLY, 0666);if (fd != -1) {write(fd, "test file 3", 11);close(fd);printf("創建測試文件: fchmodat_test/subdir/file3.txt\n");}}return 0;
}// 顯示文件權限
void show_file_permissions(const char* filepath) {struct stat sb;if (stat(filepath, &sb) == 0) {printf("  %s: ", filepath);printf("%o (", sb.st_mode & 0777);// 簡單權限顯示if (sb.st_mode & S_IRUSR) printf("r"); else printf("-");if (sb.st_mode & S_IWUSR) printf("w"); else printf("-");if (sb.st_mode & S_IXUSR) printf("x"); else printf("-");if (sb.st_mode & S_IRGRP) printf("r"); else printf("-");if (sb.st_mode & S_IWGRP) printf("w"); else printf("-");if (sb.st_mode & S_IXGRP) printf("x"); else printf("-");if (sb.st_mode & S_IROTH) printf("r"); else printf("-");if (sb.st_mode & S_IWOTH) printf("w"); else printf("-");if (sb.st_mode & S_IXOTH) printf("x"); else printf("-");printf(")\n");}
}int main() {int dirfd, result;printf("=== Fchmodat 函數示例 ===\n");// 示例1: 基本使用printf("\n示例1: 基本使用\n");// 設置測試環境if (setup_test_environment() == -1) {exit(EXIT_FAILURE);}// 顯示初始權限printf("初始文件權限:\n");show_file_permissions("fchmodat_test/file1.txt");show_file_permissions("fchmodat_test/file2.txt");show_file_permissions("fchmodat_test/subdir/file3.txt");// 示例2: 相對于當前目錄printf("\n示例2: 相對于當前目錄\n");// 使用AT_FDCWD表示當前目錄result = fchmodat(AT_FDCWD, "fchmodat_test/file1.txt", 0400, 0);if (result == 0) {printf("成功修改文件權限為只讀 (0400)\n");show_file_permissions("fchmodat_test/file1.txt");} else {printf("修改權限失敗: %s\n", strerror(errno));}// 示例3: 相對于指定目錄printf("\n示例3: 相對于指定目錄\n");// 打開測試目錄獲取文件描述符dirfd = open("fchmodat_test", O_RDONLY);if (dirfd == -1) {perror("打開測試目錄失敗");goto cleanup;}printf("獲取目錄文件描述符: %d\n", dirfd);// 相對于目錄文件描述符修改文件權限result = fchmodat(dirfd, "file2.txt", 0600, 0);if (result == 0) {printf("相對于目錄描述符修改文件權限成功\n");show_file_permissions("fchmodat_test/file2.txt");} else {printf("相對路徑修改權限失敗: %s\n", strerror(errno));}// 修改子目錄中的文件result = fchmodat(dirfd, "subdir/file3.txt", 0755, 0);if (result == 0) {printf("修改子目錄中文件權限成功\n");show_file_permissions("fchmodat_test/subdir/file3.txt");} else {printf("修改子目錄文件權限失敗: %s\n", strerror(errno));}close(dirfd);// 示例4: 使用標志位printf("\n示例4: 使用標志位\n");// 創建符號鏈接進行測試if (symlink("fchmodat_test/file1.txt", "symlink_to_file1") == -1 && errno != EEXIST) {perror("創建符號鏈接失敗");} else {printf("創建符號鏈接: symlink_to_file1 -> fchmodat_test/file1.txt\n");// 默認行為(跟隨符號鏈接)result = fchmodat(AT_FDCWD, "symlink_to_file1", 0644, 0);if (result == 0) {printf("默認行為(跟隨符號鏈接)修改成功\n");} else {printf("默認行為修改失敗: %s\n", strerror(errno));}#ifdef AT_SYMLINK_NOFOLLOW// 不跟隨符號鏈接(這個標志在fchmodat中可能不被支持)printf("注意: AT_SYMLINK_NOFOLLOW在fchmodat中可能不被支持\n");printf("因為fchmodat主要用于修改權限,而不是鏈接本身\n");
#endifunlink("symlink_to_file1");}// 示例5: 錯誤處理演示printf("\n示例5: 錯誤處理演示\n");// 使用無效的目錄文件描述符result = fchmodat(999, "file.txt", 0644, 0);if (result == -1) {if (errno == EBADF) {printf("無效目錄文件描述符錯誤處理正確: %s\n", strerror(errno));}}// 修改不存在的文件result = fchmodat(AT_FDCWD, "fchmodat_test/nonexistent.txt", 0644, 0);if (result == -1) {if (errno == ENOENT) {printf("修改不存在文件錯誤處理正確: %s\n", strerror(errno));}}// 使用無效的權限模式(雖然不會報錯,但可能被截斷)result = fchmodat(AT_FDCWD, "fchmodat_test/file1.txt", 07777, 0);if (result == 0) {printf("使用大權限值可能被截斷\n");show_file_permissions("fchmodat_test/file1.txt");}// 使用無效的標志result = fchmodat(AT_FDCWD, "fchmodat_test/file1.txt", 0644, 0x1000);if (result == -1) {printf("無效標志處理: %s\n", strerror(errno));}// 示例6: 與相關函數對比printf("\n示例6: 與相關函數對比\n");printf("chmod() vs fchmod() vs fchmodat() 對比:\n");printf("chmod(\"path/file.txt\", 0644):\n");printf("  - 通過路徑名指定文件\n");printf("  - 需要完整的路徑解析\n");printf("  - 可能受符號鏈接影響\n\n");printf("fchmod(fd, 0644):\n");printf("  - 通過文件描述符指定文件\n");printf("  - 直接操作已打開的文件\n");printf("  - 最高效,但需要先打開文件\n\n");printf("fchmodat(dirfd, \"file.txt\", 0644, 0):\n");printf("  - 相對于目錄文件描述符\n");printf("  - 支持相對路徑\n");printf("  - 更靈活的路徑處理\n");printf("  - 支持額外標志\n\n");printf("fchmodat(AT_FDCWD, \"path/file.txt\", 0644, 0):\n");printf("  - 等同于chmod()但支持標志\n");printf("  - 更現代的接口\n\n");// 示例7: 實際應用場景printf("示例7: 實際應用場景\n");printf("批量文件權限修改:\n");printf("int batch_chmod_in_directory(const char* dirname, mode_t mode) {\n");printf("    int dirfd = open(dirname, O_RDONLY);\n");printf("    if (dirfd == -1) return -1;\n");printf("    \n");printf("    DIR* dir = fdopendir(dirfd);\n");printf("    if (!dir) {\n");printf("        close(dirfd);\n");printf("        return -1;\n");printf("    }\n");printf("    \n");printf("    struct dirent* entry;\n");printf("    while ((entry = readdir(dir)) != NULL) {\n");printf("        if (entry->d_name[0] != '.') {  // 跳過隱藏文件\n");printf("            fchmodat(dirfd, entry->d_name, mode, 0);\n");printf("        }\n");printf("    }\n");printf("    \n");printf("    closedir(dir);\n");printf("    return 0;\n");printf("}\n\n");printf("安全的相對路徑操作:\n");printf("int secure_chmod_in_sandbox(int sandbox_dirfd, \n");printf("                           const char* filename, mode_t mode) {\n");printf("    // 避免路徑遍歷攻擊\n");printf("    if (strstr(filename, \"../\") != NULL) {\n");printf("        return -1; // 拒絕不安全路徑\n");printf("    }\n");printf("    \n");printf("    return fchmodat(sandbox_dirfd, filename, mode, 0);\n");printf("}\n\n");// 示例8: 相對路徑優勢printf("示例8: 相對路徑優勢\n");printf("fchmodat相對路徑的優勢:\n");printf("1. 避免重復路徑解析\n");printf("2. 在chroot環境中更安全\n");printf("3. 支持原子性目錄操作\n");printf("4. 減少字符串操作\n");printf("5. 更好的錯誤隔離\n\n");// 演示相對路徑優勢printf("相對路徑優勢演示:\n");dirfd = open("fchmodat_test", O_RDONLY);if (dirfd != -1) {printf("使用目錄文件描述符進行批量操作:\n");// 修改多個文件權限const char* files[] = {"file1.txt", "file2.txt", "subdir/file3.txt"};mode_t modes[] = {0644, 0600, 0755};for (int i = 0; i < 3; i++) {result = fchmodat(dirfd, files[i], modes[i], 0);if (result == 0) {printf("  修改 %s 權限為 %o 成功\n", files[i], modes[i]);} else {printf("  修改 %s 權限失敗: %s\n", files[i], strerror(errno));}}close(dirfd);}// 示例9: 權限管理最佳實踐printf("示例9: 權限管理最佳實踐\n");printf("使用fchmodat的最佳實踐:\n");printf("1. 優先使用AT_FDCWD進行簡單操作\n");printf("2. 復雜目錄操作使用目錄文件描述符\n");printf("3. 合理驗證路徑安全性\n");printf("4. 及時處理錯誤情況\n");printf("5. 避免硬編碼絕對路徑\n");printf("6. 使用適當的權限模式\n\n");printf("權限設置建議:\n");printf("私密文件: 0600 (rw-------)\n");printf("用戶文件: 0644 (rw-r--r--)\n");printf("可執行文件: 0755 (rwxr-xr-x)\n");printf("目錄: 0755 (rwxr-xr-x)\n");printf("臨時文件: 0600 (rw-------)\n\n");// 示例10: 性能考慮printf("示例10: 性能考慮\n");printf("fchmodat性能特點:\n");printf("1. 相對于chmod減少路徑解析\n");printf("2. 相對于fchmod避免文件打開/關閉\n");printf("3. 目錄文件描述符可重復使用\n");printf("4. 批量操作效率更高\n");printf("5. 減少系統調用開銷\n\n");// 清理測試環境cleanup:printf("清理測試環境...\n");unlink("fchmodat_test/subdir/file3.txt");rmdir("fchmodat_test/subdir");unlink("fchmodat_test/file1.txt");unlink("fchmodat_test/file2.txt");rmdir("fchmodat_test");printf("\n總結:\n");printf("fchmodat是chmod的現代擴展版本\n");printf("支持相對目錄文件描述符\n");printf("提供額外的控制標志\n");printf("更安全的路徑處理\n");printf("適用于復雜的文件權限管理場景\n");printf("是現代Linux應用開發的重要工具\n");return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/93257.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/93257.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/93257.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

20250726-5-Kubernetes 網絡-Service 代理模式詳解(iptables與ipvs)_筆記

一、服務三種常用類型 ?? 1. LoadBalancer類型 工作原理:與NodePort類似,在每個節點上啟用端口暴露服務,同時Kubernetes會請求底層云平臺(如阿里云、騰訊云、AWS等)的負載均衡器,將每個Node([NodeIP]:[NodePort])作為后端添加。 自動化實現:云廠商通過官方實現的控制…

horizon置備出錯

報錯內容如下&#xff1a; [2025/7/28 19:15] 置備 Customization failure: Customization of the guest operating system is not supported due to the given reason: 期間出錯 解決方法&#xff1a;將模板轉換為虛擬機&#xff0c;安裝vmtools&#xff1b;再安裝vmtools之后…

【unitrix】 6.19 Ord特質(ord.rs)

一、源碼 這段代碼定義了一個標記特征&#xff08;marker trait&#xff09;Ord 和三個實現&#xff0c;用于將類型標記與 Rust 標準庫中的 Ordering 枚舉關聯起來。 use crate::sealed::Sealed; use core::cmp::Ordering; use crate::number::{Greater, Equal, Less}; /// 用于…

數據結構之順序表鏈表棧

順序表 什么是 list list 的使用 線性表是什么 順序表是什么 順序表和線性表的關系 順序表和數組的區別 List 和 ArrayList 的關系 如何自己模擬實現 myArrayList ArrayList 的構造 ArrayList 的常見方法 以下兩種寫法有什么區別 ArrayList<Integer> arrayLis…

day062-監控告警方式與Grafana優雅展示

文章目錄0. 老男孩思想-馬太效應1. API監控2. zabbix的API接口2.1 生成zabbix的api token2.2 訪問格式2.3 前端添加web監測3. 監控告警方式3.1 云監控-郵件告警3.1.1 郵箱開啟授權碼3.1.2 zabbix前端配置3.1.3 消息模板3.1.4 配置郵箱收件人信息3.1.5 配置觸發器3.2 企業微信告…

Ettus USRP X410/X440 運行 ADC 自校準

Ettus USRP X410/X440 運行 ADC 自校準 打開一個接收&#xff08;Rx&#xff09;會話到您在設備名稱輸入中指定的設備并返回會話句柄 out&#xff0c;您可以使用該句柄在所有后續 NI-USRP VI 中識別此儀器會話。 支持設備&#xff1a;Ettus USRP X410/X440輸入/輸出 文明.png 會…

Qt元類型系統(QMetaType)詳解

Qt元類型系統詳解一、Qt元類型系統(QMetaType)詳解1. 核心功能2. 注冊機制3. 關鍵技術點4. 信號槽支持5. 流式傳輸支持6. 使用場景7. 注意事項二、完整示例1、基本實例2、基本實例3、元類型在信號槽中的應用4、高級用法三、元對象編譯器moc元對象編譯器&#xff08;Moc&#xf…

《C++繼承詳解:從入門到理解公有、私有與保護繼承》

《C繼承詳解&#xff1a;從入門到理解公有、私有與保護繼承》 文章目錄《C繼承詳解&#xff1a;從入門到理解公有、私有與保護繼承》一、繼承的概念及定義1.1 繼承的概念1.2 繼承定義1.2.1 定義格式1.2.2 繼承基類成員訪問方式的變化1.3 繼承類模版二、基類和派生類間的轉換三、…

佳能iR-ADV C5560復印機如何掃描文件到電腦

打印機與電腦連接首先&#xff0c;確保佳能iR-ADV C5560復印機通過USB或Wi-Fi等網絡連接的方式成功連接到電腦。這可以通過USB線纜或Wi-Fi等網絡來實現。連接完成后&#xff0c;便可利用打印機內置的掃描功能&#xff0c;輕松將文件掃描并傳輸至電腦中。【掃描操作步驟】接下來…

騰訊AI IDE

1.官網說明&#xff1a;打開騰訊AI IDE官網。2.安裝說明&#xff1a;安裝成功后的界面。3.登錄 說明&#xff1a;通過郵箱和密碼登錄。4.成功說明&#xff1a;成功登錄如下界面。5.簡單一問說明&#xff1a;理解能力感覺不錯。擁有Claude-3.7-Sonnet??&#xff0c;??Claude…

【LeetCode 熱題 100】(一)哈希

1. 兩數之和 class Solution {public int[] twoSum(int[] nums, int target) {int length nums.length;// 1.聲明一個hashmap {nums[i], i}HashMap<Integer, Integer> map new HashMap<>();for (int i 0; i < length; i) {int second target - nums[i];if(m…

PMOS快速關斷電路、PMOS加速關斷電路

[電源系列]二、低成本MOS快速關斷電路原理分析 MOS的減速加速電路設計 分享一個微碧在網上看到的電路情況 加速電路1 PMOS關斷時間較長。 當用100kHz的頻率驅動PMOS時&#xff0c;PMOS G極的電壓信號并不是一個脈沖波&#xff0c;PMOS一直處于線性放大的狀態&#xff0c;并且…

Docker筆記(基本命令、掛載本地gpu、Dockerfile文件配置、數據掛載、docker換源)

Docker 主要用于環境搭建以及服務部署 基本命令 1.查看鏡像 docker images 2.查看容器 docker ps # 查看容器僅僅為查看運行狀態的容器 docker ps -a # 查看所有狀態的容器3.退出容器 exit4.刪除鏡像、容器 docker rm 鏡像ID docker rm 容器ID docker rm -f 容器ID # 強制刪除…

算法競賽階段二-數據結構(37)數據結構循環鏈表模擬實現

之前單鏈表中&#xff0c;數組全初始化為0&#xff0c;末尾最后一個next 存的就是0&#xff0c;指向的就是頭節點循環鏈表的基本概念循環鏈表是一種特殊的鏈表&#xff0c;其尾節點的指針域指向頭節點&#xff0c;形成一個閉環。與普通單鏈表相比&#xff0c;循環鏈表的遍歷需要…

20250727讓飛凌OK3576-C開發板在Rockchip的原廠Android14下通過耳機播音

20250727讓飛凌OK3576-C開發板在Rockchip的原廠Android14下通過耳機播音 2025/7/27 23:28緣起&#xff1a;很容易知道 飛凌OK3576-C開發板 使用的聲卡芯片是 NAU88C22YG 新唐科技(NUVOTON) NAU8822LYG NAU88C22YG 新唐立體聲音頻編解碼芯片原理圖&#xff1a;OK3576-C V1.2_202…

正向代理和反向代理的理解

**正向代理&#xff08;Forward Proxy&#xff09;和反向代理&#xff08;Reverse Proxy&#xff09;**是兩種不同類型的代理服務器&#xff0c;它們在數據傳輸過程中扮演的角色、使用場景以及工作方式都有所不同。 正向代理&#xff08;Forward Proxy&#xff09; 定義與作用&…

Java 后端 Cookie Session Token會話跟蹤技術

概述 會話從字面理解就是"兩方交流"&#xff0c;那問題就來了&#xff0c;HTTP&#xff08;超文本傳輸協議&#xff09;里面的"傳輸"不就包含了"兩方交流"的意思嗎&#xff1f;為什么要多此一舉提出會話技術呢&#xff1f; 談到這個&#xff0c;…

智譜AI GLM大模型 GLM-4-Plus的快速使用 ChatOpenAI類來調用GLM-4模型

智譜AIGLM-4&#xff0c;2024年1月16日發布的第四代基座大模型&#xff0c;其整體性能相較前代提升近60%&#xff0c;多維度指標逼近OpenAI的GPT-4水平。該模型支持128K上下文窗口&#xff08;約300頁文本處理能力&#xff09;&#xff0c;在長文本信息處理中實現100%精度召回。…

AsyncLocal淺復制的問題解決方案

針對C#中AsyncLocal<T>淺復制問題&#xff0c;以下是幾種主要的解決方案&#xff1a; 1. 使用不可變對象&#xff08;推薦&#xff09; 將存儲在AsyncLocal<T>中的對象設計為不可變的&#xff0c;避免修改共享狀態&#xff1a; public class ImmutableUserContext …

IIS發布.NET9 API 常見報錯匯總

記錄工作過程中發現的IIS常見錯誤。 1. HTTP Error 500.19 - Internal Server Error .NET 9 API --》vs打包方式如下&#xff1a; 發布到IIS后報錯HTTP Error 500.19 - Internal Server Error。 解決方案&#xff1a; 下載ASP.NET Core Hosting Bundle&#xff08;ASP.NET Co…