source: lib/iow-stdio.c @ 50c5053

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