kernel32: Only change the Unix current after exec_process to make sure it's inherited...
[wine] / dlls / kernel32 / oldconfig.c
1 /*
2  * Support for converting from old configuration format
3  *
4  * Copyright 2005 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES
21  *   This file should be removed after a suitable transition period.
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #include <fcntl.h>
35 #ifdef HAVE_DIRENT_H
36 # include <dirent.h>
37 #endif
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_LINUX_HDREG_H
42 # include <linux/hdreg.h>
43 #endif
44
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winternl.h"
50 #include "ntddscsi.h"
51 #include "wine/library.h"
52 #include "wine/unicode.h"
53 #include "wine/debug.h"
54 #include "kernel_private.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(reg);
57
58 static NTSTATUS create_key( HANDLE root, const char *name, HANDLE *key, DWORD *disp )
59 {
60     OBJECT_ATTRIBUTES attr;
61     UNICODE_STRING nameW;
62     NTSTATUS status;
63
64     attr.Length = sizeof(attr);
65     attr.RootDirectory = root;
66     attr.ObjectName = &nameW;
67     attr.Attributes = 0;
68     attr.SecurityDescriptor = NULL;
69     attr.SecurityQualityOfService = NULL;
70
71     if (!RtlCreateUnicodeStringFromAsciiz( &nameW, name )) return STATUS_NO_MEMORY;
72     status = NtCreateKey( key, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, disp );
73     if (status) ERR("Cannot create %s registry key\n", name );
74     RtlFreeUnicodeString( &nameW );
75     return status;
76 }
77
78 /******************************************************************
79  *              create_scsi_entry
80  *
81  * Initializes registry to contain scsi info about the cdrom in NT.
82  * All devices (even not real scsi ones) have this info in NT.
83  * NOTE: programs usually read these registry entries after sending the
84  *       IOCTL_SCSI_GET_ADDRESS ioctl to the cdrom
85  */
86 static void create_scsi_entry( PSCSI_ADDRESS scsi_addr, LPCSTR lpDriver, UINT uDriveType,
87     LPSTR lpDriveName, LPSTR lpUnixDeviceName )
88 {
89     static UCHAR uCdromNumber = 0;
90     static UCHAR uTapeNumber = 0;
91
92     UNICODE_STRING nameW;
93     WCHAR dataW[50];
94     DWORD sizeW;
95     char buffer[40];
96     DWORD value;
97     const char *data;
98     HANDLE scsiKey;
99     HANDLE portKey;
100     HANDLE busKey;
101     HANDLE targetKey;
102     HANDLE lunKey;
103     DWORD disp;
104
105     if (create_key( 0, "Machine\\HARDWARE\\DEVICEMAP", &scsiKey, &disp )) return;
106     NtClose( scsiKey );
107
108     /* Ensure there is Scsi key */
109     if (create_key( 0, "Machine\\HARDWARE\\DEVICEMAP\\Scsi", &scsiKey, &disp )) return;
110
111     snprintf(buffer,sizeof(buffer),"Scsi Port %d",scsi_addr->PortNumber);
112     if (create_key( scsiKey, buffer, &portKey, &disp )) return;
113
114     RtlCreateUnicodeStringFromAsciiz( &nameW, "Driver" );
115     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriver, strlen(lpDriver)+1);
116     NtSetValueKey( portKey, &nameW, 0, REG_SZ, dataW, sizeW );
117     RtlFreeUnicodeString( &nameW );
118     value = 10;
119     RtlCreateUnicodeStringFromAsciiz( &nameW, "FirstBusTimeScanInMs" );
120     NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
121     RtlFreeUnicodeString( &nameW );
122
123     value = 0;
124     if (strcmp(lpDriver, "atapi") == 0)
125     {
126 #ifdef HDIO_GET_DMA
127         int fd, dma;
128         
129         fd = open(lpUnixDeviceName, O_RDONLY|O_NONBLOCK);
130         if (fd != -1)
131         {
132             if (ioctl(fd, HDIO_GET_DMA, &dma) != -1) value = dma;
133             close(fd);
134         }
135 #endif
136         RtlCreateUnicodeStringFromAsciiz( &nameW, "DMAEnabled" );
137         NtSetValueKey( portKey,&nameW, 0, REG_DWORD, &value, sizeof(DWORD) );
138         RtlFreeUnicodeString( &nameW );
139     }
140
141     snprintf(buffer, sizeof(buffer),"Scsi Bus %d", scsi_addr->PathId);
142     if (create_key( portKey, buffer, &busKey, &disp )) return;
143
144     /* FIXME: get real controller Id for SCSI */
145     if (create_key( busKey, buffer, &targetKey, &disp )) return;
146     NtClose( targetKey );
147
148     snprintf(buffer, sizeof(buffer),"Target Id %d", scsi_addr->TargetId);
149     if (create_key( busKey, buffer, &targetKey, &disp )) return;
150
151     snprintf(buffer, sizeof(buffer),"Logical Unit Id %d", scsi_addr->Lun);
152     if (create_key( targetKey, buffer, &lunKey, &disp )) return;
153
154     switch (uDriveType)
155     {
156         case DRIVE_NO_ROOT_DIR:
157         default:
158             data = "OtherPeripheral"; break;
159         case DRIVE_FIXED:
160             data = "DiskPeripheral"; break;
161         case DRIVE_REMOVABLE:
162             data = "TapePeripheral";
163             snprintf(buffer, sizeof(buffer), "Tape%d", uTapeNumber++);
164             break;
165         case DRIVE_CDROM:
166             data = "CdRomPeripheral";
167             snprintf(buffer, sizeof(buffer), "Cdrom%d", uCdromNumber++);
168             break;
169     }
170     RtlCreateUnicodeStringFromAsciiz( &nameW, "Type" );
171     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, data, strlen(data)+1);
172     NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
173     RtlFreeUnicodeString( &nameW );
174
175     RtlCreateUnicodeStringFromAsciiz( &nameW, "Identifier" );
176     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpDriveName, strlen(lpDriveName)+1);
177     NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
178     RtlFreeUnicodeString( &nameW );
179
180     if (uDriveType == DRIVE_CDROM || uDriveType == DRIVE_REMOVABLE)
181     {
182         RtlCreateUnicodeStringFromAsciiz( &nameW, "DeviceName" );
183         RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, buffer, strlen(buffer)+1);
184         NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
185         RtlFreeUnicodeString( &nameW );
186     }
187
188     RtlCreateUnicodeStringFromAsciiz( &nameW, "UnixDeviceName" );
189     RtlMultiByteToUnicodeN( dataW, sizeof(dataW), &sizeW, lpUnixDeviceName, strlen(lpUnixDeviceName)+1);
190     NtSetValueKey( lunKey, &nameW, 0, REG_SZ, dataW, sizeW );
191     RtlFreeUnicodeString( &nameW );
192
193     NtClose( lunKey );
194     NtClose( targetKey );
195     NtClose( busKey );
196     NtClose( portKey );
197     NtClose( scsiKey );
198 }
199
200 struct LinuxProcScsiDevice
201 {
202     int host;
203     int channel;
204     int target;
205     int lun;
206     char vendor[9];
207     char model[17];
208     char rev[5];
209     char type[33];
210     int ansirev;
211 };
212
213 /*
214  * we need to declare white spaces explicitly via %*1[ ],
215  * as there are very dainbread CD-ROM devices out there
216  * which have their manufacturer name blanked out via spaces,
217  * which confuses fscanf's parsing (skips all blank spaces)
218  */
219 static int SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
220 {
221     int result;
222
223     result = fscanf( procfile,
224         "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
225         &dev->host,
226         &dev->channel,
227         &dev->target,
228         &dev->lun );
229     if( result == EOF )
230     {
231         /* "end of entries" return, so no TRACE() here */
232         return EOF;
233     }
234     if( result != 4 )
235     {
236         ERR("bus id line scan count error (fscanf returns %d, expected 4)\n", result);
237         return 0;
238     }
239     result = fscanf( procfile,
240         "  Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
241         dev->vendor,
242         dev->model,
243         dev->rev );
244     if( result != 3 )
245     {
246         ERR("model line scan count error (fscanf returns %d, expected 3)\n", result);
247         return 0;
248     }
249
250     result = fscanf( procfile,
251         "  Type:%*3[ ]%32c%*1[ ]ANSI SCSI%*1[ ]revision:%*1[ ]%x\n",
252         dev->type,
253         &dev->ansirev );
254     if( result != 2 )
255     {
256         ERR("SCSI type line scan count error (fscanf returns %d, expected 2)\n", result);
257         return 0;
258     }
259     /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
260     dev->vendor[8] = 0;
261     dev->model[16] = 0;
262     dev->rev[4] = 0;
263     dev->type[32] = 0;
264
265     return 1;
266 }
267
268
269 /* create the hardware registry branch */
270 static void create_hardware_branch(void)
271 {
272     /* The following mostly will work on Linux, but should not cause
273      * problems on other systems. */
274     static const char procname_ide_media[] = "/proc/ide/%s/media";
275     static const char procname_ide_model[] = "/proc/ide/%s/model";
276     static const char procname_scsi[] = "/proc/scsi/scsi";
277     DIR *idedir;
278     struct dirent *dent = NULL;
279     FILE *procfile = NULL;
280     char cStr[40], cDevModel[40], cUnixDeviceName[40], read1[10] = "\0", read2[10] = "\0";
281     SCSI_ADDRESS scsi_addr;
282     UINT nType;
283     struct LinuxProcScsiDevice dev;
284     int result = 0, nSgNumber = 0;
285     UCHAR uFirstSCSIPort = 0;
286
287     /* Enumerate all ide devices first */
288     idedir = opendir("/proc/ide");
289     if (idedir)
290     {
291         while ((dent = readdir(idedir)))
292         {
293             if (strncmp(dent->d_name, "hd", 2) == 0)
294             {
295                 sprintf(cStr, procname_ide_media, dent->d_name);
296                 procfile = fopen(cStr, "r");
297                 if (!procfile)
298                 {
299                     ERR("Could not open %s\n", cStr);
300                     continue;
301                 } else {
302                     fgets(cStr, sizeof(cStr), procfile);
303                     fclose(procfile);
304                     nType = DRIVE_UNKNOWN;
305                     if (strncasecmp(cStr, "disk", 4)  == 0) nType = DRIVE_FIXED;
306                     if (strncasecmp(cStr, "cdrom", 5) == 0) nType = DRIVE_CDROM;
307
308                     if (nType == DRIVE_UNKNOWN) continue;
309                 }
310
311                 sprintf(cStr, procname_ide_model, dent->d_name);
312                 procfile = fopen(cStr, "r");
313                 if (!procfile)
314                 {
315                     ERR("Could not open %s\n", cStr);
316                     switch (nType)
317                     {
318                     case DRIVE_FIXED: strcpy(cDevModel, "Wine harddisk"); break;
319                     case DRIVE_CDROM: strcpy(cDevModel, "Wine CDROM"); break;
320                     }
321                 } else {
322                     fgets(cDevModel, sizeof(cDevModel), procfile);
323                     fclose(procfile);
324                     cDevModel[strlen(cDevModel) - 1] = 0;
325                 }
326
327                 sprintf(cUnixDeviceName, "/dev/%s", dent->d_name);
328                 scsi_addr.PortNumber = (dent->d_name[2] - 'a') / 2;
329                 scsi_addr.PathId = 0;
330                 scsi_addr.TargetId = (dent->d_name[2] - 'a') % 2;
331                 scsi_addr.Lun = 0;
332                 if (scsi_addr.PortNumber + 1 > uFirstSCSIPort)
333                     uFirstSCSIPort = scsi_addr.PortNumber + 1;
334
335                 create_scsi_entry(&scsi_addr, "atapi", nType, cDevModel, cUnixDeviceName);
336             }
337         }
338         closedir(idedir);
339     }
340
341     /* Now goes SCSI */
342     procfile = fopen(procname_scsi, "r");
343     if (!procfile)
344     {
345         TRACE("Could not open %s\n", procname_scsi);
346         return;
347     }
348     fgets(cStr, 40, procfile);
349     sscanf(cStr, "Attached %9s %9s", read1, read2);
350
351     if (strcmp(read1, "devices:") != 0)
352     {
353         WARN("Incorrect %s format\n", procname_scsi);
354         fclose( procfile );
355         return;
356     }
357     if (strcmp(read2, "none") == 0)
358     {
359         TRACE("No SCSI devices found in %s.\n", procname_scsi);
360         fclose( procfile );
361         return;
362     }
363
364     /* Read info for one device */
365     while ((result = SCSI_getprocentry(procfile, &dev)) > 0)
366     {
367         scsi_addr.PortNumber = dev.host;
368         scsi_addr.PathId = dev.channel;
369         scsi_addr.TargetId = dev.target;
370         scsi_addr.Lun = dev.lun;
371
372         scsi_addr.PortNumber += uFirstSCSIPort;
373         if (strncmp(dev.type, "Direct-Access", 13) == 0) nType = DRIVE_FIXED;
374         else if (strncmp(dev.type, "Sequential-Access", 17) == 0) nType = DRIVE_REMOVABLE;
375         else if (strncmp(dev.type, "CD-ROM", 6) == 0) nType = DRIVE_CDROM;
376         else if (strncmp(dev.type, "Processor", 9) == 0) nType = DRIVE_NO_ROOT_DIR;
377         else continue;
378
379         strcpy(cDevModel, dev.vendor);
380         strcat(cDevModel, dev.model);
381         strcat(cDevModel, dev.rev);
382         sprintf(cUnixDeviceName, "/dev/sg%d", nSgNumber++);
383         /* FIXME: get real driver name */
384         create_scsi_entry(&scsi_addr, "WINE SCSI", nType, cDevModel, cUnixDeviceName);
385     }
386     if( result != EOF )
387         WARN("Incorrect %s format\n", procname_scsi);
388     fclose( procfile );
389 }
390
391
392 /***********************************************************************
393  *              convert_old_config
394  */
395 void convert_old_config(void)
396 {
397     HANDLE key;
398     DWORD disp;
399
400     /* create some hardware keys (FIXME: should not be done here) */
401     if (create_key( 0, "Machine\\HARDWARE", &key, &disp )) return;
402     NtClose( key );
403     if (disp != REG_OPENED_EXISTING_KEY) create_hardware_branch();
404 }