Revision bc4a957c

b/os-posix.c
361 361

  
362 362
    return qemu_pipe(fds);
363 363
}
364

  
365
int qemu_create_pidfile(const char *filename)
366
{
367
    char buffer[128];
368
    int len;
369
    int fd;
370

  
371
    fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
372
    if (fd == -1) {
373
        return -1;
374
    }
375
    if (lockf(fd, F_TLOCK, 0) == -1) {
376
        return -1;
377
    }
378
    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
379
    if (write(fd, buffer, len) != len) {
380
        return -1;
381
    }
382

  
383
    return 0;
384
}
b/os-win32.c
240 240
{
241 241
    fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
242 242
}
243

  
244
int qemu_create_pidfile(const char *filename)
245
{
246
    char buffer[128];
247
    int len;
248
    HANDLE file;
249
    OVERLAPPED overlap;
250
    BOOL ret;
251
    memset(&overlap, 0, sizeof(overlap));
252

  
253
    file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
254
		      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
255

  
256
    if (file == INVALID_HANDLE_VALUE) {
257
        return -1;
258
    }
259
    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
260
    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
261
		      &overlap, NULL);
262
    if (ret == 0) {
263
        return -1;
264
    }
265
    return 0;
266
}
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