{"id":271229,"date":"2023-04-30T20:07:58","date_gmt":"2023-04-30T12:07:58","guid":{"rendered":"https:\/\/lrxjmw.cn\/zwlhssrjxnhj.html"},"modified":"2023-04-30T20:07:58","modified_gmt":"2023-04-30T12:07:58","slug":"zwlhssrjxnhj","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/zwlhssrjxnhj.html","title":{"rendered":"\u638c\u63e1Linux syscall\u51fd\u6570\uff0c\u6df1\u5165\u89e3\u6790\u5185\u6838\u673a\u5236"},"content":{"rendered":"
Linux<\/a>\u64cd\u4f5c\u7cfb\u7edf<\/a>\u662f\u4e00\u4e2a\u5f00\u653e\u6e90\u4ee3\u7801\u3001\u81ea\u7531\u4f7f\u7528\u548c\u5206\u53d1\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u56e0\u6b64\u5b83\u7684\u5185\u6838\u4e00\u76f4\u90fd\u662f\u5e7f\u5927\u7a0b\u5e8f\u5458\u5b66\u4e60\u7814\u7a76\u7684\u70ed\u70b9\u3002\u5728Linux\u5185\u6838\u4e2d\uff0c\u6709\u4e00\u4e9b\u975e\u5e38\u91cd\u8981\u4e14\u57fa\u7840\u7684\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\uff0c\u4e5f\u5c31\u662fsyscall\u51fd\u6570\u3002\u672c\u6587\u5c06\u4ece9\u4e2a\u65b9\u9762\u8be6\u7ec6\u8bb2\u89e3syscall\u51fd\u6570\uff0c\u8ba9\u5927\u5bb6\u66f4\u597d\u5730\u4e86\u89e3\u5b83\u7684\u4f5c\u7528\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n \u65b9\u97621\uff1a\u4ec0\u4e48\u662fsyscall\u51fd\u6570<\/b><\/p>\n Syscall\u51fd\u6570\u662fLinux\u5185\u6838\u4e2d\u63d0\u4f9b\u7ed9\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u4f7f\u7528\u7684\u63a5\u53e3\uff0c\u4e5f\u79f0\u4e3a\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u4f7f\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u5411\u5185\u6838\u53d1\u51fa\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u5185\u6838\u8fd4\u56de\u7ed3\u679c\u3002Syscall\u51fd\u6570\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e0e\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u6865\u6881linux syscall\u51fd\u6570<\/strong>\uff0c\u662fLinux\u7cfb\u7edf\u4e2d\u6700\u57fa\u672c\u3001\u6700\u5e95\u5c42\u7684API\u4e4b\u4e00\u3002<\/p>\n \u65b9\u97622\uff1asyscall\u51fd\u6570\u5206\u7c7b<\/b><\/p>\n \u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u6240\u6709\u7684\u7cfb\u7edf\u8c03\u7528\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u552f\u4e00\u7684\u7f16\u53f7\u6765\u6807\u8bc6\u3002\u8fd9\u4e2a\u7f16\u53f7\u88ab\u79f0\u4e3a\u7cfb\u7edf\u8c03\u7528\u53f7\uff08syscall number\uff09\u3002\u6839\u636e\u4e0d\u540c\u7684\u529f\u80fd\u7279\u70b9\uff0csyscall\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u51e0\u7c7b\uff1a<\/p>\n 1.\u8fdb\u7a0b\u7ba1\u7406\uff1a\u5982fork\u3001execve\u7b49\uff1b<\/p>\n 2.\u6587\u4ef6\u7ba1\u7406\uff1a\u5982open\u3001read\u3001write\u7b49\uff1b<\/p>\n 3.\u7f51\u7edc\u7ba1\u7406\uff1a\u5982socket\u3001bind\u3001listen\u7b49\uff1b<\/p>\n 4.\u5185\u5b58\u7ba1\u7406\uff1a\u5982mmap\u3001munmap\u7b49\uff1b<\/p>\n 5.\u65f6\u95f4\u7ba1\u7406\uff1a\u5982time\u3001gettimeofday\u7b49\uff1b<\/p>\n 6.\u7528\u6237\u7ba1\u7406\uff1a\u5982getuid\u3001setuid\u7b49\u3002<\/p>\n \u65b9\u97623\uff1asyscall\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5<\/b><\/p>\n \u5728C\u8bed\u8a00\u4e2dredhat linux 9.0\u4e0b\u8f7d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7cfb\u7edf\u5934\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u6765\u8c03\u7528syscall\u51fd\u6570\u3002\u4f8b\u5982\uff0copen\u6587\u4ef6\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n \n \u5176\u4e2d\uff0cO_RDONLY\u662f\u6253\u5f00\u6587\u4ef6\u7684\u6a21\u5f0f\u4e4b\u4e00\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709O_WRONLY\u3001O_RDWR\u7b49\u6a21\u5f0f\u3002<\/p>\n \u65b9\u97624\uff1asyscall\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/b><\/p>\n \u7cfb\u7edf\u8c03\u7528\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u503c\uff0c\u4ee3\u8868\u6267\u884c\u7ed3\u679c\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u7cfb\u7edf\u8c03\u7528\u6267\u884c\u6210\u529f\uff0c\u5219\u8fd4\u56de0\uff1b\u5426\u5219\u8fd4\u56de\u4e00\u4e2a\u8d1f\u503c\u3002<\/p>\n \u65b9\u97625\uff1asyscall\u51fd\u6570\u7684\u9519\u8bef\u5904\u7406<\/b><\/p>\n \u5f53\u7cfb\u7edf\u8c03\u7528\u6267\u884c\u5931\u8d25\u65f6\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8d1f\u6570\uff0c\u5e76\u5c06errno\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u76f8\u5e94\u7684\u9519\u8bef\u7801\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528syscall\u51fd\u6570\u65f6\u9700\u8981\u6ce8\u610f\u9519\u8bef\u5904\u7406\u3002<\/p>\n \u65b9\u97626\uff1asyscall\u51fd\u6570\u7684\u5e95\u5c42\u5b9e\u73b0<\/b><\/p>\n <\/p>\n \u5728Linux\u5185\u6838\u4e2dlinux syscall\u51fd\u6570<\/strong>\uff0c\u6240\u6709\u7684\u7cfb\u7edf\u8c03\u7528\u90fd\u662f\u7531\u5185\u6838\u4e2d\u65ad\u5b9e\u73b0\u7684\u3002\u5f53\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u8c03\u7528syscall\u51fd\u6570\u65f6\uff0c\u4f1a\u89e6\u53d1\u4e00\u4e2a\u8f6f\u4e2d\u65ad\uff08\u4e5f\u79f0\u4e3a\u7cfb\u7edf\u8c03\u7528\u4e2d\u65ad\uff09\uff0c\u7136\u540e\u5185\u6838\u4f1a\u6839\u636e\u4f20\u5165\u53c2\u6570\u7684\u4e0d\u540c\u6765\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u7528\u6237\u7a7a\u95f4\u7a0b\u5e8f\u3002<\/p>\n \u65b9\u97627\uff1asyscall\u51fd\u6570\u548cglibc\u5e93<\/b><\/p>\n glibc\u5e93\u662fLinux\u7cfb\u7edf\u4e2d\u5e38\u7528\u7684C\u8bed\u8a00\u6807\u51c6\u5e93\uff0c\u5b83\u5c01\u88c5\u4e86\u5927\u91cf\u7684\u7cfb\u7edf\u8c03\u7528\u63a5\u53e3\uff0c\u5e76\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u3001\u66f4\u6613\u7528\u7684API\u3002\u5728\u4f7f\u7528glibc\u5e93\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u5176\u5c01\u88c5\u7684\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528syscall\u51fd\u6570\u3002<\/p>\n \u65b9\u97628\uff1a\u5982\u4f55\u67e5\u627esyscall\u51fd\u6570<\/b><\/p>\nc\n#include <fcntl.h>\n#include <unistd.h>\nint fd = open("\/path\/to\/file",O_RDONLY);\n<\/pre>\n<\/p>\n