Browse Source

[Feat] [1.0.0] Add vmnetproxy to getty

xcbosa mbp16 2 months ago
parent
commit
27b3b2fcb8
1 changed files with 13 additions and 1 deletions
  1. 13 1
      post_sbin/getty.c

+ 13 - 1
post_sbin/getty.c

@@ -645,6 +645,13 @@ void log_message(const char *format, ...);
 
 /* 文件操作函数 */
 
+static pthread_mutex_t file_lock;
+
+__attribute__((constructor))
+static void __file_lock_initiator(void) {
+    pthread_mutex_init(&file_lock, NULL);
+}
+
 /**
  * 打开代理文件
  * @param flags 打开文件的标志
@@ -664,6 +671,7 @@ int open_proxy_file(int flags) {
  * @param fd 文件描述符
  */
 void lock_file(int fd) {
+    pthread_mutex_lock(&file_lock);
     if (flock(fd, LOCK_EX) < 0) {
         log_message("无法锁定文件: %s", strerror(errno));
     }
@@ -679,6 +687,7 @@ void unlock_file(int fd) {
     if (flock(fd, LOCK_UN) < 0) {
         log_message("无法解锁文件: %s", strerror(errno));
     }
+    pthread_mutex_unlock(&file_lock);
 }
 
 /* 消息处理函数 */
@@ -1414,6 +1423,9 @@ void *message_handler_thread(void *arg) {
     while (1) {
         // 读取消息
         int bytes_read = read_message(proxy_fd, buffer, MAX_BUFFER_SIZE, 1); // 1表示client
+
+        log_message("[Debug] Read %d bytes", bytes_read);
+
         if (bytes_read <= 0) {
             usleep(100); // 如果没有消息,等待一段时间(100微秒)
             continue;
@@ -1460,7 +1472,7 @@ int vmnetproxy_main() {
     init_connection_map();
     
     // 打开代理文件
-    proxy_fd = open_proxy_file(O_RDWR | O_DIRECT);
+    proxy_fd = open_proxy_file(O_RDWR | O_DIRECT | O_SYNC);
     if (proxy_fd < 0) {
         log_message("无法打开代理文件,退出");
         return 1;