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