source: libwandio/iow-stdio.c @ e4eff86

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since e4eff86 was e4eff86, checked in by Shane Alcock <salcock@…>, 5 years ago

Ensure all libwandio code includes config.h

This fixes a nasty bug on 32 bit machines where the size of off_t will
change between functions, causing all sorts of havoc. The reason the size
changes is because FILE_OFFSET_BITS is defined to 64 inside config.h so
any source files that include config.h will have 64 bit off_t's whereas
any files that don't include it will end up with a 32 bit off_t (on a 32 bit
machine).

  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34
35#define _GNU_SOURCE 1
36#include "config.h"
37#include "wandio_internal.h"
38#include "wandio.h"
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <sys/uio.h>
42#include <fcntl.h>
43#include <stdlib.h>
44#include <unistd.h>
45#include <string.h>
46#include <assert.h>
47
48/* Libtrace IO module implementing a standard IO writer, i.e. no decompression
49 */
50
51enum { MIN_WRITE_SIZE = 4096 };
52
53struct stdiow_t {
54        char buffer[MIN_WRITE_SIZE];
55        int offset;
56        int fd;
57};
58
59extern iow_source_t stdio_wsource;
60
61#define DATA(iow) ((struct stdiow_t *)((iow)->data))
62
63static int safe_open(const char *filename, int flags)
64{
65        int fd = -1;
66        uid_t userid = 0;
67        gid_t groupid = 0;
68        char *sudoenv = NULL;
69
70/* Try opening with O_DIRECT */
71#ifdef O_DIRECT
72        fd = open(filename,
73                flags
74                |O_WRONLY
75                |O_CREAT
76                |O_TRUNC
77                |(force_directio_write?O_DIRECT:0),
78                0666);
79#endif
80/* If that failed (or we don't support O_DIRECT) try opening without */
81        if (fd == -1) {
82                fd = open(filename,
83                        flags
84                        |O_WRONLY
85                        |O_CREAT
86                        |O_TRUNC,
87                        0666);
88        }
89
90        if (fd == -1)
91                return fd;
92
93        /* If we're running via sudo, we want to write files owned by the
94         * original user rather than root.
95         *
96         * TODO: make this some sort of config option */
97
98        sudoenv = getenv("SUDO_UID");
99        if (sudoenv != NULL) {
100                userid = strtol(sudoenv, NULL, 10);
101        }
102        sudoenv = getenv("SUDO_GID");
103        if (sudoenv != NULL) {
104                groupid = strtol(sudoenv, NULL, 10);
105        }
106       
107        if (userid != 0 && fchown(fd, userid, groupid) == -1) {
108                perror("fchown");
109                return -1;
110        }
111
112        return fd;
113}
114
115iow_t *stdio_wopen(const char *filename,int flags)
116{
117        iow_t *iow = malloc(sizeof(iow_t));
118        iow->source = &stdio_wsource;
119        iow->data = malloc(sizeof(struct stdiow_t));
120
121        if (strcmp(filename,"-") == 0) 
122                DATA(iow)->fd = 1; /* STDOUT */
123        else {
124                DATA(iow)->fd = safe_open(filename, flags);
125        }
126
127        if (DATA(iow)->fd == -1) {
128                free(iow);
129                return NULL;
130        }
131
132        DATA(iow)->offset = 0;
133
134        return iow;
135}
136
137#define min(a,b) ((a)<(b) ? (a) : (b))
138#define max(a,b) ((a)>(b) ? (a) : (b))
139/* Round A Down to the nearest multiple of B */
140#define rounddown(a,b) ((a)-((a)%b)
141
142/* When doing directio (O_DIRECT) we need to make sure that we write multiples of MIN_WRITE_SIZE.
143 * So we accumulate data into DATA(iow)->buffer, and write it out when we get at least MIN_WRITE_SIZE.
144 *
145 * Since most writes are likely to be larger than MIN_WRITE_SIZE optimise for that case.
146 */
147static off_t stdio_wwrite(iow_t *iow, const char *buffer, off_t len)
148{
149        int towrite = len;
150        /* Round down size to the nearest multiple of MIN_WRITE_SIZE */
151
152        assert(towrite >= 0);
153
154        while (DATA(iow)->offset + towrite >= MIN_WRITE_SIZE) {
155                int err;
156                struct iovec iov[2];
157                int total = (DATA(iow)->offset+towrite);
158                int amount;
159                int count=0;
160                /* Round down to the nearest multiple */
161                total = total - (total % MIN_WRITE_SIZE);
162                amount = total;
163                if (DATA(iow)->offset) {
164                        iov[count].iov_base = DATA(iow)->buffer;
165                        iov[count].iov_len = min(DATA(iow)->offset,amount);
166                        amount -= iov[count].iov_len;
167                        ++count;
168                }
169                /* How much to write from this buffer? */
170                if (towrite) {
171                        iov[count].iov_base = (void*)buffer;    /* cast away constness, which is safe
172                                                                 * here
173                                                                 */
174                        iov[count].iov_len = amount;
175                        amount -= iov[count].iov_len;
176                        ++count;
177                }
178                assert(amount == 0);
179                err=writev(DATA(iow)->fd, iov, count);
180                if (err==-1)
181                        return -1;
182
183                /* Drop off "err" bytes from the beginning of the buffers */
184                amount = min(DATA(iow)->offset, err); /* How much we took out of the buffer */
185                memmove(DATA(iow)->buffer, 
186                        DATA(iow)->buffer+amount,
187                        DATA(iow)->offset-amount);
188                DATA(iow)->offset -= amount;
189
190                err -= amount; /* How much was written */
191
192                assert(err <= towrite);
193
194                buffer += err;
195                towrite -= err;
196
197                assert(DATA(iow)->offset == 0);
198        }
199
200        /* Make sure we're not going to overflow the buffer.  The above writev should assure
201         * that this is true
202         */
203        assert(DATA(iow)->offset + towrite <= MIN_WRITE_SIZE);
204        assert(towrite >= 0);
205
206        if (towrite > 0) {
207                /* Copy the remainder into the buffer to write next time. */
208                memcpy(DATA(iow)->buffer + DATA(iow)->offset, buffer, towrite);
209                DATA(iow)->offset += towrite;
210        }
211
212        return len;
213}
214
215static void stdio_wclose(iow_t *iow)
216{
217        long err;
218        /* Now, there might be some non multiple of the direct filesize left over, if so turn off
219         * O_DIRECT and write the final chunk.
220         */
221#ifdef O_DIRECT
222        err=fcntl(DATA(iow)->fd, F_GETFL);
223        if (err != -1 && (err & O_DIRECT) != 0) {
224                fcntl(DATA(iow)->fd,F_SETFL, err & ~O_DIRECT);
225        }
226#endif
227        err=write(DATA(iow)->fd, DATA(iow)->buffer, DATA(iow)->offset);
228        DATA(iow)->offset = 0;
229        close(DATA(iow)->fd);
230        free(iow->data);
231        free(iow);
232}
233
234iow_source_t stdio_wsource = {
235        "stdiow",
236        stdio_wwrite,
237        stdio_wclose
238};
Note: See TracBrowser for help on using the repository browser.