Revision bc4a957c osdep.c

b/osdep.c
73 73
#endif
74 74
}
75 75

  
76
int qemu_create_pidfile(const char *filename)
77
{
78
    char buffer[128];
79
    int len;
80
#ifndef _WIN32
81
    int fd;
82

  
83
    fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
84
    if (fd == -1)
85
        return -1;
86

  
87
    if (lockf(fd, F_TLOCK, 0) == -1)
88
        return -1;
89

  
90
    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
91
    if (write(fd, buffer, len) != len)
92
        return -1;
93
#else
94
    HANDLE file;
95
    OVERLAPPED overlap;
96
    BOOL ret;
97
    memset(&overlap, 0, sizeof(overlap));
98

  
99
    file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
100
		      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
101

  
102
    if (file == INVALID_HANDLE_VALUE)
103
      return -1;
104

  
105
    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
106
    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
107
		      &overlap, NULL);
108
    if (ret == 0)
109
      return -1;
110
#endif
111
    return 0;
112
}
113

  
114 76

  
115 77
/*
116 78
 * Opens a file with FD_CLOEXEC set

Also available in: Unified diff