]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/fd-util.h
tree-wide: sort includes in *.h
[thirdparty/systemd.git] / src / basic / fd-util.h
CommitLineData
3ffd4af2
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3#pragma once
4
5/***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22***/
23
3ffd4af2
LP
24#include <dirent.h>
25#include <stdbool.h>
71d35b6b 26#include <stdio.h>
3ffd4af2
LP
27#include <sys/socket.h>
28
29#include "macro.h"
30
23e096cc
LP
31/* Make sure we can distinguish fd 0 and NULL */
32#define FD_TO_PTR(fd) INT_TO_PTR((fd)+1)
33#define PTR_TO_FD(p) (PTR_TO_INT(p)-1)
34
3ffd4af2
LP
35int close_nointr(int fd);
36int safe_close(int fd);
37void safe_close_pair(int p[]);
38
39void close_many(const int fds[], unsigned n_fd);
40
41int fclose_nointr(FILE *f);
42FILE* safe_fclose(FILE *f);
43DIR* safe_closedir(DIR *f);
44
45static inline void closep(int *fd) {
46 safe_close(*fd);
47}
48
49static inline void close_pairp(int (*p)[2]) {
50 safe_close_pair(*p);
51}
52
53static inline void fclosep(FILE **f) {
54 safe_fclose(*f);
55}
56
57DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, pclose);
58DEFINE_TRIVIAL_CLEANUP_FUNC(DIR*, closedir);
59
60#define _cleanup_close_ _cleanup_(closep)
61#define _cleanup_fclose_ _cleanup_(fclosep)
62#define _cleanup_pclose_ _cleanup_(pclosep)
63#define _cleanup_closedir_ _cleanup_(closedirp)
64#define _cleanup_close_pair_ _cleanup_(close_pairp)
65
66int fd_nonblock(int fd, bool nonblock);
67int fd_cloexec(int fd, bool cloexec);
68
69int close_all_fds(const int except[], unsigned n_except);
70
71int same_fd(int a, int b);
72
73void cmsg_close_all(struct msghdr *mh);
4fee3975
LP
74
75bool fdname_is_valid(const char *s);