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