Fix interactive reset command.
[adu.git] / fd.c
1 /*
2  * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file fd.c Helper functions for file descriptor handling. */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <sys/mman.h>
12
13 #include "adu.h"
14 #include "error.h"
15
16 /**
17  * Wrapper for the write system call.
18  *
19  * \param fd The file descriptor to write to.
20  * \param buf The buffer to write.
21  * \param size The length of \a buf in bytes.
22  *
23  * This function writes out the given buffer and retries if an interrupt
24  * occurred during the write.
25  *
26  * \return Standard.
27  *
28  * \sa write(2).
29  */
30 ssize_t __write(int fd, const void *buf, size_t size)
31 {
32         ssize_t ret;
33
34         for (;;) {
35                 ret = write(fd, buf, size);
36                 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
37                         continue;
38                 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
39         }
40 }
41
42 /**
43  * Write the whole buffer to a file descriptor.
44  *
45  * \param fd The file descriptor to write to.
46  * \param buf The buffer to write.
47  * \param size The length of \a buf in bytes.
48  *
49  * This function writes the given buffer and continues on short writes and
50  * when interrupted by a signal.
51  *
52  * \return Standard.
53  */
54 static ssize_t write_all(int fd, const void *buf, size_t size)
55 {
56 //      DEBUG_LOG("writing %zu bytes\n", size);
57         const char *b = buf;
58         while (size) {
59                 ssize_t ret = __write(fd, b, size);
60 //              DEBUG_LOG("ret: %zd\n", ret);
61                 if (ret < 0)
62                         return ret;
63                 b += ret;
64                 size -= ret;
65         }
66         return 1;
67 }
68
69 /**
70  * Wrapper for the open(2) system call.
71  *
72  * \param path The filename.
73  * \param flags The usual open(2) flags.
74  * \param mode Specifies the permissions to use.
75  *
76  * The mode parameter must be specified when O_CREAT is in the flags, and is
77  * ignored otherwise.
78  *
79  * \return The file descriptor on success, negative on errors.
80  *
81  * \sa open(2).
82  */
83 static int __open(const char *path, int flags, mode_t mode)
84 {
85         int ret = open(path, flags, mode);
86
87         if (ret >= 0)
88                 return ret;
89         return -ERRNO_TO_ERROR(errno);
90 }
91
92 /**
93  * Open a file, write the given buffer and close the file.
94  *
95  * \param filename Full path to the file to open.
96  * \param buf The buffer to write to the file.
97  * \param size The size of \a buf.
98  *
99  * \return Standard.
100  */
101 int adu_write_file(const char *filename, const void *buf, size_t size)
102 {
103         int ret, fd;
104
105         ret = __open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
106         if (ret < 0)
107                 return ret;
108         fd = ret;
109         ret = write_all(fd, buf, size);
110         if (ret < 0)
111                 goto out;
112         ret = 1;
113 out:
114         close(fd);
115         return ret;
116 }
117
118 /**
119  * Wrapper for chdir(2).
120  *
121  * \param path The specified directory.
122  *
123  * \return Standard.
124  */
125 int __chdir(const char *path)
126 {
127         int ret = chdir(path);
128
129         if (ret >= 0)
130                 return 1;
131         return -ERRNO_TO_ERROR(errno);
132 }
133
134 /**
135  * Save the cwd and open a given directory.
136  *
137  * \param dirname Path to the directory to open.
138  * \param dir Result pointer.
139  * \param cwd File descriptor of the current working directory.
140  *
141  * \return Standard.
142  *
143  * Opening the current directory (".") and calling fchdir() to return is
144  * usually faster and more reliable than saving cwd in some buffer and calling
145  * chdir() afterwards.
146  *
147  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
148  * stored in \a cwd. If the function returns success, and \a cwd is not \p
149  * NULL, the caller must close this file descriptor (probably after calling
150  * fchdir(*cwd)).
151  *
152  * On errors, the function undos everything, so the caller needs neither close
153  * any files, nor change back to the original working directory.
154  *
155  * \sa getcwd(3).
156  *
157  */
158 int adu_opendir(const char *dirname, DIR **dir, int *cwd)
159 {
160         int ret;
161
162         if (cwd) {
163                 ret = __open(".", O_RDONLY, 0);
164                 if (ret < 0)
165                         return ret;
166                 *cwd = ret;
167         }
168         ret = __chdir(dirname);
169         if (ret < 0)
170                 goto close_cwd;
171         *dir = opendir(".");
172         if (*dir)
173                 return 1;
174         ret = -ERRNO_TO_ERROR(errno);
175 /* Ignore return value of fchdir() and close(). We're busted anyway. */
176         if (cwd)
177                 fchdir(*cwd);
178 close_cwd:
179         if (cwd)
180                 close(*cwd);
181         return ret;
182 }
183
184 /**
185  * A wrapper for fchdir().
186  *
187  * \param fd An open file descriptor.
188  *
189  * \return Standard.
190  */
191 int adu_fchdir(int fd)
192 {
193         if (fchdir(fd) < 0)
194                 return -ERRNO_TO_ERROR(errno);
195         return 1;
196 }
197
198 /**
199  * Open a file and map it into memory.
200  *
201  * \param path Name of the regular file to map.
202  * \param open_mode Either \p O_RDONLY or \p O_RDWR.
203  * \param map On success, the mapping is returned here.
204  * \param size size of the mapping.
205  * \param fd_ptr The file descriptor of the mapping.
206  *
207  * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
208  * open call is closed after mmap().  Otherwise the file is kept open and the
209  * file descriptor is returned in \a fd_ptr.
210  *
211  * \return Standard.
212  *
213  * \sa mmap(2).
214  */
215 int mmap_full_file(const char *path, int open_mode, void **map,
216                 size_t *size, int *fd_ptr)
217 {
218         int fd, ret, mmap_prot, mmap_flags;
219         struct stat file_status;
220
221         if (open_mode == O_RDONLY) {
222                 mmap_prot = PROT_READ;
223                 mmap_flags = MAP_PRIVATE;
224         } else {
225                 mmap_prot = PROT_READ | PROT_WRITE;
226                 mmap_flags = MAP_SHARED;
227         }
228         ret = __open(path, open_mode, 0);
229         if (ret < 0)
230                 return ret;
231         fd = ret;
232         if (fstat(fd, &file_status) < 0) {
233                 ret = -ERRNO_TO_ERROR(errno);
234                 goto out;
235         }
236         *size = file_status.st_size;
237         ret = -E_EMPTY;
238         DEBUG_LOG("%s: size %zu\n", path, *size);
239         if (!*size)
240                 goto out;
241         *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
242         if (*map == MAP_FAILED) {
243                 *map = NULL;
244                 ret = -E_MMAP;
245                 goto out;
246         }
247         ret = 1;
248 out:
249         if (ret < 0 || !fd_ptr)
250                 close(fd);
251         else
252                 *fd_ptr = fd;
253         return ret;
254 }
255
256 /**
257  * A wrapper for munmap(2).
258  *
259  * \param start The start address of the memory mapping.
260  * \param length The size of the mapping.
261  *
262  * \return Positive on success, \p -E_MUNMAP on errors.
263  *
264  * \sa munmap(2), mmap_full_file().
265  */
266 int adu_munmap(void *start, size_t length)
267 {
268         int err;
269         if (munmap(start, length) >= 0)
270                 return 1;
271         err = errno;
272         ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
273                 strerror(err));
274         return -ERRNO_TO_ERROR(err);
275 }