summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_file.c
blob: ce971f8240179edabd7e0fb56093ab14755f0e6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


nxt_int_t
nxt_file_open(nxt_task_t *task, nxt_file_t *file, nxt_uint_t mode,
    nxt_uint_t create, nxt_file_access_t access)
{
#ifdef __CYGWIN__
    mode |= O_BINARY;
#endif

    /* O_NONBLOCK is to prevent blocking on FIFOs, special devices, etc. */
    mode |= (O_NONBLOCK | create);

    file->fd = open((char *) file->name, mode, access);

    file->error = (file->fd == -1) ? nxt_errno : 0;

#if (NXT_DEBUG)
    nxt_thread_time_update(task->thread);
#endif

    nxt_debug(task, "open(\"%FN\", 0x%uXi, 0x%uXi): %FD err:%d",
              file->name, mode, access, file->fd, file->error);

    if (file->fd != -1) {
        return NXT_OK;
    }

    if (file->log_level != 0) {
        nxt_log(task, file->log_level, "open(\"%FN\") failed %E",
                file->name, file->error);
    }

    return NXT_ERROR;
}


void
nxt_file_close(nxt_task_t *task, nxt_file_t *file)
{
    nxt_debug(task, "close(%FD)", file->fd);

    if (close(file->fd) != 0) {
        nxt_log(task, NXT_LOG_CRIT, "close(%FD, \"%FN\") failed %E",
                file->fd, file->name, nxt_errno);
    }
}


ssize_t
nxt_file_write(nxt_file_t *file, const u_char *buf, size_t size,
    nxt_off_t offset)
{
    ssize_t  n;

    nxt_thread_debug(thr);

    n = pwrite(file->fd, buf, size, offset);

    file->error = (n < 0) ? nxt_errno : 0;

    nxt_thread_time_debug_update(thr);

    nxt_log_debug(thr->log, "pwrite(%FD, %p, %uz, %O): %z",
                  file->fd, buf, size, offset, n);

    if (nxt_fast_path(n >= 0)) {
        return n;
    }

    nxt_thread_log_error(NXT_LOG_CRIT,
                         "pwrite(%FD, \"%FN\", %p, %uz, %O) failed %E",
                         file->fd, file->name, buf, size,
                         offset, file->error);

    return NXT_ERROR;
}


ssize_t
nxt_file_read(nxt_file_t *file, u_char *buf, size_t size, nxt_off_t offset)
{
    ssize_t  n;

    nxt_thread_debug(thr);

    n = pread(file->fd, buf, size, offset);

    file->error = (n <= 0) ? nxt_errno : 0;

    nxt_thread_time_debug_update(thr);

    nxt_log_debug(thr->log, "pread(%FD, %p, %uz, %O): %z",
                  file->fd, buf, size, offset, n);

    if (nxt_fast_path(n >= 0)) {
        return n;
    }

    nxt_thread_log_error(NXT_LOG_CRIT,
                         "pread(%FD, \"%FN\", %p, %uz, %O) failed %E",
                         file->fd, file->name, buf, size,
                         offset, file->error);

    return NXT_ERROR;
}


#if (NXT_HAVE_READAHEAD)

/* FreeBSD 8.0 fcntl(F_READAHEAD, size) enables read ahead up to the size. */

void
nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size)
{
    int     ret;
    u_char  buf;

    ret = fcntl(file->fd, F_READAHEAD, (int) size);

    nxt_thread_log_debug("fcntl(%FD, F_READAHEAD, %uz): %d",
                         file->fd, size, ret);

    if (nxt_fast_path(ret != -1)) {
        (void) nxt_file_read(file, &buf, 1, offset);
        return;
    }

    nxt_thread_log_error(NXT_LOG_CRIT,
                         "fcntl(%FD, \"%FN\", F_READAHEAD, %uz) failed %E",
                         file->fd, file->name, size, nxt_errno);
}

#elif (NXT_HAVE_POSIX_FADVISE)

/*
 * POSIX_FADV_SEQUENTIAL
 *   Linux doubles the default readahead window size of a backing device
 *   which is usually 128K.
 *
 *   FreeBSD does nothing.
 *
 * POSIX_FADV_WILLNEED
 *   Linux preloads synchronously up to 2M of specified file region in
 *   the kernel page cache.  Linux-specific readahead(2) syscall does
 *   the same.  Both operations are blocking despite posix_fadvise(2)
 *   claims the opposite.
 *
 *   FreeBSD does nothing.
 */

void
nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size)
{
    nxt_err_t  err;

    err = posix_fadvise(file->fd, offset, size, POSIX_FADV_WILLNEED);

    nxt_thread_log_debug("posix_fadvise(%FD, \"%FN\", %O, %uz, %d): %d",
                         file->fd, file->name, offset, size,
                         POSIX_FADV_WILLNEED, err);

    if (nxt_fast_path(err == 0)) {
        return;
    }

    nxt_thread_log_error(NXT_LOG_CRIT,
                         "posix_fadvise(%FD, \"%FN\", %O, %uz, %d) failed %E",
                         file->fd, file->name, offset, size,
                         POSIX_FADV_WILLNEED, err);
}

#elif (NXT_HAVE_RDAHEAD)

/* MacOSX fcntl(F_RDAHEAD). */

void
nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size)
{
    int     ret;
    u_char  buf;

    ret = fcntl(file->fd, F_RDAHEAD, 1);

    nxt_thread_log_debug("fcntl(%FD, F_RDAHEAD, 1): %d", file->fd, ret);

    if (nxt_fast_path(ret != -1)) {
        (void) nxt_file_read(file, &buf, 1, offset);
        return;
    }

    nxt_thread_log_error(NXT_LOG_CRIT,
                         "fcntl(%FD, \"%FN\", F_RDAHEAD, 1) failed %E",
                         file->fd, file->name, nxt_errno);
}

#else

void
nxt_file_read_ahead(nxt_file_t *file, nxt_off_t offset, size_t size)
{
    u_char  buf;

    (void) nxt_file_read(file, &buf, 1, offset);
}

#endif


nxt_int_t
nxt_file_info(nxt_file_t *file, nxt_file_info_t *fi)
{
    int  n;

    if (file->fd == NXT_FILE_INVALID) {
        n = stat((char *) file->name, fi);

        file->error = (n != 0) ? nxt_errno : 0;

        nxt_thread_log_debug("stat(\"%FN)\": %d", file->name, n);

        if (n == 0) {
            return NXT_OK;
        }

        if (file->log_level != 0) {
            nxt_thread_log_error(file->log_level, "stat(\"%FN\") failed %E",
                                 file->name, file->error);
        }

        return NXT_ERROR;

    } else {
        n = fstat(file->fd, fi);

        file->error = (n != 0) ? nxt_errno : 0;

        nxt_thread_log_debug("fstat(%FD): %d", file->fd, n);

        if (n == 0) {
            return NXT_OK;
        }

        /* Use NXT_LOG_CRIT because fstat() error on open file is strange. */

        nxt_thread_log_error(NXT_LOG_CRIT, "fstat(%FD, \"%FN\") failed %E",
                             file->fd, file->name, file->error);

        return NXT_ERROR;
    }
}


nxt_int_t
nxt_file_delete(nxt_file_name_t *name)
{
    nxt_thread_log_debug("unlink(\"%FN\")", name);

    if (nxt_fast_path(unlink((char *) name) == 0)) {
        return NXT_OK;
    }

    nxt_thread_log_alert("unlink(\"%FN\") failed %E", name, nxt_errno);

    return NXT_ERROR;
}


nxt_int_t
nxt_file_set_access(nxt_file_name_t *name, nxt_file_access_t access)
{
    if (nxt_fast_path(chmod((char *) name, access) == 0)) {
        return NXT_OK;
    }

    nxt_thread_log_alert("chmod(\"%FN\") failed %E", name, nxt_errno);

    return NXT_ERROR;
}


nxt_int_t
nxt_file_rename(nxt_file_name_t *old_name, nxt_file_name_t *new_name)
{
    int  ret;

    nxt_thread_log_debug("rename(\"%FN\", \"%FN\")", old_name, new_name);

    ret = rename((char *) old_name, (char *) new_name);
    if (nxt_fast_path(ret == 0)) {
        return NXT_OK;
    }

    nxt_thread_log_alert("rename(\"%FN\", \"%FN\") failed %E",
                         old_name, new_name, nxt_errno);

    return NXT_ERROR;
}


/*
 * ioctl(FIONBIO) sets a non-blocking mode using one syscall,
 * thereas fcntl(F_SETFL, O_NONBLOCK) needs to learn the current state
 * using fcntl(F_GETFL).
 *
 * ioctl() and fcntl() are syscalls at least in Linux 2.2, FreeBSD 2.x,
 * and Solaris 7.
 *
 * Linux 2.4 uses BKL for ioctl() and fcntl(F_SETFL).
 * Linux 2.6 does not use BKL.
 */

#if (NXT_HAVE_FIONBIO)

nxt_int_t
nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd)
{
    int  nb;

    nb = 1;

    if (nxt_fast_path(ioctl(fd, FIONBIO, &nb) != -1)) {
        return NXT_OK;
    }

    nxt_log(task, NXT_LOG_CRIT, "ioctl(%d, FIONBIO) failed %E", fd, nxt_errno);

    return NXT_ERROR;

}


nxt_int_t
nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd)
{
    int  nb;

    nb = 0;

    if (nxt_fast_path(ioctl(fd, FIONBIO, &nb) != -1)) {
        return NXT_OK;
    }

    nxt_log(task, NXT_LOG_CRIT, "ioctl(%d, !FIONBIO) failed %E", fd, nxt_errno);

    return NXT_ERROR;
}

#else /* !(NXT_HAVE_FIONBIO) */

nxt_int_t
nxt_fd_nonblocking(nxt_task_t *task, nxt_fd_t fd)
{
    int  flags;

    flags = fcntl(fd, F_GETFL);

    if (nxt_slow_path(flags == -1)) {
        nxt_log(task, NXT_LOG_CRIT, "fcntl(%d, F_GETFL) failed %E",
                fd, nxt_errno);
        return NXT_ERROR;
    }

    flags |= O_NONBLOCK;

    if (nxt_slow_path(fcntl(fd, F_SETFL, flags) == -1)) {
        nxt_log(task, NXT_LOG_CRIT, "fcntl(%d, F_SETFL, O_NONBLOCK) failed %E",
                fd, nxt_errno);
        return NXT_ERROR;
    }

    return NXT_OK;
}


nxt_int_t
nxt_fd_blocking(nxt_task_t *task, nxt_fd_t fd)
{
    int  flags;

    flags = fcntl(fd, F_GETFL);

    if (nxt_slow_path(flags == -1)) {
        nxt_log(task, NXT_LOG_CRIT, "fcntl(%d, F_GETFL) failed %E",
                fd, nxt_errno);
        return NXT_ERROR;
    }

    flags &= O_NONBLOCK;

    if (nxt_slow_path(fcntl(fd, F_SETFL, flags) == -1)) {
        nxt_log(task, NXT_LOG_CRIT, "fcntl(%d, F_SETFL, !O_NONBLOCK) failed %E",
                fd, nxt_errno);
        return NXT_ERROR;
    }

    return NXT_OK;
}

#endif /* NXT_HAVE_FIONBIO */


ssize_t
nxt_fd_write(nxt_fd_t fd, u_char *buf, size_t size)
{
    ssize_t    n;
    nxt_err_t  err;

    n = write(fd, buf, size);

    err = (n == -1) ? nxt_errno : 0;

    nxt_thread_log_debug("write(%FD, %p, %uz): %z", fd, buf, size, n);

    if (nxt_slow_path(n <= 0)) {
        nxt_thread_log_alert("write(%FD) failed %E", fd, err);
    }

    return n;
}


ssize_t
nxt_fd_read(nxt_fd_t fd, u_char *buf, size_t size)
{
    ssize_t    n;
    nxt_err_t  err;

    n = read(fd, buf, size);

    err = (n == -1) ? nxt_errno : 0;

    nxt_thread_log_debug("read(%FD, %p, %uz): %z", fd, buf, size, n);

    if (nxt_slow_path(n <= 0)) {

        if (err == NXT_EAGAIN) {
            return 0;
        }

        nxt_thread_log_alert("read(%FD) failed %E", fd, err);
    }

    return n;
}


void
nxt_fd_close(nxt_fd_t fd)
{
    nxt_thread_log_debug("close(%FD)", fd);

    if (nxt_slow_path(close(fd) != 0)) {
        nxt_thread_log_error(NXT_LOG_CRIT, "close(%FD) failed %E",
                             fd, nxt_errno);
    }
}


/*
 * nxt_file_redirect() redirects the file to the fd descriptor.
 * Then the fd descriptor is closed.
 */

nxt_int_t
nxt_file_redirect(nxt_file_t *file, nxt_fd_t fd)
{
    nxt_thread_log_debug("dup2(%FD, %FD, \"%FN\")", fd, file->fd, file->name);

    if (dup2(fd, file->fd) == -1) {
        nxt_thread_log_error(NXT_LOG_CRIT, "dup2(%FD, %FD, \"%FN\") failed %E",
                             fd, file->fd, file->name, nxt_errno);
        return NXT_ERROR;
    }

    if (close(fd) != 0) {
        nxt_thread_log_error(NXT_LOG_CRIT, "close(%FD, \"%FN\") failed %E",
                             fd, file->name, nxt_errno);
        return NXT_ERROR;
    }

    return NXT_OK;
}


/* nxt_file_stderr() redirects the stderr descriptor to the file. */

nxt_int_t
nxt_file_stderr(nxt_file_t *file)
{
    nxt_thread_log_debug("dup2(%FD, %FD, \"%FN\")",
                         file->fd, STDERR_FILENO, file->name);

    if (dup2(file->fd, STDERR_FILENO) != -1) {
        return NXT_OK;
    }

    nxt_thread_log_error(NXT_LOG_CRIT, "dup2(%FD, %FD, \"%FN\") failed %E",
                         file->fd, STDERR_FILENO, file->name, nxt_errno);

    return NXT_ERROR;
}


nxt_int_t
nxt_stderr_start(void)
{
    int  flags, fd;

    flags = fcntl(nxt_stderr, F_GETFL);

    if (flags != -1) {
        /*
         * If the stderr output of a multithreaded application is
         * redirected to a file:
         *    Linux, Solaris and MacOSX do not write atomically to the output;
         *    MacOSX besides adds zeroes to the output.
         * O_APPEND fixes this.
         */
        (void) fcntl(nxt_stderr, F_SETFL, flags | O_APPEND);

    } else {
        /*
         * The stderr descriptor is closed before application start.
         * Reserve the stderr descriptor for future use.  Errors are
         * ignored because anyway they could be written nowhere.
         */
        fd = open("/dev/null", O_WRONLY | O_APPEND);

        if (fd != -1) {
            (void) dup2(fd, nxt_stderr);

            if (fd != nxt_stderr) {
                (void) close(fd);
            }
        }
    }

    return flags;
}


nxt_int_t
nxt_pipe_create(nxt_task_t *task, nxt_fd_t *pp, nxt_bool_t nbread,
    nxt_bool_t nbwrite)
{
    if (pipe(pp) != 0) {
        nxt_log(task, NXT_LOG_CRIT, "pipe() failed %E", nxt_errno);

        return NXT_ERROR;
    }

    nxt_debug(task, "pipe(): %FD:%FD", pp[0], pp[1]);

    if (nbread) {
        if (nxt_fd_nonblocking(task, pp[0]) != NXT_OK) {
            return NXT_ERROR;
        }
    }

    if (nbwrite) {
        if (nxt_fd_nonblocking(task, pp[1]) != NXT_OK) {
            return NXT_ERROR;
        }
    }

    return NXT_OK;
}


void
nxt_pipe_close(nxt_task_t *task, nxt_fd_t *pp)
{
    nxt_debug(task, "pipe close(%FD:%FD)", pp[0], pp[1]);

    if (close(pp[0]) != 0) {
        nxt_log(task, NXT_LOG_CRIT, "pipe close(%FD) failed %E",
                pp[0], nxt_errno);
    }

    if (close(pp[1]) != 0) {
        nxt_log(task, NXT_LOG_CRIT, "pipe close(%FD) failed %E",
                pp[1], nxt_errno);
    }
}


size_t
nxt_dir_current(char *buf, size_t len)
{
    if (nxt_fast_path(getcwd(buf, len) != NULL)) {
        return nxt_strlen(buf);
    }

    nxt_thread_log_alert("getcwd(%uz) failed %E", len, nxt_errno);

    return 0;
}