source: libwandio/iow-stdio.c @ 10f924c

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

Fixed broken -fvisibility check in configure

  • Added a m4 script that does this properly rather than our hax gcc version check.
  • Tidied up CFLAGS in configure so we aren't adding the same sets of flags multiple times
  • Created a wandio_internal.h file for storing global variables that shouldn't be made public

Thanks to Alistair King, whose patch to try and make this work for
non-gcc systems brought my attention to just how broken this was :)

  • 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 "wandio_internal.h"
37#include "wandio.h"
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/uio.h>
41#include <fcntl.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include <string.h>
45#include <assert.h>
46
47/* Libtrace IO module implementing a standard IO writer, i.e. no decompression
48 */
49
50enum { MIN_WRITE_SIZE = 4096 };
51
52struct stdiow_t {
53        char buffer[MIN_WRITE_SIZE];
54        int offset;
55        int fd;
56};
57
58extern iow_source_t stdio_wsource;
59
60#define DATA(iow) ((struct stdiow_t *)((iow)->data))
61
62static int safe_open(const char *filename, int flags)
63{
64        int fd = -1;
65        uid_t userid = 0;
66        gid_t groupid = 0;
67        char *sudoenv = NULL;
68
69/* Try opening with O_DIRECT */
70#ifdef O_DIRECT
71        fd = open(filename,
72                flags
73                |O_WRONLY
74                |O_CREAT
75                |O_TRUNC
76                |(force_directio_write?O_DIRECT:0),
77                0666);
78#endif
79/* If that failed (or we don't support O_DIRECT) try opening without */
80        if (fd == -1) {
81                fd = open(filename,
82                        flags
83                        |O_WRONLY
84                        |O_CREAT
85                        |O_TRUNC,
86                        0666);
87        }
88
89        if (fd == -1)
90                return fd;
91
92        /* If we're running via sudo, we want to write files owned by the
93         * original user rather than root.
94         *
95         * TODO: make this some sort of config option */
96
97        sudoenv = getenv("SUDO_UID");
98        if (sudoenv != NULL) {
99                userid = strtol(sudoenv, NULL, 10);
100        }
101        sudoenv = getenv("SUDO_GID");
102        if (sudoenv != NULL) {
103                groupid = strtol(sudoenv, NULL, 10);
104        }
105       
106        if (userid != 0 && fchown(fd, userid, groupid) == -1) {
107                perror("fchown");
108                return -1;
109        }
110
111        return fd;
112}
113
114iow_t *stdio_wopen(const char *filename,int flags)
115{
116        iow_t *iow = malloc(sizeof(iow_t));
117        iow->source = &stdio_wsource;
118        iow->data = malloc(sizeof(struct stdiow_t));
119
120        if (strcmp(filename,"-") == 0) 
121                DATA(iow)->fd = 1; /* STDOUT */
122        else {
123                DATA(iow)->fd = safe_open(filename, flags);
124        }
125
126        if (DATA(iow)->fd == -1) {
127                free(iow);
128                return NULL;
129        }
130
131        DATA(iow)->offset = 0;
132
133        return iow;
134}
135
136#define min(a,b) ((a)<(b) ? (a) : (b))
137#define max(a,b) ((a)>(b) ? (a) : (b))
138/* Round A Down to the nearest multiple of B */
139#define rounddown(a,b) ((a)-((a)%b)
140
141/* When doing directio (O_DIRECT) we need to make sure that we write multiples of MIN_WRITE_SIZE.
142 * So we accumulate data into DATA(iow)->buffer, and write it out when we get at least MIN_WRITE_SIZE.
143 *
144 * Since most writes are likely to be larger than MIN_WRITE_SIZE optimise for that case.
145 */
146static off_t stdio_wwrite(iow_t *iow, const char *buffer, off_t len)
147{
148        int towrite = len;
149        /* Round down size to the nearest multiple of MIN_WRITE_SIZE */
150
151        assert(towrite >= 0);
152
153        while (DATA(iow)->offset + towrite >= MIN_WRITE_SIZE) {
154                int err;
155                struct iovec iov[2];
156                int total = (DATA(iow)->offset+towrite);
157                int amount;
158                int count=0;
159                /* Round down to the nearest multiple */
160                total = total - (total % MIN_WRITE_SIZE);
161                amount = total;
162                if (DATA(iow)->offset) {
163                        iov[count].iov_base = DATA(iow)->buffer;
164                        iov[count].iov_len = min(DATA(iow)->offset,amount);
165                        amount -= iov[count].iov_len;
166                        ++count;
167                }
168                /* How much to write from this buffer? */
169                if (towrite) {
170                        iov[count].iov_base = (void*)buffer;    /* cast away constness, which is safe
171                                                                 * here
172                                                                 */
173                        iov[count].iov_len = amount;
174                        amount -= iov[count].iov_len;
175                        ++count;
176                }
177                assert(amount == 0);
178                err=writev(DATA(iow)->fd, iov, count);
179                if (err==-1)
180                        return -1;
181
182                /* Drop off "err" bytes from the beginning of the buffers */
183                amount = min(DATA(iow)->offset, err); /* How much we took out of the buffer */
184                memmove(DATA(iow)->buffer, 
185                        DATA(iow)->buffer+amount,
186                        DATA(iow)->offset-amount);
187                DATA(iow)->offset -= amount;
188
189                err -= amount; /* How much was written */
190
191                assert(err <= towrite);
192
193                buffer += err;
194                towrite -= err;
195
196                assert(DATA(iow)->offset == 0);
197        }
198
199        /* Make sure we're not going to overflow the buffer.  The above writev should assure
200         * that this is true
201         */
202        assert(DATA(iow)->offset + towrite <= MIN_WRITE_SIZE);
203        assert(towrite >= 0);
204
205        if (towrite > 0) {
206                /* Copy the remainder into the buffer to write next time. */
207                memcpy(DATA(iow)->buffer + DATA(iow)->offset, buffer, towrite);
208                DATA(iow)->offset += towrite;
209        }
210
211        return len;
212}
213
214static void stdio_wclose(iow_t *iow)
215{
216        long err;
217        /* Now, there might be some non multiple of the direct filesize left over, if so turn off
218         * O_DIRECT and write the final chunk.
219         */
220#ifdef O_DIRECT
221        err=fcntl(DATA(iow)->fd, F_GETFL);
222        if (err != -1 && (err & O_DIRECT) != 0) {
223                fcntl(DATA(iow)->fd,F_SETFL, err & ~O_DIRECT);
224        }
225#endif
226        err=write(DATA(iow)->fd, DATA(iow)->buffer, DATA(iow)->offset);
227        DATA(iow)->offset = 0;
228        close(DATA(iow)->fd);
229        free(iow->data);
230        free(iow);
231}
232
233iow_source_t stdio_wsource = {
234        "stdiow",
235        stdio_wwrite,
236        stdio_wclose
237};
Note: See TracBrowser for help on using the repository browser.