Przeglądaj źródła

[Feat] [1.0.0] Add vmnetproxy to getty

xcbosa mbp16 2 miesięcy temu
rodzic
commit
b75cb09457
1 zmienionych plików z 19 dodań i 6 usunięć
  1. 19 6
      post_sbin/getty.c

+ 19 - 6
post_sbin/getty.c

@@ -692,13 +692,24 @@ void unlock_file(int fd) {
  */
 int write_message(int fd, void *message, int size) {
     lock_file(fd);
-    
-    // 将文件指针移动到文件末尾(追加写入)
-    if (lseek(fd, 0, SEEK_END) < 0) {
-        log_message("无法定位文件指针到末尾: %s", strerror(errno));
+
+    // 将文件指针移动到文件开头
+    if (lseek(fd, 0, SEEK_SET) < 0) {
+        log_message("无法定位文件指针: %s", strerror(errno));
         unlock_file(fd);
         return -1;
     }
+
+    long total_message_size = 0;
+    while (1) {
+        int message_size;
+        if (read(fd, &message_size, sizeof(int)) != sizeof(int)) {
+            ftruncate(fd, total_message_size);
+            break;
+        }
+        total_message_size += message_size;
+        lseek(fd, total_message_size, SEEK_SET);
+    }
     
     // 写入消息大小(用于读取时分隔消息)
     int total_size = size + sizeof(int);
@@ -768,19 +779,21 @@ int read_message(int fd, void *buffer, int size, int target) {
         // 读取第一条消息的大小
         if (read(fd, &message_size, sizeof(int)) != sizeof(int)) {
             log_message("读取消息大小失败: %s", strerror(errno));
+            ftruncate(fd, total_message_size);
             unlock_file(fd);
             return -1;
         }
-
-        total_message_size += message_size;
     
         // 检查消息大小是否合理
         content_size = message_size - sizeof(int);
         if (content_size <= 0 || content_size > size) {
             log_message("消息大小不合理: %d", content_size);
+            ftruncate(fd, total_message_size);
             unlock_file(fd);
             return -1;
         }
+
+        total_message_size += message_size;
     
         // 先读取消息头部以检查target
         MessageHeader header;