~ubuntu-branches/ubuntu/natty/mysql-5.1/natty-proposed

« back to all changes in this revision

Viewing changes to mysys/my_fopen.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 08:30:45 UTC
  • mfrom: (1.4.1)
  • Revision ID: package-import@ubuntu.com-20120222083045-2rd53r4bnyx7qus4
Tags: 5.1.61-0ubuntu0.11.04.1
* SECURITY UPDATE: Update to 5.1.61 to fix multiple security issues
  (LP: #937869)
  - http://www.oracle.com/technetwork/topics/security/cpujan2012-366304.html
  - CVE-2011-2262
  - CVE-2012-0075
  - CVE-2012-0112
  - CVE-2012-0113
  - CVE-2012-0114
  - CVE-2012-0115
  - CVE-2012-0116
  - CVE-2012-0117
  - CVE-2012-0118
  - CVE-2012-0119
  - CVE-2012-0120
  - CVE-2012-0484
  - CVE-2012-0485
  - CVE-2012-0486
  - CVE-2012-0487
  - CVE-2012-0488
  - CVE-2012-0489
  - CVE-2012-0490
  - CVE-2012-0491
  - CVE-2012-0492
  - CVE-2012-0493
  - CVE-2012-0494
  - CVE-2012-0495
  - CVE-2012-0496

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000 MySQL AB
 
1
/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
18
18
#include <errno.h>
19
19
#include "mysys_err.h"
20
20
 
 
21
#if defined(__FreeBSD__)
 
22
extern int getosreldate(void);
 
23
#endif
 
24
 
21
25
static void make_ftype(char * to,int flag);
22
26
 
23
27
/*
97
101
} /* my_fopen */
98
102
 
99
103
 
100
 
        /* Close a stream */
101
 
 
 
104
#if defined(_WIN32)
 
105
 
 
106
static FILE *my_win_freopen(const char *path, const char *mode, FILE *stream)
 
107
{
 
108
  int handle_fd, fd= _fileno(stream);
 
109
  HANDLE osfh;
 
110
 
 
111
  DBUG_ASSERT(path && stream);
 
112
 
 
113
  /* Services don't have stdout/stderr on Windows, so _fileno returns -1. */
 
114
  if (fd < 0)
 
115
  {
 
116
    if (!freopen(path, mode, stream))
 
117
      return NULL;
 
118
 
 
119
    fd= _fileno(stream);
 
120
  }
 
121
 
 
122
  if ((osfh= CreateFile(path, GENERIC_READ | GENERIC_WRITE,
 
123
                        FILE_SHARE_READ | FILE_SHARE_WRITE |
 
124
                        FILE_SHARE_DELETE, NULL,
 
125
                        OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
 
126
                        NULL)) == INVALID_HANDLE_VALUE)
 
127
    return NULL;
 
128
 
 
129
  if ((handle_fd= _open_osfhandle((intptr_t)osfh,
 
130
                                  _O_APPEND | _O_TEXT)) == -1)
 
131
  {
 
132
    CloseHandle(osfh);
 
133
    return NULL;
 
134
  }
 
135
 
 
136
  if (_dup2(handle_fd, fd) < 0)
 
137
  {
 
138
    CloseHandle(osfh);
 
139
    return NULL;
 
140
  }
 
141
 
 
142
  _close(handle_fd);
 
143
 
 
144
  return stream;
 
145
}
 
146
 
 
147
#elif defined(__FreeBSD__)
 
148
 
 
149
/* No close operation hook. */
 
150
 
 
151
static int no_close(void *cookie __attribute__((unused)))
 
152
{
 
153
  return 0;
 
154
}
 
155
 
 
156
/*
 
157
  A hack around a race condition in the implementation of freopen.
 
158
 
 
159
  The race condition steams from the fact that the current fd of
 
160
  the stream is closed before its number is used to duplicate the
 
161
  new file descriptor. This defeats the desired atomicity of the
 
162
  close and duplicate of dup2().
 
163
 
 
164
  See PR number 79887 for reference:
 
165
  http://www.freebsd.org/cgi/query-pr.cgi?pr=79887
 
166
*/
 
167
 
 
168
static FILE *my_freebsd_freopen(const char *path, const char *mode, FILE *stream)
 
169
{
 
170
  int old_fd;
 
171
  FILE *result;
 
172
 
 
173
  flockfile(stream);
 
174
 
 
175
  old_fd= fileno(stream);
 
176
 
 
177
  /* Use a no operation close hook to avoid having the fd closed. */
 
178
  stream->_close= no_close;
 
179
 
 
180
  /* Relies on the implicit dup2 to close old_fd. */
 
181
  result= freopen(path, mode, stream);
 
182
 
 
183
  /* If successful, the _close hook was replaced. */
 
184
 
 
185
  if (result == NULL)
 
186
    close(old_fd);
 
187
  else
 
188
    funlockfile(result);
 
189
 
 
190
  return result;
 
191
}
 
192
 
 
193
#endif
 
194
 
 
195
 
 
196
/**
 
197
  Change the file associated with a file stream.
 
198
 
 
199
  @param path   Path to file.
 
200
  @param mode   Mode of the stream.
 
201
  @param stream File stream.
 
202
 
 
203
  @note
 
204
    This function is used to redirect stdout and stderr to a file and
 
205
    subsequently to close and reopen that file for log rotation.
 
206
 
 
207
  @retval A FILE pointer on success. Otherwise, NULL.
 
208
*/
 
209
 
 
210
FILE *my_freopen(const char *path, const char *mode, FILE *stream)
 
211
{
 
212
  FILE *result;
 
213
 
 
214
#if defined(_WIN32)
 
215
  result= my_win_freopen(path, mode, stream);
 
216
#elif defined(__FreeBSD__)
 
217
  /*
 
218
    XXX: Once the fix is ported to the stable releases, this should
 
219
         be dependent upon the specific FreeBSD versions. Check at:
 
220
         http://www.freebsd.org/cgi/query-pr.cgi?pr=79887
 
221
  */
 
222
  if (getosreldate() > 900027)
 
223
    result= freopen(path, mode, stream);
 
224
  else
 
225
    result= my_freebsd_freopen(path, mode, stream);
 
226
#else
 
227
  result= freopen(path, mode, stream);
 
228
#endif
 
229
 
 
230
  return result;
 
231
}
 
232
 
 
233
 
 
234
/* Close a stream */
102
235
int my_fclose(FILE *fd, myf MyFlags)
103
236
{
104
237
  int err,file;