~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to hw/xfree86/parser/write.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $XFree86: xc/programs/Xserver/hw/xfree86/parser/write.c,v 1.18 2003/08/24 17:37:08 dawes Exp $ */
 
2
/* 
 
3
 * 
 
4
 * Copyright (c) 1997  Metro Link Incorporated
 
5
 * 
 
6
 * Permission is hereby granted, free of charge, to any person obtaining a
 
7
 * copy of this software and associated documentation files (the "Software"), 
 
8
 * to deal in the Software without restriction, including without limitation
 
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
10
 * and/or sell copies of the Software, and to permit persons to whom the
 
11
 * Software is furnished to do so, subject to the following conditions:
 
12
 * 
 
13
 * The above copyright notice and this permission notice shall be included in
 
14
 * all copies or substantial portions of the Software.
 
15
 * 
 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
19
 * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
20
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 
21
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
22
 * SOFTWARE.
 
23
 * 
 
24
 * Except as contained in this notice, the name of the Metro Link shall not be
 
25
 * used in advertising or otherwise to promote the sale, use or other dealings
 
26
 * in this Software without prior written authorization from Metro Link.
 
27
 * 
 
28
 */
 
29
/*
 
30
 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
 
31
 *
 
32
 * Permission is hereby granted, free of charge, to any person obtaining a
 
33
 * copy of this software and associated documentation files (the "Software"),
 
34
 * to deal in the Software without restriction, including without limitation
 
35
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
36
 * and/or sell copies of the Software, and to permit persons to whom the
 
37
 * Software is furnished to do so, subject to the following conditions:
 
38
 *
 
39
 * The above copyright notice and this permission notice shall be included in
 
40
 * all copies or substantial portions of the Software.
 
41
 *
 
42
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
43
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
44
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
45
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
46
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 
47
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
48
 * OTHER DEALINGS IN THE SOFTWARE.
 
49
 *
 
50
 * Except as contained in this notice, the name of the copyright holder(s)
 
51
 * and author(s) shall not be used in advertising or otherwise to promote
 
52
 * the sale, use or other dealings in this Software without prior written
 
53
 * authorization from the copyright holder(s) and author(s).
 
54
 */
 
55
 
 
56
 
 
57
/* View/edit this file with tab stops set to 4 */
 
58
 
 
59
#ifdef HAVE_XORG_CONFIG_H
 
60
#include <xorg-config.h>
 
61
#endif
 
62
 
 
63
#include "xf86Parser.h"
 
64
#include "xf86tokens.h"
 
65
#include "Configint.h"
 
66
 
 
67
#include <unistd.h>
 
68
#include <sys/types.h>
 
69
#include <sys/wait.h>
 
70
#include <signal.h>
 
71
#include <errno.h>
 
72
 
 
73
#if ((defined(sun) && !defined(SVR4)) || defined(macII)) && !defined(__GLIBC__)
 
74
#ifndef strerror
 
75
extern char *sys_errlist[];
 
76
extern int sys_nerr;
 
77
#define strerror(n) \
 
78
        (((n) >= 0 && (n) < sys_nerr) ? sys_errlist[n] : "unknown error")
 
79
#endif
 
80
#endif
 
81
 
 
82
#if defined(SVR4) || defined(__linux__) || defined(CSRG_BASED)
 
83
#define HAS_SAVED_IDS_AND_SETEUID
 
84
#endif
 
85
#if defined(__UNIXOS2__) || defined(WIN32)
 
86
#define HAS_NO_UIDS
 
87
#endif
 
88
 
 
89
#ifdef HAS_NO_UIDS
 
90
#define doWriteConfigFile xf86writeConfigFile
 
91
#define Local /**/
 
92
#else
 
93
#define Local static
 
94
#endif
 
95
 
 
96
Local int
 
97
doWriteConfigFile (const char *filename, XF86ConfigPtr cptr)
 
98
{
 
99
        FILE *cf;
 
100
 
 
101
        if ((cf = fopen (filename, "w")) == NULL)
 
102
        {
 
103
                return 0;
 
104
        }
 
105
 
 
106
        if (cptr->conf_comment)
 
107
                fprintf (cf, "%s\n", cptr->conf_comment);
 
108
 
 
109
        xf86printLayoutSection (cf, cptr->conf_layout_lst);
 
110
 
 
111
        fprintf (cf, "Section \"Files\"\n");
 
112
        xf86printFileSection (cf, cptr->conf_files);
 
113
        fprintf (cf, "EndSection\n\n");
 
114
 
 
115
        fprintf (cf, "Section \"Module\"\n");
 
116
        xf86printModuleSection (cf, cptr->conf_modules);
 
117
        fprintf (cf, "EndSection\n\n");
 
118
 
 
119
        xf86printVendorSection (cf, cptr->conf_vendor_lst);
 
120
 
 
121
        xf86printServerFlagsSection (cf, cptr->conf_flags);
 
122
 
 
123
        xf86printInputSection (cf, cptr->conf_input_lst);
 
124
 
 
125
        xf86printVideoAdaptorSection (cf, cptr->conf_videoadaptor_lst);
 
126
 
 
127
        xf86printModesSection (cf, cptr->conf_modes_lst);
 
128
 
 
129
        xf86printMonitorSection (cf, cptr->conf_monitor_lst);
 
130
 
 
131
        xf86printDeviceSection (cf, cptr->conf_device_lst);
 
132
 
 
133
        xf86printScreenSection (cf, cptr->conf_screen_lst);
 
134
 
 
135
        xf86printDRISection (cf, cptr->conf_dri);
 
136
 
 
137
        xf86printExtensionsSection (cf, cptr->conf_extensions);
 
138
 
 
139
        fclose(cf);
 
140
        return 1;
 
141
}
 
142
 
 
143
#ifndef HAS_NO_UIDS
 
144
 
 
145
int
 
146
xf86writeConfigFile (const char *filename, XF86ConfigPtr cptr)
 
147
{
 
148
        int ret;
 
149
 
 
150
#if !defined(HAS_SAVED_IDS_AND_SETEUID)
 
151
        int pid, p;
 
152
        int status;
 
153
        void (*csig)(int);
 
154
#else
 
155
        int ruid, euid;
 
156
#endif
 
157
 
 
158
        if (getuid() != geteuid())
 
159
        {
 
160
 
 
161
#if !defined(HAS_SAVED_IDS_AND_SETEUID)
 
162
                /* Need to fork to change ruid without loosing euid */
 
163
#ifdef SIGCHLD
 
164
                csig = signal(SIGCHLD, SIG_DFL);
 
165
#endif
 
166
                switch ((pid = fork()))
 
167
                {
 
168
                case -1:
 
169
                        ErrorF("xf86writeConfigFile(): fork failed (%s)\n",
 
170
                                        strerror(errno));
 
171
                        return 0;
 
172
                case 0: /* child */
 
173
                        setuid(getuid());
 
174
                        ret = doWriteConfigFile(filename, cptr);
 
175
                        exit(ret);
 
176
                        break;
 
177
                default: /* parent */
 
178
                        do
 
179
                        {
 
180
                                p = waitpid(pid, &status, 0);
 
181
                        } while (p == -1 && errno == EINTR);
 
182
                }
 
183
#ifdef SIGCHLD
 
184
                signal(SIGCHLD, csig);
 
185
#endif
 
186
                if (p != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0)
 
187
                        return 1;       /* success */
 
188
                else
 
189
                        return 0;
 
190
 
 
191
#else /* HAS_SAVED_IDS_AND_SETEUID */
 
192
 
 
193
                ruid = getuid();
 
194
                euid = geteuid();
 
195
 
 
196
                if (seteuid(ruid) == -1)
 
197
                {
 
198
                        ErrorF("xf86writeConfigFile(): seteuid(%d) failed (%s)\n",
 
199
                                        ruid, strerror(errno));
 
200
                        return 0;
 
201
                }
 
202
                ret = doWriteConfigFile(filename, cptr);
 
203
 
 
204
                if (seteuid(euid) == -1)
 
205
                {
 
206
                        ErrorF("xf86writeConfigFile(): seteuid(%d) failed (%s)\n",
 
207
                                        euid, strerror(errno));
 
208
                }
 
209
                return ret;
 
210
 
 
211
#endif /* HAS_SAVED_IDS_AND_SETEUID */
 
212
 
 
213
        }
 
214
        else
 
215
        {
 
216
                return doWriteConfigFile(filename, cptr);
 
217
        }
 
218
}
 
219
 
 
220
#endif /* !HAS_NO_UIDS */