* bus/bus.c, bus/bus.h, bus/main.c, bus/test.c, dbus/dbus-sysdeps-unix.c, dbus/dbus...
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-win.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
3  * 
4  * Copyright (C) 2002, 2003  Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  * Copyright (C) 2005 Novell, Inc.
7  * Copyright (C) 2006 Ralf Habacker <ralf.habacker@freenet.de>
8  * Copyright (C) 2006 Peter Kümmel  <syntheticpp@gmx.net>
9  * Copyright (C) 2006 Christian Ehrlicher <ch.ehrlicher@gmx.de>
10  *
11  * Licensed under the Academic Free License version 2.1
12  * 
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  *
27  */
28 #undef open
29
30 #define STRSAFE_NO_DEPRECATE
31
32 #ifndef DBUS_WINCE
33 #define _WIN32_WINNT 0x0500
34 #endif
35
36 #include "dbus-internals.h"
37 #include "dbus-sysdeps.h"
38 #include "dbus-threads.h"
39 #include "dbus-protocol.h"
40 #include "dbus-string.h"
41 #include "dbus-sysdeps-win.h"
42 #include "dbus-protocol.h"
43 #include "dbus-hash.h"
44 #include "dbus-sockets-win.h"
45 #include "dbus-userdb.h"
46 #include "dbus-list.h"
47
48 #include <windows.h>
49 #include <fcntl.h>
50
51 #include <process.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54
55 #ifndef O_BINARY
56 #define O_BINARY 0
57 #endif
58
59 #ifndef HAVE_SOCKLEN_T
60 #define socklen_t int
61 #endif
62
63 _DBUS_DEFINE_GLOBAL_LOCK (win_fds);
64 _DBUS_DEFINE_GLOBAL_LOCK (sid_atom_cache);
65
66
67 static
68 void 
69 _dbus_lock_sockets()
70 {
71         _dbus_assert (win_fds!=0); 
72         _DBUS_LOCK   (win_fds);
73 }
74
75 static
76 void 
77 _dbus_unlock_sockets()
78 {
79         _dbus_assert (win_fds!=0); 
80         _DBUS_UNLOCK (win_fds);
81 }
82
83 #ifdef _DBUS_WIN_USE_RANDOMIZER
84 static int  win_encap_randomizer;
85 #endif
86 static DBusHashTable *sid_atom_cache = NULL;
87
88
89 static DBusString dbusdir;
90 static int working_dir_init = 0;
91
92 int _dbus_init_working_dir(char *s)
93 {
94   /* change working directory to one level above 
95      of dbus-daemon executable path.  
96      This allows the usage of relative path in 
97      config files or command line parameters */
98   DBusString daemon_path,bin_path;
99
100   if (!_dbus_string_init (&daemon_path))
101     return FALSE;
102   
103   if (!_dbus_string_init (&bin_path))
104     return FALSE;
105
106   if (!_dbus_string_init (&dbusdir))
107     return FALSE;
108   
109   _dbus_string_append(&daemon_path,s);
110   _dbus_string_get_dirname(&daemon_path,&bin_path);
111   _dbus_string_get_dirname(&bin_path,&dbusdir);
112   chdir(_dbus_string_get_const_data(&dbusdir));
113   _dbus_verbose ("Change working path to %s\n",_dbus_string_get_const_data (&dbusdir));
114   working_dir_init = 1;
115   return TRUE;
116 }
117
118 DBusString *_dbus_get_working_dir(void)
119 {
120   if (!working_dir_init) 
121     return NULL;
122         
123   _dbus_verbose ("retrieving working path %s\n",_dbus_string_get_const_data (&dbusdir));
124   return &dbusdir;
125 }
126
127 /**
128  * File interface
129  *
130  */
131 dbus_bool_t
132 _dbus_open_file (DBusFile   *file,
133                  const char *filename,
134                  int         oflag,
135                  int         pmode)
136 {
137   if (pmode!=-1)
138     file->FDATA = _open (filename, oflag, pmode);
139   else
140     file->FDATA = _open (filename, oflag);
141   if (file->FDATA >= 0)
142     return TRUE;
143   else
144     {
145       file->FDATA = -1;
146       return FALSE;
147     }
148 }
149
150 dbus_bool_t
151 _dbus_close_file (DBusFile  *file,
152                   DBusError *error)
153 {
154   const int fd = file->FDATA;
155
156   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
157
158   _dbus_assert (fd >= 0);
159
160   if (_close (fd) == -1)
161     {
162       dbus_set_error (error, _dbus_error_from_errno (errno),
163                       "Could not close fd %d: %s", fd,
164                       _dbus_strerror (errno));
165       return FALSE;
166     }
167
168   file->FDATA = -1;
169   _dbus_verbose ("closed C file descriptor %d:\n",fd);
170
171   return TRUE;
172 }
173
174 int
175 _dbus_read_file(DBusFile   *file,
176                 DBusString *buffer,
177                 int         count)
178 {
179   const int fd = file->FDATA;
180   int bytes_read;
181   int start;
182   char *data;
183   _dbus_assert (count >= 0);
184
185   start = _dbus_string_get_length (buffer);
186
187   if (!_dbus_string_lengthen (buffer, count))
188     {
189       errno = ENOMEM;
190       return -1;
191     }
192
193   data = _dbus_string_get_data_len (buffer, start, count);
194
195   _dbus_assert (fd >= 0);
196
197   _dbus_verbose ("read: count=%d fd=%d\n", count, fd);
198   bytes_read = read (fd, data, count);
199
200   if (bytes_read == -1)
201     _dbus_verbose ("read: failed: %s\n", _dbus_strerror (errno));
202   else
203     _dbus_verbose ("read: = %d\n", bytes_read);
204
205   if (bytes_read < 0)
206     {
207       /* put length back (note that this doesn't actually realloc anything) */
208       _dbus_string_set_length (buffer, start);
209       return -1;
210     }
211   else
212     {
213       /* put length back (doesn't actually realloc) */
214       _dbus_string_set_length (buffer, start + bytes_read);
215
216 #if 0
217
218       if (bytes_read > 0)
219         _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
220 #endif
221
222       return bytes_read;
223     }
224 }
225
226 int
227 _dbus_write_file (DBusFile         *file,
228                   const DBusString *buffer,
229                   int               start,
230                   int               len)
231 {
232   const int fd = file->FDATA;
233   const char *data;
234   int bytes_written;
235
236   data = _dbus_string_get_const_data_len (buffer, start, len);
237
238   _dbus_assert (fd >= 0);
239
240   _dbus_verbose ("write: len=%d fd=%d\n", len, fd);
241   bytes_written = write (fd, data, len);
242
243   if (bytes_written == -1)
244     _dbus_verbose ("write: failed: %s\n", _dbus_strerror (errno));
245   else
246     _dbus_verbose ("write: = %d\n", bytes_written);
247
248 #if 0
249
250   if (bytes_written > 0)
251     _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
252 #endif
253
254   return bytes_written;
255 }
256
257 dbus_bool_t
258 _dbus_is_valid_file (DBusFile* file)
259 {
260   return file->FDATA >= 0;
261 }
262
263 dbus_bool_t _dbus_fstat (DBusFile    *file,
264                          struct stat *sb)
265 {
266   return fstat(file->FDATA, sb) >= 0;
267 }
268
269 /**
270  * init a pipe instance.
271  *
272  * @param fd the file descriptor to init from 
273  * @returns a DBusPipe instance
274  */
275 DBusPipe _dbus_pipe_init(int         fd)
276 {
277         DBusPipe pipe;
278         pipe.fd = fd;
279         return pipe;
280 }
281
282 /**
283  * write data to a pipe.
284  *
285  * @param pipe the pipe instance
286  * @param buffer the buffer to write data from
287  * @param start the first byte in the buffer to write
288  * @param len the number of bytes to try to write
289  * @returns the number of bytes written or -1 on error
290  */
291 int
292 _dbus_pipe_write (DBusPipe          pipe,
293                   const DBusString *buffer,
294                   int               start,
295                   int               len)
296 {
297         DBusFile file;
298         file.FDATA = pipe.fd;
299         return _dbus_write_file(&file, buffer, start, len);
300 }
301
302 /**
303  * read data from a pipe.
304  *
305  * @param pipe the pipe instance
306  * @param buffer the buffer to read data in
307  * @param count the number of bytes to try to read
308  * @returns the number of bytes read or -1 on error
309  */
310 int
311 _dbus_pipe_read(DBusPipe    pipe,
312                 DBusString *buffer,
313                 int         count)
314 {
315         DBusFile file;
316         file.FDATA = pipe.fd;
317         return _dbus_read_file(&file, buffer, count);
318 }
319
320 /**
321  * close a pipe.
322  *
323  * @param pipe the pipe instance
324  * @param error return location for an error
325  * @returns #FALSE if error is set
326  */
327  int
328 _dbus_pipe_close(DBusPipe    pipe,
329                  DBusError    *error)
330 {
331         DBusFile file;
332         file.FDATA = pipe.fd;
333         return _dbus_close_file(&file, error);
334 }
335
336 /**
337  * check if a pipe is valid, which means is constructed
338  * by a valid file descriptor
339  *
340  * @param pipe the pipe instance
341  * @returns #FALSE if pipe is not valid
342  */
343 dbus_bool_t _dbus_pipe_is_valid(DBusPipe pipe)
344 {
345         return pipe.fd >= 0;
346 }
347
348 /**
349  * check if a pipe is a special pipe, which means using 
350  * a non default file descriptor (>2)
351  *
352  * @param pipe the pipe instance
353  * @returns #FALSE if pipe is not a special pipe
354  */
355 dbus_bool_t _dbus_pipe_is_special(DBusPipe pipe)
356 {
357         return pipe.fd > 2;
358 }
359
360 #undef FDATA
361
362 /**
363  * Socket interface
364  *
365  */
366
367 static DBusSocket *win_fds = NULL;
368 static int win_n_fds = 0; // is this the size? rename to win_fds_size? #
369
370 #if 0
371 #define TO_HANDLE(n)   ((n)^win32_encap_randomizer)
372 #define FROM_HANDLE(n) ((n)^win32_encap_randomizer)
373 #else
374 #define TO_HANDLE(n)   ((n)+0x10000000)
375 #define FROM_HANDLE(n) ((n)-0x10000000)
376 #define IS_HANDLE(n)   ((n)&0x10000000)
377 #endif
378
379
380 static
381 void
382 _dbus_win_deallocate_fd (int fd)
383 {
384   _DBUS_LOCK (win_fds);
385   win_fds[FROM_HANDLE (fd)].is_used = 0;
386   _DBUS_UNLOCK (win_fds);
387 }
388
389 static
390 int
391 _dbus_win_allocate_fd (void)
392 {
393   int i;
394
395   _DBUS_LOCK (win_fds);
396
397   if (win_fds == NULL)
398     {
399 #ifdef _DBUS_WIN_USE_RANDOMIZER
400       DBusString random;
401 #endif
402
403       win_n_fds = 16;
404       /* Use malloc to avoid memory leak failure in dbus-test */
405       win_fds = malloc (win_n_fds * sizeof (*win_fds));
406
407       _dbus_assert (win_fds != NULL);
408
409       for (i = 0; i < win_n_fds; i++)
410         win_fds[i].is_used = 0;
411
412 #ifdef _DBUS_WIN_USE_RANDOMIZER
413
414       _dbus_string_init (&random);
415       _dbus_generate_random_bytes (&random, sizeof (int));
416       memmove (&win_encap_randomizer, _dbus_string_get_const_data (&random), sizeof (int));
417       win_encap_randomizer &= 0xFF;
418       _dbus_string_free (&random);
419 #endif
420
421     }
422
423   for (i = 0; i < win_n_fds && win_fds[i].is_used != 0; i++)
424     ;
425
426   if (i == win_n_fds)
427     {
428       int oldn = win_n_fds;
429       int j;
430
431       win_n_fds += 16;
432       win_fds = realloc (win_fds, win_n_fds * sizeof (*win_fds));
433
434       _dbus_assert (win_fds != NULL);
435
436       for (j = oldn; j < win_n_fds; j++)
437         win_fds[i].is_used = 0;
438     }
439
440   memset(&win_fds[i], 0, sizeof(win_fds[i]));
441
442   win_fds[i].is_used = 1;
443   win_fds[i].fd = -1;
444   win_fds[i].port_file_fd = -1;
445   win_fds[i].close_on_exec = FALSE;
446   win_fds[i].non_blocking = FALSE;
447
448   _DBUS_UNLOCK (win_fds);
449
450   return i;
451 }
452
453 static
454 int
455 _dbus_create_handle_from_socket (int s)
456 {
457   int i;
458   int handle = -1;
459
460   // check: parameter must be a valid value
461   _dbus_assert(s != -1);
462   _dbus_assert(!IS_HANDLE(s));
463
464   // get index of a new position in the map
465   i = _dbus_win_allocate_fd ();
466
467   // fill new posiiton in the map: value->index
468   win_fds[i].fd = s;
469   win_fds[i].is_used = 1;
470
471   // create handle from the index: index->handle
472   handle = TO_HANDLE (i);
473
474   _dbus_verbose ("_dbus_create_handle_from_value, value: %d, handle: %d\n", s, handle);
475
476   return handle;
477 }
478
479 int
480 _dbus_socket_to_handle (DBusSocket *s)
481 {
482   int i;
483   int handle = -1;
484
485   // check: parameter must be a valid socket
486   _dbus_assert(s != NULL);
487   _dbus_assert(s->fd != -1);
488   _dbus_assert(!IS_HANDLE(s->fd));
489
490   _DBUS_LOCK (win_fds);
491
492   // at the first call there is no win_fds
493   // will be constructed  _dbus_create_handle_from_socket
494   // because handle = -1
495   if (win_fds != NULL)
496     {
497       // search for the value in the map
498       // find the index of the value: value->index
499       for (i = 0; i < win_n_fds; i++)
500         if (win_fds[i].is_used == 1 && win_fds[i].fd == s->fd)
501           {
502             // create handle from the index: index->handle
503             handle = TO_HANDLE (i);
504             break;
505           }
506     }
507   _DBUS_UNLOCK (win_fds);
508
509
510   if (handle == -1)
511     {
512       handle = _dbus_create_handle_from_socket(s->fd);
513     }
514
515   _dbus_assert(handle != -1);
516
517   return handle;
518 }
519
520 static
521 void 
522 _dbus_handle_to_socket_unlocked (int          handle,
523                                  DBusSocket **ptr)
524 {
525   int i;
526
527   // check: parameter must be a valid handle
528   _dbus_assert(handle != -1);
529   _dbus_assert(IS_HANDLE(handle));
530   _dbus_assert(ptr != NULL);
531
532   // map from handle to index: handle->index
533   i = FROM_HANDLE (handle);
534
535   _dbus_assert (win_fds != NULL);
536   _dbus_assert (i >= 0 && i < win_n_fds);
537
538   // check for if fd is valid
539   _dbus_assert (win_fds[i].is_used == 1);
540
541   // get socket from index: index->socket
542   *ptr = &win_fds[i];
543
544   _dbus_verbose ("_dbus_socket_to_handle_unlocked: socket=%d, handle=%d, index=%d\n", (*ptr)->fd, handle, i);
545 }
546
547 void 
548 _dbus_handle_to_socket (int          handle,
549                         DBusSocket **ptr)
550 {
551   _dbus_lock_sockets();
552   _dbus_handle_to_socket_unlocked (handle, ptr);
553   _dbus_unlock_sockets();
554 }
555
556 #undef TO_HANDLE
557 #undef IS_HANDLE
558 #undef FROM_HANDLE
559 #define FROM_HANDLE(n) 1==DBUS_WIN_DONT_USE__FROM_HANDLE__DIRECTLY
560 #define win_fds 1==DBUS_WIN_DONT_USE_win_fds_DIRECTLY
561
562
563
564
565 /**
566  * Thin wrapper around the read() system call that appends
567  * the data it reads to the DBusString buffer. It appends
568  * up to the given count, and returns the same value
569  * and same errno as read(). The only exception is that
570  * _dbus_read() handles EINTR for you. _dbus_read() can
571  * return ENOMEM, even though regular UNIX read doesn't.
572  *
573  * @param fd the file descriptor to read from
574  * @param buffer the buffer to append data to
575  * @param count the amount of data to read
576  * @returns the number of bytes read or -1
577  */
578 int
579 _dbus_read_socket (int               handle,
580                    DBusString       *buffer,
581                    int               count)
582 {
583   DBusSocket *s;
584   int bytes_read;
585   int start;
586   char *data;
587
588   _dbus_assert (count >= 0);
589
590   start = _dbus_string_get_length (buffer);
591
592   if (!_dbus_string_lengthen (buffer, count))
593     {
594       errno = ENOMEM;
595       return -1;
596     }
597
598   data = _dbus_string_get_data_len (buffer, start, count);
599
600   _dbus_handle_to_socket(handle, &s);
601
602   if(s->is_used)
603     {
604       _dbus_verbose ("recv: count=%d socket=%d\n", count, s->fd);
605       bytes_read = recv (s->fd, data, count, 0);
606       if (bytes_read == SOCKET_ERROR)
607         {
608           DBUS_SOCKET_SET_ERRNO();
609           _dbus_verbose ("recv: failed: %s\n", _dbus_strerror (errno));
610           bytes_read = -1;
611         }
612       else
613         _dbus_verbose ("recv: = %d\n", bytes_read);
614     }
615   else
616     {
617       _dbus_assert_not_reached ("no valid socket");
618     }
619
620   if (bytes_read < 0)
621     {
622       /* put length back (note that this doesn't actually realloc anything) */
623       _dbus_string_set_length (buffer, start);
624       return -1;
625     }
626   else
627     {
628       /* put length back (doesn't actually realloc) */
629       _dbus_string_set_length (buffer, start + bytes_read);
630
631 #if 0
632
633       if (bytes_read > 0)
634         _dbus_verbose_bytes_of_string (buffer, start, bytes_read);
635 #endif
636
637       return bytes_read;
638     }
639 }
640
641 /**
642  * Thin wrapper around the write() system call that writes a part of a
643  * DBusString and handles EINTR for you.
644  * 
645  * @param fd the file descriptor to write
646  * @param buffer the buffer to write data from
647  * @param start the first byte in the buffer to write
648  * @param len the number of bytes to try to write
649  * @returns the number of bytes written or -1 on error
650  */
651 int
652 _dbus_write_socket (int               handle,
653                     const DBusString *buffer,
654                     int               start,
655                     int               len)
656 {
657   DBusSocket *s;
658   int is_used;
659   const char *data;
660   int bytes_written;
661
662   data = _dbus_string_get_const_data_len (buffer, start, len);
663
664   _dbus_handle_to_socket(handle, &s);
665
666   if (s->is_used)
667     {
668       _dbus_verbose ("send: len=%d socket=%d\n", len, s->fd);
669       bytes_written = send (s->fd, data, len, 0);
670       if (bytes_written == SOCKET_ERROR)
671         {
672           DBUS_SOCKET_SET_ERRNO();
673           _dbus_verbose ("send: failed: %s\n", _dbus_strerror (errno));
674           bytes_written = -1;
675         }
676       else
677         _dbus_verbose ("send: = %d\n", bytes_written);
678     }
679   else
680     {
681       _dbus_assert_not_reached ("unhandled fd type");
682     }
683
684 #if 0
685   if (bytes_written > 0)
686     _dbus_verbose_bytes_of_string (buffer, start, bytes_written);
687 #endif
688
689   return bytes_written;
690 }
691
692
693 /**
694  * Closes a file descriptor.
695  *
696  * @param fd the file descriptor
697  * @param error error object
698  * @returns #FALSE if error set
699  */
700 dbus_bool_t
701 _dbus_close_socket (int        handle,
702                     DBusError *error)
703 {
704   DBusSocket *s;
705
706   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
707
708   _dbus_lock_sockets();
709
710   _dbus_handle_to_socket_unlocked (handle, &s);
711
712
713   if (s->is_used)
714     {
715       if (s->port_file_fd >= 0)
716         {
717           _chsize (s->port_file_fd, 0);
718           close (s->port_file_fd);
719           s->port_file_fd = -1;
720           unlink (_dbus_string_get_const_data (&s->port_file));
721           free ((char *) _dbus_string_get_const_data (&s->port_file));
722         }
723
724       if (closesocket (s->fd) == SOCKET_ERROR)
725         {
726           DBUS_SOCKET_SET_ERRNO ();
727           dbus_set_error (error, _dbus_error_from_errno (errno),
728               "Could not close socket: socket=%d, handle=%d, %s",
729                           s->fd, handle, _dbus_strerror (errno));
730           _dbus_unlock_sockets();
731           return FALSE;
732         }
733       _dbus_verbose ("_dbus_close_socket: socket=%d, handle=%d\n",
734                      s->fd, handle);
735     }
736   else
737     {
738       _dbus_assert_not_reached ("unhandled fd type");
739     }
740
741   _dbus_unlock_sockets();
742
743   _dbus_win_deallocate_fd (handle);
744
745   return TRUE;
746
747 }
748
749 /**
750  * Sets the file descriptor to be close
751  * on exec. Should be called for all file
752  * descriptors in D-Bus code.
753  *
754  * @param fd the file descriptor
755  */
756 void
757 _dbus_fd_set_close_on_exec (int handle)
758 {
759   DBusSocket *s;
760   if (handle < 0)
761     return;
762
763   _dbus_lock_sockets();
764
765   _dbus_handle_to_socket_unlocked (handle, &s);
766   s->close_on_exec = TRUE;
767
768   _dbus_unlock_sockets();
769 }
770
771 /**
772  * Sets a file descriptor to be nonblocking.
773  *
774  * @param fd the file descriptor.
775  * @param error address of error location.
776  * @returns #TRUE on success.
777  */
778 dbus_bool_t
779 _dbus_set_fd_nonblocking (int             handle,
780                           DBusError      *error)
781 {
782   DBusSocket *s;
783   u_long one = 1;
784
785   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
786
787   _dbus_lock_sockets();
788
789   _dbus_handle_to_socket_unlocked(handle, &s);
790
791   if (s->is_used)
792     {
793       if (ioctlsocket (s->fd, FIONBIO, &one) == SOCKET_ERROR)
794         {
795           dbus_set_error (error, _dbus_error_from_errno (WSAGetLastError ()),
796                           "Failed to set socket %d:%d to nonblocking: %s", s->fd,
797                           _dbus_strerror (WSAGetLastError ()));
798           _dbus_unlock_sockets();
799           return FALSE;
800         }
801     }
802   else
803     {
804       _dbus_assert_not_reached ("unhandled fd type");
805     }
806
807   _dbus_unlock_sockets();
808
809   return TRUE;
810 }
811
812
813 /**
814  * Like _dbus_write() but will use writev() if possible
815  * to write both buffers in sequence. The return value
816  * is the number of bytes written in the first buffer,
817  * plus the number written in the second. If the first
818  * buffer is written successfully and an error occurs
819  * writing the second, the number of bytes in the first
820  * is returned (i.e. the error is ignored), on systems that
821  * don't have writev. Handles EINTR for you.
822  * The second buffer may be #NULL.
823  *
824  * @param fd the file descriptor
825  * @param buffer1 first buffer
826  * @param start1 first byte to write in first buffer
827  * @param len1 number of bytes to write from first buffer
828  * @param buffer2 second buffer, or #NULL
829  * @param start2 first byte to write in second buffer
830  * @param len2 number of bytes to write in second buffer
831  * @returns total bytes written from both buffers, or -1 on error
832  */
833 int
834 _dbus_write_socket_two (int               handle,
835                         const DBusString *buffer1,
836                         int               start1,
837                         int               len1,
838                         const DBusString *buffer2,
839                         int               start2,
840                         int               len2)
841 {
842   DBusSocket *s;
843   WSABUF vectors[2];
844   const char *data1;
845   const char *data2;
846   int rc;
847   DWORD bytes_written;
848   int ret1;
849
850   _dbus_assert (buffer1 != NULL);
851   _dbus_assert (start1 >= 0);
852   _dbus_assert (start2 >= 0);
853   _dbus_assert (len1 >= 0);
854   _dbus_assert (len2 >= 0);
855
856   _dbus_handle_to_socket(handle, &s);
857
858   data1 = _dbus_string_get_const_data_len (buffer1, start1, len1);
859
860   if (buffer2 != NULL)
861     data2 = _dbus_string_get_const_data_len (buffer2, start2, len2);
862   else
863     {
864       data2 = NULL;
865       start2 = 0;
866       len2 = 0;
867     }
868
869   if (s->is_used)
870     {
871       vectors[0].buf = (char*) data1;
872       vectors[0].len = len1;
873       vectors[1].buf = (char*) data2;
874       vectors[1].len = len2;
875
876       _dbus_verbose ("WSASend: len1+2=%d+%d socket=%d\n", len1, len2, s->fd);
877       rc = WSASend (s->fd, vectors, data2 ? 2 : 1, &bytes_written,
878                     0, NULL, NULL);
879       if (rc < 0)
880         {
881           DBUS_SOCKET_SET_ERRNO ();
882           _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror (errno));
883           bytes_written = -1;
884         }
885       else
886         _dbus_verbose ("WSASend: = %ld\n", bytes_written);
887       return bytes_written;
888     }
889   else
890     {
891       _dbus_assert_not_reached ("unhandled fd type");
892     }
893   return 0;
894 }
895
896 /**
897  * @def _DBUS_MAX_SUN_PATH_LENGTH
898  *
899  * Maximum length of the path to a UNIX domain socket,
900  * sockaddr_un::sun_path member. POSIX requires that all systems
901  * support at least 100 bytes here, including the nul termination.
902  * We use 99 for the max value to allow for the nul.
903  *
904  * We could probably also do sizeof (addr.sun_path)
905  * but this way we are the same on all platforms
906  * which is probably a good idea.
907  */
908
909 /**
910  * Creates a socket and connects it to the UNIX domain socket at the
911  * given path.  The connection fd is returned, and is set up as
912  * nonblocking.
913  * 
914  * On Windows there are no UNIX domain sockets. Instead, connects to a
915  * localhost-bound TCP socket, whose port number is stored in a file
916  * at the given path.
917  * 
918  * Uses abstract sockets instead of filesystem-linked sockets if
919  * requested (it's possible only on Linux; see "man 7 unix" on Linux).
920  * On non-Linux abstract socket usage always fails.
921  *
922  * @param path the path to UNIX domain socket
923  * @param abstract #TRUE to use abstract namespace
924  * @param error return location for error code
925  * @returns connection file descriptor or -1 on error
926  */
927 int
928 _dbus_connect_unix_socket (const char     *path,
929                            dbus_bool_t     abstract,
930                            DBusError      *error)
931 {
932 #ifdef DBUS_WINCE
933         return -1;
934 #else
935   int fd, n, port;
936   char buf[7];
937
938   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
939
940   _dbus_verbose ("connecting to pseudo-unix socket at %s\n",
941                  path);
942
943   if (abstract)
944     {
945       dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
946                       "Implementation does not support abstract socket namespace\n");
947       return -1;
948     }
949
950   fd = _sopen (path, O_RDONLY, SH_DENYNO);
951
952   if (fd == -1)
953     {
954       dbus_set_error (error, _dbus_error_from_errno (errno),
955                       "Failed to open file %s: %s",
956                       path, _dbus_strerror (errno));
957       return -1;
958     }
959
960   n = read (fd, buf, sizeof (buf) - 1);
961   close (fd);
962
963   if (n == 0)
964     {
965       dbus_set_error (error, DBUS_ERROR_FAILED,
966                       "Failed to read port number from file %s",
967                       path);
968       return -1;
969     }
970
971   buf[n] = '\0';
972   port = atoi (buf);
973
974   if (port <= 0 || port > 0xFFFF)
975     {
976       dbus_set_error (error, DBUS_ERROR_FAILED,
977                       "Invalid port numer in file %s",
978                       path);
979       return -1;
980     }
981
982   return _dbus_connect_tcp_socket (NULL, port, error);
983 #endif //DBUS_WINCE
984
985 }
986
987 /**
988  * Creates a socket and binds it to the given path,
989  * then listens on the socket. The socket is
990  * set to be nonblocking.
991  *
992  * Uses abstract sockets instead of filesystem-linked
993  * sockets if requested (it's possible only on Linux;
994  * see "man 7 unix" on Linux).
995  * On non-Linux abstract socket usage always fails.
996  *
997  * @param path the socket name
998  * @param abstract #TRUE to use abstract namespace
999  * @param error return location for errors
1000  * @returns the listening file descriptor or -1 on error
1001  */
1002 int
1003 _dbus_listen_unix_socket (const char     *path,
1004                           dbus_bool_t     abstract,
1005                           DBusError      *error)
1006 {
1007 #ifdef DBUS_WINCE
1008         return -1;
1009 #else
1010   DBusSocket *s;
1011   int listen_handle;
1012   struct sockaddr sa;
1013   int addr_len;
1014   int filefd;
1015   int n, l;
1016   DBusString portstr;
1017
1018   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1019
1020   _dbus_verbose ("listening on pseudo-unix socket at %s\n",
1021                  path);
1022
1023   if (abstract)
1024     {
1025       dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
1026                       "Implementation does not support abstract socket namespace\n");
1027       return -1;
1028     }
1029
1030   listen_handle = _dbus_listen_tcp_socket (NULL, 0, error);
1031
1032   if (listen_handle == -1)
1033     return -1;
1034
1035   _dbus_handle_to_socket(listen_handle, &s);
1036
1037   addr_len = sizeof (sa);
1038   if (getsockname (s->fd, &sa, &addr_len) == SOCKET_ERROR)
1039     {
1040       DBUS_SOCKET_SET_ERRNO ();
1041       dbus_set_error (error, _dbus_error_from_errno (errno),
1042                       "getsockname failed: %s",
1043                       _dbus_strerror (errno));
1044       _dbus_close_socket (listen_handle, NULL);
1045       return -1;
1046     }
1047
1048   _dbus_assert (((struct sockaddr_in*) &sa)->sin_family == AF_INET);
1049
1050   filefd = _sopen (path, O_CREAT|O_WRONLY|_O_SHORT_LIVED, SH_DENYWR, 0666);
1051
1052   if (filefd == -1)
1053     {
1054       dbus_set_error (error, _dbus_error_from_errno (errno),
1055                       "Failed to create pseudo-unix socket port number file %s: %s",
1056                       path, _dbus_strerror (errno));
1057       _dbus_close_socket (listen_handle, NULL);
1058       return -1;
1059     }
1060
1061   _dbus_lock_sockets();
1062   _dbus_handle_to_socket_unlocked(listen_handle, &s);
1063   s->port_file_fd = filefd;
1064   _dbus_unlock_sockets();
1065
1066   /* Use strdup() to avoid memory leak in dbus-test */
1067   path = strdup (path);
1068   if (!path)
1069     {
1070       _DBUS_SET_OOM (error);
1071       _dbus_close_socket (listen_handle, NULL);
1072       return -1;
1073     }
1074
1075   _dbus_string_init_const (&s->port_file, path);
1076
1077   if (!_dbus_string_init (&portstr))
1078     {
1079       _DBUS_SET_OOM (error);
1080       _dbus_close_socket (listen_handle, NULL);
1081       return -1;
1082     }
1083
1084   if (!_dbus_string_append_int (&portstr, ntohs (((struct sockaddr_in*) &sa)->sin_port)))
1085     {
1086       _DBUS_SET_OOM (error);
1087       _dbus_close_socket (listen_handle, NULL);
1088       return -1;
1089     }
1090
1091   l = _dbus_string_get_length (&portstr);
1092   n = write (filefd, _dbus_string_get_const_data (&portstr), l);
1093   _dbus_string_free (&portstr);
1094
1095   if (n == -1)
1096     {
1097       dbus_set_error (error, _dbus_error_from_errno (errno),
1098                       "Failed to write port number to file %s: %s",
1099                       path, _dbus_strerror (errno));
1100       _dbus_close_socket (listen_handle, NULL);
1101       return -1;
1102     }
1103   else if (n < l)
1104     {
1105       dbus_set_error (error, _dbus_error_from_errno (errno),
1106                       "Failed to write port number to file %s",
1107                       path);
1108       _dbus_close_socket (listen_handle, NULL);
1109       return -1;
1110     }
1111
1112   return listen_handle;
1113 #endif //DBUS_WINCE
1114 }
1115
1116 #if 0
1117
1118 /**
1119  * Opens the client side of a Windows named pipe. The connection D-BUS
1120  * file descriptor index is returned. It is set up as nonblocking.
1121  * 
1122  * @param path the path to named pipe socket
1123  * @param error return location for error code
1124  * @returns connection D-BUS file descriptor or -1 on error
1125  */
1126 int
1127 _dbus_connect_named_pipe (const char     *path,
1128                           DBusError      *error)
1129 {
1130   _dbus_assert_not_reached ("not implemented");
1131 }
1132
1133 #endif
1134
1135
1136 dbus_bool_t
1137 _dbus_account_to_win_sid (const wchar_t  *waccount,
1138                           void          **ppsid,
1139                           DBusError      *error)
1140 {
1141   dbus_bool_t retval = FALSE;
1142   DWORD sid_length, wdomain_length;
1143   SID_NAME_USE use;
1144   wchar_t *wdomain;
1145
1146   *ppsid = NULL;
1147
1148   sid_length = 0;
1149   wdomain_length = 0;
1150   if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1151                            NULL, &wdomain_length, &use)
1152       && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1153     {
1154       _dbus_win_set_error_from_win_error (error, GetLastError ());
1155       return FALSE;
1156     }
1157
1158   *ppsid = dbus_malloc (sid_length);
1159   if (!*ppsid)
1160     {
1161       _DBUS_SET_OOM (error);
1162       return FALSE;
1163     }
1164
1165   wdomain = dbus_new (wchar_t, wdomain_length);
1166   if (!wdomain)
1167     {
1168       _DBUS_SET_OOM (error);
1169       goto out1;
1170     }
1171
1172   if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1173                            wdomain, &wdomain_length, &use))
1174     {
1175       _dbus_win_set_error_from_win_error (error, GetLastError ());
1176       goto out2;
1177     }
1178
1179   if (!IsValidSid ((PSID) *ppsid))
1180     {
1181       dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1182       goto out2;
1183     }
1184
1185   retval = TRUE;
1186
1187 out2:
1188   dbus_free (wdomain);
1189 out1:
1190   if (!retval)
1191     {
1192       dbus_free (*ppsid);
1193       *ppsid = NULL;
1194     }
1195
1196   return retval;
1197 }
1198
1199
1200 dbus_bool_t
1201 fill_win_user_info_name_and_groups (wchar_t       *wname,
1202                                     wchar_t       *wdomain,
1203                                     DBusUserInfo *info,
1204                                     DBusError    *error)
1205 {
1206 #ifdef DBUS_WINCE
1207         return TRUE;
1208 #else
1209   dbus_bool_t retval = FALSE;
1210   char *name, *domain;
1211   LPLOCALGROUP_USERS_INFO_0 local_groups = NULL;
1212   LPGROUP_USERS_INFO_0 global_groups = NULL;
1213   DWORD nread, ntotal;
1214
1215   name = _dbus_win_utf16_to_utf8 (wname, error);
1216   if (!name)
1217     return FALSE;
1218
1219   domain = _dbus_win_utf16_to_utf8 (wdomain, error);
1220   if (!domain)
1221     goto out0;
1222
1223   info->username = dbus_malloc (strlen (domain) + 1 + strlen (name) + 1);
1224   if (!info->username)
1225     {
1226       _DBUS_SET_OOM (error);
1227       goto out1;
1228     }
1229
1230   strcpy (info->username, domain);
1231   strcat (info->username, "\\");
1232   strcat (info->username, name);
1233
1234   info->n_group_ids = 0;
1235   if (NetUserGetLocalGroups (NULL, wname, 0, LG_INCLUDE_INDIRECT,
1236                              (LPBYTE *) &local_groups, MAX_PREFERRED_LENGTH,
1237                              &nread, &ntotal) == NERR_Success)
1238     {
1239       DWORD i;
1240       int n;
1241
1242       info->group_ids = dbus_new (dbus_gid_t, nread);
1243       if (!info->group_ids)
1244         {
1245           _DBUS_SET_OOM (error);
1246           goto out3;
1247         }
1248
1249       for (i = n = 0; i < nread; i++)
1250         {
1251           PSID group_sid;
1252           if (_dbus_account_to_win_sid (local_groups[i].lgrui0_name,
1253                                         &group_sid, error))
1254             {
1255               info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1256               dbus_free (group_sid);
1257             }
1258         }
1259       info->n_group_ids = n;
1260     }
1261
1262   if (NetUserGetGroups (NULL, wname, 0,
1263                         (LPBYTE *) &global_groups, MAX_PREFERRED_LENGTH,
1264                         &nread, &ntotal) == NERR_Success)
1265     {
1266       DWORD i;
1267       int n = info->n_group_ids;
1268
1269       info->group_ids = dbus_realloc (info->group_ids, (n + nread) * sizeof (dbus_gid_t));
1270       if (!info->group_ids)
1271         {
1272           _DBUS_SET_OOM (error);
1273           goto out4;
1274         }
1275
1276       for (i = 0; i < nread; i++)
1277         {
1278           PSID group_sid;
1279           if (_dbus_account_to_win_sid (global_groups[i].grui0_name,
1280                                         &group_sid, error))
1281             {
1282               info->group_ids[n++] = _dbus_win_sid_to_uid_t (group_sid);
1283               dbus_free (group_sid);
1284             }
1285         }
1286       info->n_group_ids = n;
1287     }
1288
1289   if (info->n_group_ids > 0)
1290     {
1291       /* FIXME: find out actual primary group */
1292       info->primary_gid = info->group_ids[0];
1293     }
1294   else
1295     {
1296       info->group_ids = dbus_new (dbus_gid_t, 1);
1297       info->n_group_ids = 1;
1298       info->group_ids[0] = DBUS_GID_UNSET;
1299       info->primary_gid = DBUS_GID_UNSET;
1300     }
1301
1302   retval = TRUE;
1303
1304 out4:
1305   if (global_groups != NULL)
1306     NetApiBufferFree (global_groups);
1307 out3:
1308   if (local_groups != NULL)
1309     NetApiBufferFree (local_groups);
1310 out1:
1311   dbus_free (domain);
1312 out0:
1313   dbus_free (name);
1314
1315   return retval;
1316 #endif //DBUS_WINCE
1317 }
1318
1319 dbus_bool_t
1320 fill_win_user_info_homedir (wchar_t      *wname,
1321                             wchar_t      *wdomain,
1322                             DBusUserInfo *info,
1323                             DBusError    *error)
1324 {
1325 #ifdef DBUS_WINCE
1326         //TODO
1327         return TRUE;
1328 #else
1329   dbus_bool_t retval = FALSE;
1330   USER_INFO_1 *user_info = NULL;
1331   wchar_t wcomputername[MAX_COMPUTERNAME_LENGTH + 1];
1332   DWORD wcomputername_length = MAX_COMPUTERNAME_LENGTH + 1;
1333   dbus_bool_t local_computer;
1334   wchar_t *dc = NULL;
1335   NET_API_STATUS ret = 0;
1336
1337   /* If the domain is this computer's name, assume it's a local user.
1338    * Otherwise look up a DC for the domain, and ask it.
1339    */
1340
1341   GetComputerNameW (wcomputername, &wcomputername_length);
1342   local_computer = (wcsicmp (wcomputername, wdomain) == 0);
1343
1344   if (!local_computer)
1345     {
1346       ret = NetGetAnyDCName (NULL, wdomain, (LPBYTE *) &dc);
1347       if (ret != NERR_Success)
1348         {
1349           info->homedir = _dbus_strdup ("\\");
1350           _dbus_warn("NetGetAnyDCName() failed with errorcode %d '%s'\n",ret,_dbus_lm_strerror(ret));
1351           return TRUE;
1352         }
1353     }
1354
1355   /* No way to find out the profile of another user, let's try the
1356    * "home directory" from NetUserGetInfo's USER_INFO_1.
1357    */
1358   ret = NetUserGetInfo (dc, wname, 1, (LPBYTE *) &user_info);
1359   if (ret == NERR_Success )
1360     if(user_info->usri1_home_dir != NULL &&
1361         user_info->usri1_home_dir != (LPWSTR)0xfeeefeee &&  /* freed memory http://www.gamedev.net/community/forums/topic.asp?topic_id=158402 */
1362         user_info->usri1_home_dir[0] != '\0')
1363       {
1364         info->homedir = _dbus_win_utf16_to_utf8 (user_info->usri1_home_dir, error);
1365         if (!info->homedir)
1366           goto out1;
1367       }
1368     else
1369       {
1370         _dbus_verbose("NetUserGetInfo() returned no home dir entry\n");
1371         /* Not set, so use something random. */
1372         info->homedir = _dbus_strdup ("\\");
1373       }
1374   else
1375     {
1376       char *dc_string = _dbus_win_utf16_to_utf8(dc,error);
1377           char *user_name = _dbus_win_utf16_to_utf8(wname,error);
1378       _dbus_warn("NetUserGetInfo() for user '%s' failed with errorcode %d '%s', %s\n",user_name, ret,_dbus_lm_strerror(ret),dc_string);
1379       dbus_free(user_name);
1380       dbus_free(dc_string);
1381       /* Not set, so use something random. */
1382       info->homedir = _dbus_strdup ("\\");
1383     }
1384
1385   retval = TRUE;
1386
1387 out1:
1388   if (dc != NULL)
1389     NetApiBufferFree (dc);
1390   if (user_info != NULL)
1391     NetApiBufferFree (user_info);
1392
1393   return retval;
1394 #endif //DBUS_WINCE
1395 }
1396
1397 dbus_bool_t
1398 fill_win_user_info_from_name (wchar_t      *wname,
1399                               DBusUserInfo *info,
1400                               DBusError    *error)
1401 {
1402 #ifdef DBUS_WINCE
1403         return TRUE;
1404         //TODO
1405 #else
1406   dbus_bool_t retval = FALSE;
1407   PSID sid;
1408   wchar_t *wdomain;
1409   DWORD sid_length, wdomain_length;
1410   SID_NAME_USE use;
1411
1412   sid_length = 0;
1413   wdomain_length = 0;
1414   if (!LookupAccountNameW (NULL, wname, NULL, &sid_length,
1415                            NULL, &wdomain_length, &use) &&
1416       GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1417     {
1418       _dbus_win_set_error_from_win_error (error, GetLastError ());
1419       return FALSE;
1420     }
1421
1422   sid = dbus_malloc (sid_length);
1423   if (!sid)
1424     {
1425       _DBUS_SET_OOM (error);
1426       return FALSE;
1427     }
1428
1429   wdomain = dbus_new (wchar_t, wdomain_length);
1430   if (!wdomain)
1431     {
1432       _DBUS_SET_OOM (error);
1433       goto out0;
1434     }
1435
1436   if (!LookupAccountNameW (NULL, wname, sid, &sid_length,
1437                            wdomain, &wdomain_length, &use))
1438     {
1439       _dbus_win_set_error_from_win_error (error, GetLastError ());
1440       goto out1;
1441     }
1442
1443   if (!IsValidSid (sid))
1444     {
1445       dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1446       goto out1;
1447     }
1448
1449   info->uid = _dbus_win_sid_to_uid_t (sid);
1450
1451   if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1452     goto out1;
1453
1454   if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1455     goto out1;
1456
1457   retval = TRUE;
1458
1459 out1:
1460   dbus_free (wdomain);
1461 out0:
1462   dbus_free (sid);
1463
1464   return retval;
1465 #endif //DBUS_WINCE
1466 }
1467
1468 dbus_bool_t
1469 _dbus_win_sid_to_name_and_domain (dbus_uid_t uid,
1470                                   wchar_t  **wname,
1471                                   wchar_t  **wdomain,
1472                                   DBusError *error)
1473 {
1474 #ifdef DBUS_WINCE
1475         return TRUE;
1476         //TODO
1477 #else
1478   PSID sid;
1479   DWORD wname_length, wdomain_length;
1480   SID_NAME_USE use;
1481
1482   if (!_dbus_uid_t_to_win_sid (uid, &sid))
1483     {
1484       _dbus_win_set_error_from_win_error (error, GetLastError ());
1485       return FALSE;
1486     }
1487
1488   wname_length = 0;
1489   wdomain_length = 0;
1490   if (!LookupAccountSidW (NULL, sid, NULL, &wname_length,
1491                           NULL, &wdomain_length, &use) &&
1492       GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1493     {
1494       _dbus_win_set_error_from_win_error (error, GetLastError ());
1495       goto out0;
1496     }
1497
1498   *wname = dbus_new (wchar_t, wname_length);
1499   if (!*wname)
1500     {
1501       _DBUS_SET_OOM (error);
1502       goto out0;
1503     }
1504
1505   *wdomain = dbus_new (wchar_t, wdomain_length);
1506   if (!*wdomain)
1507     {
1508       _DBUS_SET_OOM (error);
1509       goto out1;
1510     }
1511
1512   if (!LookupAccountSidW (NULL, sid, *wname, &wname_length,
1513                           *wdomain, &wdomain_length, &use))
1514     {
1515       _dbus_win_set_error_from_win_error (error, GetLastError ());
1516       goto out2;
1517     }
1518
1519   return TRUE;
1520
1521 out2:
1522   dbus_free (*wdomain);
1523   *wdomain = NULL;
1524 out1:
1525   dbus_free (*wname);
1526   *wname = NULL;
1527 out0:
1528   LocalFree (sid);
1529
1530   return FALSE;
1531 #endif //DBUS_WINCE
1532 }
1533
1534 dbus_bool_t
1535 fill_win_user_info_from_uid (dbus_uid_t    uid,
1536                              DBusUserInfo *info,
1537                              DBusError    *error)
1538 {
1539 #ifdef DBUS_WINCE
1540         return TRUE;
1541         //TODO
1542 #else
1543   PSID sid;
1544   dbus_bool_t retval = FALSE;
1545   wchar_t *wname, *wdomain;
1546
1547   info->uid = uid;
1548
1549   if (!_dbus_win_sid_to_name_and_domain (uid, &wname, &wdomain, error))
1550     {
1551       _dbus_verbose("%s after _dbus_win_sid_to_name_and_domain\n",__FUNCTION__);
1552       return FALSE;
1553     }
1554
1555   if (!fill_win_user_info_name_and_groups (wname, wdomain, info, error))
1556     {
1557       _dbus_verbose("%s after fill_win_user_info_name_and_groups\n",__FUNCTION__);
1558       goto out0;
1559     }
1560
1561
1562   if (!fill_win_user_info_homedir (wname, wdomain, info, error))
1563     {
1564       _dbus_verbose("%s after fill_win_user_info_homedir\n",__FUNCTION__);
1565       goto out0;
1566     }
1567
1568   retval = TRUE;
1569
1570 out0:
1571   dbus_free (wdomain);
1572   dbus_free (wname);
1573
1574   return retval;
1575 #endif //DBUS_WINCE
1576 }
1577
1578
1579
1580
1581 void
1582 _dbus_win_startup_winsock (void)
1583 {
1584   /* Straight from MSDN, deuglified */
1585
1586   static dbus_bool_t beenhere = FALSE;
1587
1588   WORD wVersionRequested;
1589   WSADATA wsaData;
1590   int err;
1591
1592   if (beenhere)
1593     return;
1594
1595   wVersionRequested = MAKEWORD (2, 0);
1596
1597   err = WSAStartup (wVersionRequested, &wsaData);
1598   if (err != 0)
1599     {
1600       _dbus_assert_not_reached ("Could not initialize WinSock");
1601       _dbus_abort ();
1602     }
1603
1604   /* Confirm that the WinSock DLL supports 2.0.  Note that if the DLL
1605    * supports versions greater than 2.0 in addition to 2.0, it will
1606    * still return 2.0 in wVersion since that is the version we
1607    * requested.
1608    */
1609   if (LOBYTE (wsaData.wVersion) != 2 ||
1610       HIBYTE (wsaData.wVersion) != 0)
1611     {
1612       _dbus_assert_not_reached ("No usable WinSock found");
1613       _dbus_abort ();
1614     }
1615
1616   beenhere = TRUE;
1617 }
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627 /************************************************************************
1628  
1629  UTF / string code
1630  
1631  ************************************************************************/
1632
1633 /**
1634  * Measure the message length without terminating nul 
1635  */
1636 int _dbus_printf_string_upper_bound (const char *format,
1637                                      va_list args)
1638 {
1639   /* MSVCRT's vsnprintf semantics are a bit different */
1640   /* The C library source in the Platform SDK indicates that this
1641    * would work, but alas, it doesn't. At least not on Windows
1642    * 2000. Presumably those sources correspond to the C library on
1643    * some newer or even future Windows version.
1644    *
1645     len = _vsnprintf (NULL, _DBUS_INT_MAX, format, args);
1646    */
1647   char p[1024];
1648   int len;
1649   len = vsnprintf (p, sizeof(p)-1, format, args);
1650   if (len == -1) // try again
1651     {
1652       char *p;
1653       p = malloc (strlen(format)*3);
1654       len = vsnprintf (p, sizeof(p)-1, format, args);
1655       free(p);
1656     }
1657   return len;
1658 }
1659
1660
1661 /**
1662  * Returns the UTF-16 form of a UTF-8 string. The result should be
1663  * freed with dbus_free() when no longer needed.
1664  *
1665  * @param str the UTF-8 string
1666  * @param error return location for error code
1667  */
1668 wchar_t *
1669 _dbus_win_utf8_to_utf16 (const char *str,
1670                          DBusError  *error)
1671 {
1672   DBusString s;
1673   int n;
1674   wchar_t *retval;
1675
1676   _dbus_string_init_const (&s, str);
1677
1678   if (!_dbus_string_validate_utf8 (&s, 0, _dbus_string_get_length (&s)))
1679     {
1680       dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid UTF-8");
1681       return NULL;
1682     }
1683
1684   n = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0);
1685
1686   if (n == 0)
1687     {
1688       _dbus_win_set_error_from_win_error (error, GetLastError ());
1689       return NULL;
1690     }
1691
1692   retval = dbus_new (wchar_t, n);
1693
1694   if (!retval)
1695     {
1696       _DBUS_SET_OOM (error);
1697       return NULL;
1698     }
1699
1700   if (MultiByteToWideChar (CP_UTF8, 0, str, -1, retval, n) != n)
1701     {
1702       dbus_free (retval);
1703       dbus_set_error_const (error, DBUS_ERROR_FAILED, "MultiByteToWideChar inconsistency");
1704       return NULL;
1705     }
1706
1707   return retval;
1708 }
1709
1710 /**
1711  * Returns the UTF-8 form of a UTF-16 string. The result should be
1712  * freed with dbus_free() when no longer needed.
1713  *
1714  * @param str the UTF-16 string
1715  * @param error return location for error code
1716  */
1717 char *
1718 _dbus_win_utf16_to_utf8 (const wchar_t *str,
1719                          DBusError     *error)
1720 {
1721   int n;
1722   char *retval;
1723
1724   n = WideCharToMultiByte (CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
1725
1726   if (n == 0)
1727     {
1728       _dbus_win_set_error_from_win_error (error, GetLastError ());
1729       return NULL;
1730     }
1731
1732   retval = dbus_malloc (n);
1733
1734   if (!retval)
1735     {
1736       _DBUS_SET_OOM (error);
1737       return NULL;
1738     }
1739
1740   if (WideCharToMultiByte (CP_UTF8, 0, str, -1, retval, n, NULL, NULL) != n)
1741     {
1742       dbus_free (retval);
1743       dbus_set_error_const (error, DBUS_ERROR_FAILED, "WideCharToMultiByte inconsistency");
1744       return NULL;
1745     }
1746
1747   return retval;
1748 }
1749
1750
1751
1752
1753
1754
1755 /************************************************************************
1756  
1757  uid ... <-> win sid functions
1758  
1759  ************************************************************************/
1760
1761 dbus_bool_t
1762 _dbus_win_account_to_sid (const wchar_t *waccount,
1763                           void           **ppsid,
1764                           DBusError       *error)
1765 {
1766   dbus_bool_t retval = FALSE;
1767   DWORD sid_length, wdomain_length;
1768   SID_NAME_USE use;
1769   wchar_t *wdomain;
1770
1771   *ppsid = NULL;
1772
1773   sid_length = 0;
1774   wdomain_length = 0;
1775   if (!LookupAccountNameW (NULL, waccount, NULL, &sid_length,
1776                            NULL, &wdomain_length, &use) &&
1777       GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1778     {
1779       _dbus_win_set_error_from_win_error (error, GetLastError ());
1780       return FALSE;
1781     }
1782
1783   *ppsid = dbus_malloc (sid_length);
1784   if (!*ppsid)
1785     {
1786       _DBUS_SET_OOM (error);
1787       return FALSE;
1788     }
1789
1790   wdomain = dbus_new (wchar_t, wdomain_length);
1791   if (!wdomain)
1792     {
1793       _DBUS_SET_OOM (error);
1794       goto out1;
1795     }
1796
1797   if (!LookupAccountNameW (NULL, waccount, (PSID) *ppsid, &sid_length,
1798                            wdomain, &wdomain_length, &use))
1799     {
1800       _dbus_win_set_error_from_win_error (error, GetLastError ());
1801       goto out2;
1802     }
1803
1804   if (!IsValidSid ((PSID) *ppsid))
1805     {
1806       dbus_set_error_const (error, DBUS_ERROR_FAILED, "Invalid SID");
1807       goto out2;
1808     }
1809
1810   retval = TRUE;
1811
1812 out2:
1813   dbus_free (wdomain);
1814 out1:
1815   if (!retval)
1816     {
1817       dbus_free (*ppsid);
1818       *ppsid = NULL;
1819     }
1820
1821   return retval;
1822 }
1823
1824 static void
1825 _sid_atom_cache_shutdown (void *unused)
1826 {
1827   DBusHashIter iter;
1828   _DBUS_LOCK (sid_atom_cache);
1829   _dbus_hash_iter_init (sid_atom_cache, &iter);
1830   while (_dbus_hash_iter_next (&iter))
1831     {
1832       ATOM atom;
1833       atom = (ATOM) _dbus_hash_iter_get_value (&iter);
1834       GlobalDeleteAtom(atom);
1835       _dbus_hash_iter_remove_entry(&iter);
1836     }
1837   _DBUS_UNLOCK (sid_atom_cache);
1838   _dbus_hash_table_unref (sid_atom_cache);
1839   sid_atom_cache = NULL;
1840 }
1841
1842 /**
1843  * Returns the 2-way associated dbus_uid_t form a SID.
1844  *
1845  * @param psid pointer to the SID
1846  */
1847 dbus_uid_t
1848 _dbus_win_sid_to_uid_t (PSID psid)
1849 {
1850   dbus_uid_t uid;
1851   dbus_uid_t olduid;
1852   char *string;
1853   ATOM atom;
1854
1855   if (!IsValidSid (psid))
1856     {
1857       _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1858       return DBUS_UID_UNSET;
1859     }
1860   if (!ConvertSidToStringSidA (psid, &string))
1861     {
1862       _dbus_verbose("%s invalid sid\n",__FUNCTION__);
1863       return DBUS_UID_UNSET;
1864     }
1865
1866   atom = GlobalAddAtom(string);
1867
1868   if (atom == 0)
1869     {
1870       _dbus_verbose("%s GlobalAddAtom failed\n",__FUNCTION__);
1871       LocalFree (string);
1872       return DBUS_UID_UNSET;
1873     }
1874
1875   _DBUS_LOCK (sid_atom_cache);
1876
1877   if (sid_atom_cache == NULL)
1878     {
1879       sid_atom_cache = _dbus_hash_table_new (DBUS_HASH_ULONG, NULL, NULL);
1880       _dbus_register_shutdown_func (_sid_atom_cache_shutdown, NULL);
1881     }
1882
1883   uid = atom;
1884   olduid = (dbus_uid_t) _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1885
1886   if (olduid)
1887     {
1888       _dbus_verbose("%s sid with id %i found in cache\n",__FUNCTION__, olduid);
1889       uid = olduid;
1890     }
1891   else
1892     {
1893       _dbus_hash_table_insert_ulong (sid_atom_cache, uid, (void*) uid);
1894       _dbus_verbose("%s sid %s added with uid %i to cache\n",__FUNCTION__, string, uid);
1895     }
1896
1897   _DBUS_UNLOCK (sid_atom_cache);
1898
1899   return uid;
1900 }
1901
1902 dbus_bool_t  _dbus_uid_t_to_win_sid (dbus_uid_t uid, PSID *ppsid)
1903 {
1904   void* atom;
1905   char string[255];
1906
1907   atom = _dbus_hash_table_lookup_ulong (sid_atom_cache, uid);
1908   if (atom == NULL)
1909     {
1910       _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__,uid);
1911       return FALSE;
1912     }
1913   memset( string, '.', sizeof(string) );
1914   if (!GlobalGetAtomNameA( (ATOM) atom, string, 255 ))
1915     {
1916       _dbus_verbose("%s uid %i not found in cache\n",__FUNCTION__, uid);
1917       return FALSE;
1918     }
1919   if (!ConvertStringSidToSidA(string, ppsid))
1920     {
1921       _dbus_verbose("%s could not convert %s into sid \n",__FUNCTION__, string);
1922       return FALSE;
1923     }
1924   _dbus_verbose("%s converted %s into sid \n",__FUNCTION__, string);
1925   return TRUE;
1926 }
1927
1928
1929 /** @} end of sysdeps-win */
1930
1931
1932 /** Gets our UID
1933  * @returns process UID
1934  */
1935 dbus_uid_t
1936 _dbus_getuid(void)
1937 {
1938   dbus_uid_t retval = DBUS_UID_UNSET;
1939   HANDLE process_token = NULL;
1940   TOKEN_USER *token_user = NULL;
1941   DWORD n;
1942
1943   if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1944     _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1945   else if ((!GetTokenInformation (process_token, TokenUser, NULL, 0, &n)
1946             && GetLastError () != ERROR_INSUFFICIENT_BUFFER)
1947            || (token_user = alloca (n)) == NULL
1948            || !GetTokenInformation (process_token, TokenUser, token_user, n, &n))
1949     _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1950   else
1951     retval = _dbus_win_sid_to_uid_t (token_user->User.Sid);
1952
1953   if (process_token != NULL)
1954     CloseHandle (process_token);
1955
1956   _dbus_verbose("_dbus_getuid() returns %d\n",retval);
1957   return retval;
1958 }
1959
1960 #ifdef DBUS_BUILD_TESTS
1961 /** Gets our GID
1962  * @returns process GID
1963  */
1964 dbus_gid_t
1965 _dbus_getgid (void)
1966 {
1967   dbus_gid_t retval = DBUS_GID_UNSET;
1968   HANDLE process_token = NULL;
1969   TOKEN_PRIMARY_GROUP *token_primary_group = NULL;
1970   DWORD n;
1971
1972   if (!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &process_token))
1973     _dbus_win_warn_win_error ("OpenProcessToken failed", GetLastError ());
1974   else if ((!GetTokenInformation (process_token, TokenPrimaryGroup,
1975                                   NULL, 0, &n) &&
1976             GetLastError () != ERROR_INSUFFICIENT_BUFFER) ||
1977            (token_primary_group = alloca (n)) == NULL ||
1978            !GetTokenInformation (process_token, TokenPrimaryGroup,
1979                                  token_primary_group, n, &n))
1980     _dbus_win_warn_win_error ("GetTokenInformation failed", GetLastError ());
1981   else
1982     retval = _dbus_win_sid_to_uid_t (token_primary_group->PrimaryGroup);
1983
1984   if (process_token != NULL)
1985     CloseHandle (process_token);
1986
1987   return retval;
1988 }
1989
1990 #if 0
1991 dbus_bool_t
1992 _dbus_domain_test (const char *test_data_dir)
1993 {
1994   if (!_dbus_test_oom_handling ("spawn_nonexistent",
1995                                 check_spawn_nonexistent,
1996                                 NULL))
1997     return FALSE;
1998 }
1999
2000 #endif
2001
2002 #endif //DBUS_BUILD_TESTS
2003
2004 /************************************************************************
2005  
2006  pipes
2007  
2008  ************************************************************************/
2009
2010 /**
2011  * Creates a full-duplex pipe (as in socketpair()).
2012  * Sets both ends of the pipe nonblocking.
2013  *
2014  * @todo libdbus only uses this for the debug-pipe server, so in
2015  * principle it could be in dbus-sysdeps-util.c, except that
2016  * dbus-sysdeps-util.c isn't in libdbus when tests are enabled and the
2017  * debug-pipe server is used.
2018  * 
2019  * @param fd1 return location for one end
2020  * @param fd2 return location for the other end
2021  * @param blocking #TRUE if pipe should be blocking
2022  * @param error error return
2023  * @returns #FALSE on failure (if error is set)
2024  */
2025 dbus_bool_t
2026 _dbus_full_duplex_pipe (int        *fd1,
2027                         int        *fd2,
2028                         dbus_bool_t blocking,
2029                         DBusError  *error)
2030 {
2031   SOCKET temp, socket1 = -1, socket2 = -1;
2032   struct sockaddr_in saddr;
2033   int len;
2034   u_long arg;
2035   fd_set read_set, write_set;
2036   struct timeval tv;
2037   DBusSocket sock;
2038
2039
2040   _dbus_win_startup_winsock ();
2041
2042   temp = socket (AF_INET, SOCK_STREAM, 0);
2043   if (temp == INVALID_SOCKET)
2044     {
2045       DBUS_SOCKET_SET_ERRNO ();
2046       goto out0;
2047     }
2048
2049   arg = 1;
2050   if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR)
2051     {
2052       DBUS_SOCKET_SET_ERRNO ();
2053       goto out0;
2054     }
2055
2056   _DBUS_ZERO (saddr);
2057   saddr.sin_family = AF_INET;
2058   saddr.sin_port = 0;
2059   saddr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
2060
2061   if (bind (temp, (struct sockaddr *)&saddr, sizeof (saddr)))
2062     {
2063       DBUS_SOCKET_SET_ERRNO ();
2064       goto out0;
2065     }
2066
2067   if (listen (temp, 1) == SOCKET_ERROR)
2068     {
2069       DBUS_SOCKET_SET_ERRNO ();
2070       goto out0;
2071     }
2072
2073   len = sizeof (saddr);
2074   if (getsockname (temp, (struct sockaddr *)&saddr, &len))
2075     {
2076       DBUS_SOCKET_SET_ERRNO ();
2077       goto out0;
2078     }
2079
2080   socket1 = socket (AF_INET, SOCK_STREAM, 0);
2081   if (socket1 == INVALID_SOCKET)
2082     {
2083       DBUS_SOCKET_SET_ERRNO ();
2084       goto out0;
2085     }
2086
2087   arg = 1;
2088   if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2089     {
2090       DBUS_SOCKET_SET_ERRNO ();
2091       goto out1;
2092     }
2093
2094   if (connect (socket1, (struct sockaddr  *)&saddr, len) != SOCKET_ERROR ||
2095       WSAGetLastError () != WSAEWOULDBLOCK)
2096     {
2097       dbus_set_error_const (error, DBUS_ERROR_FAILED,
2098                             "_dbus_full_duplex_pipe socketpair() emulation failed");
2099       goto out1;
2100     }
2101
2102   FD_ZERO (&read_set);
2103   FD_SET (temp, &read_set);
2104
2105   tv.tv_sec = 0;
2106   tv.tv_usec = 0;
2107
2108   if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR)
2109     {
2110       DBUS_SOCKET_SET_ERRNO ();
2111       goto out1;
2112     }
2113
2114   _dbus_assert (FD_ISSET (temp, &read_set));
2115
2116   socket2 = accept (temp, (struct sockaddr *) &saddr, &len);
2117   if (socket2 == INVALID_SOCKET)
2118     {
2119       DBUS_SOCKET_SET_ERRNO ();
2120       goto out1;
2121     }
2122
2123   FD_ZERO (&write_set);
2124   FD_SET (socket1, &write_set);
2125
2126   tv.tv_sec = 0;
2127   tv.tv_usec = 0;
2128
2129   if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR)
2130     {
2131       DBUS_SOCKET_SET_ERRNO ();
2132       goto out2;
2133     }
2134
2135   _dbus_assert (FD_ISSET (socket1, &write_set));
2136
2137   if (blocking)
2138     {
2139       arg = 0;
2140       if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR)
2141         {
2142           DBUS_SOCKET_SET_ERRNO ();
2143           goto out2;
2144         }
2145
2146       arg = 0;
2147       if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2148         {
2149           DBUS_SOCKET_SET_ERRNO ();
2150           goto out2;
2151         }
2152     }
2153   else
2154     {
2155       arg = 1;
2156       if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR)
2157         {
2158           DBUS_SOCKET_SET_ERRNO ();
2159           goto out2;
2160         }
2161     }
2162
2163   sock.fd = socket1;
2164   *fd1 = _dbus_socket_to_handle (&sock);
2165   sock.fd = socket2;
2166   *fd2 = _dbus_socket_to_handle (&sock);
2167
2168   _dbus_verbose ("full-duplex pipe %d:%d <-> %d:%d\n",
2169                  *fd1, socket1, *fd2, socket2);
2170
2171   closesocket (temp);
2172
2173   return TRUE;
2174
2175 out2:
2176   closesocket (socket2);
2177 out1:
2178   closesocket (socket1);
2179 out0:
2180   closesocket (temp);
2181
2182   dbus_set_error (error, _dbus_error_from_errno (errno),
2183                   "Could not setup socket pair: %s",
2184                   _dbus_strerror (errno));
2185
2186   return FALSE;
2187 }
2188
2189 /**
2190  * Wrapper for poll().
2191  *
2192  * @param fds the file descriptors to poll
2193  * @param n_fds number of descriptors in the array
2194  * @param timeout_milliseconds timeout or -1 for infinite
2195  * @returns numbers of fds with revents, or <0 on error
2196  */
2197 #define USE_CHRIS_IMPL 0
2198 #if USE_CHRIS_IMPL
2199 int
2200 _dbus_poll (DBusPollFD *fds,
2201             int         n_fds,
2202             int         timeout_milliseconds)
2203 {
2204 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2205   char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2206   char *msgp;
2207
2208   int ret = 0;
2209   int i;
2210   struct timeval tv;
2211   int ready;
2212
2213 #define DBUS_STACK_WSAEVENTS 256
2214   WSAEVENT eventsOnStack[DBUS_STACK_WSAEVENTS];
2215   WSAEVENT *pEvents = NULL;
2216   if (n_fds > DBUS_STACK_WSAEVENTS)
2217     pEvents = calloc(sizeof(WSAEVENT), n_fds);
2218   else
2219     pEvents = eventsOnStack;
2220
2221   _dbus_lock_sockets();
2222
2223 #ifdef DBUS_ENABLE_VERBOSE_MODE
2224   msgp = msg;
2225   msgp += sprintf (msgp, "WSAEventSelect: to=%d\n\t", timeout_milliseconds);
2226   for (i = 0; i < n_fds; i++)
2227     {
2228       static dbus_bool_t warned = FALSE;
2229       DBusSocket *s;
2230       DBusPollFD *fdp = &fds[i];
2231
2232       _dbus_handle_to_socket_unlocked(fdp->fd, &s);  
2233
2234       if (s->is_used == 0)
2235         {
2236           _dbus_warn ("no valid socket");
2237           warned = TRUE;
2238         }
2239
2240       if (fdp->events & _DBUS_POLLIN)
2241         msgp += sprintf (msgp, "R:%d ", s->fd);
2242
2243       if (fdp->events & _DBUS_POLLOUT)
2244         msgp += sprintf (msgp, "W:%d ", s->fd);
2245
2246       msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2247
2248       // FIXME: more robust code for long  msg
2249       //        create on heap when msg[] becomes too small
2250       if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2251         {
2252           _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2253         }
2254     }
2255
2256   msgp += sprintf (msgp, "\n");
2257   _dbus_verbose ("%s",msg);
2258 #endif
2259   for (i = 0; i < n_fds; i++)
2260     {
2261       DBusSocket *s;
2262       DBusPollFD *fdp = &fds[i];
2263       WSAEVENT ev;
2264       long lNetworkEvents = FD_OOB;
2265
2266       _dbus_handle_to_socket_unlocked(fdp->fd, &s); 
2267
2268       if (s->is_used == 0)
2269         continue;
2270
2271       ev = WSACreateEvent();
2272
2273       if (fdp->events & _DBUS_POLLIN)
2274         lNetworkEvents |= FD_READ | FD_ACCEPT | FD_CLOSE;
2275
2276       if (fdp->events & _DBUS_POLLOUT)
2277         lNetworkEvents |= FD_WRITE | FD_CONNECT;
2278
2279       WSAEventSelect(s->fd, ev, lNetworkEvents);
2280
2281       pEvents[i] = ev;
2282     }
2283
2284   _dbus_unlock_sockets();
2285
2286   ready = WSAWaitForMultipleEvents (n_fds, pEvents, FALSE, timeout_milliseconds, FALSE);
2287
2288   if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2289     {
2290       DBUS_SOCKET_SET_ERRNO ();
2291       if (errno != EWOULDBLOCK)
2292         _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", _dbus_strerror (errno));
2293       ret = -1;
2294     }
2295   else if (ready == WSA_WAIT_TIMEOUT)
2296     {
2297       _dbus_verbose ("WSAWaitForMultipleEvents: WSA_WAIT_TIMEOUT\n");
2298       ret = 0;
2299     }
2300   else if (ready >= WSA_WAIT_EVENT_0 && ready < (int)(WSA_WAIT_EVENT_0 + n_fds))
2301     {
2302       msgp = msg;
2303       msgp += sprintf (msgp, "WSAWaitForMultipleEvents: =%d\n\t", ready);
2304
2305       _dbus_lock_sockets();
2306       for (i = 0; i < n_fds; i++)
2307         {
2308           DBusSocket *s;
2309           DBusPollFD *fdp = &fds[i];
2310           WSANETWORKEVENTS ne;
2311
2312           _dbus_handle_to_socket_unlocked(fdp->fd, &s); 
2313
2314           fdp->revents = 0;
2315
2316           WSAEnumNetworkEvents(s->fd, pEvents[i], &ne);
2317
2318           if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2319             fdp->revents |= _DBUS_POLLIN;
2320
2321           if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2322             fdp->revents |= _DBUS_POLLOUT;
2323
2324           if (ne.lNetworkEvents & (FD_OOB))
2325             fdp->revents |= _DBUS_POLLERR;
2326
2327           if (ne.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
2328               msgp += sprintf (msgp, "R:%d ", s->fd);
2329
2330           if (ne.lNetworkEvents & (FD_WRITE | FD_CONNECT))
2331               msgp += sprintf (msgp, "W:%d ", s->fd);
2332
2333           if (ne.lNetworkEvents & (FD_OOB))
2334               msgp += sprintf (msgp, "E:%d ", s->fd);
2335
2336           msgp += sprintf (msgp, "lNetworkEvents:%d ", ne.lNetworkEvents);
2337
2338           if(ne.lNetworkEvents)
2339             ret++;
2340
2341           WSAEventSelect(s->fd, pEvents[i], 0);
2342         }
2343       _dbus_unlock_sockets();
2344
2345       msgp += sprintf (msgp, "\n");
2346       _dbus_verbose ("%s",msg);
2347     }
2348   else
2349     {
2350       _dbus_verbose ("WSAWaitForMultipleEvents: failed for unknown reason!");
2351       ret = -1;
2352     }
2353
2354   for(i = 0; i < n_fds; i++)
2355     {
2356       WSACloseEvent(pEvents[i]);
2357     }
2358
2359   if (n_fds > DBUS_STACK_WSAEVENTS)
2360     free(pEvents);
2361
2362   return ret;
2363 }
2364
2365 #else   // USE_CHRIS_IMPL
2366
2367 int
2368 _dbus_poll (DBusPollFD *fds,
2369             int         n_fds,
2370             int         timeout_milliseconds)
2371 {
2372 #define DBUS_POLL_CHAR_BUFFER_SIZE 2000
2373   char msg[DBUS_POLL_CHAR_BUFFER_SIZE];
2374   char *msgp;
2375
2376   fd_set read_set, write_set, err_set;
2377   int max_fd = 0;
2378   int i;
2379   struct timeval tv;
2380   int ready;
2381
2382   FD_ZERO (&read_set);
2383   FD_ZERO (&write_set);
2384   FD_ZERO (&err_set);
2385
2386   _dbus_lock_sockets();
2387
2388 #ifdef DBUS_ENABLE_VERBOSE_MODE
2389   msgp = msg;
2390   msgp += sprintf (msgp, "select: to=%d\n\t", timeout_milliseconds);
2391   for (i = 0; i < n_fds; i++)
2392     {
2393       static dbus_bool_t warned = FALSE;
2394       DBusSocket *s;
2395       DBusPollFD *fdp = &fds[i];
2396
2397       _dbus_handle_to_socket_unlocked(fdp->fd, &s);  
2398
2399       if (s->is_used == 0)
2400         {
2401           _dbus_warn ("no valid socket");
2402           warned = TRUE;
2403         }
2404
2405       if (fdp->events & _DBUS_POLLIN)
2406         msgp += sprintf (msgp, "R:%d ", s->fd);
2407
2408       if (fdp->events & _DBUS_POLLOUT)
2409         msgp += sprintf (msgp, "W:%d ", s->fd);
2410
2411       msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2412
2413       // FIXME: more robust code for long  msg
2414       //        create on heap when msg[] becomes too small
2415       if (msgp >= msg + DBUS_POLL_CHAR_BUFFER_SIZE)
2416         {
2417           _dbus_assert_not_reached ("buffer overflow in _dbus_poll");
2418         }
2419     }
2420
2421   msgp += sprintf (msgp, "\n");
2422   _dbus_verbose ("%s",msg);
2423 #endif
2424   for (i = 0; i < n_fds; i++)
2425     {
2426       DBusSocket *s;
2427       DBusPollFD *fdp = &fds[i];
2428
2429       _dbus_handle_to_socket_unlocked(fdp->fd, &s); 
2430
2431       if (s->is_used != 1)
2432         continue;
2433
2434       if (fdp->events & _DBUS_POLLIN)
2435         FD_SET (s->fd, &read_set);
2436
2437       if (fdp->events & _DBUS_POLLOUT)
2438         FD_SET (s->fd, &write_set);
2439
2440       FD_SET (s->fd, &err_set);
2441
2442       max_fd = MAX (max_fd, s->fd);
2443     }
2444
2445   _dbus_unlock_sockets();
2446
2447   tv.tv_sec = timeout_milliseconds / 1000;
2448   tv.tv_usec = (timeout_milliseconds % 1000) * 1000;
2449
2450   ready = select (max_fd + 1, &read_set, &write_set, &err_set,
2451                   timeout_milliseconds < 0 ? NULL : &tv);
2452
2453   if (DBUS_SOCKET_API_RETURNS_ERROR (ready))
2454     {
2455       DBUS_SOCKET_SET_ERRNO ();
2456       if (errno != EWOULDBLOCK)
2457         _dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno));
2458     }
2459   else if (ready == 0)
2460     _dbus_verbose ("select: = 0\n");
2461   else
2462     if (ready > 0)
2463       {
2464 #ifdef DBUS_ENABLE_VERBOSE_MODE
2465         msgp = msg;
2466         msgp += sprintf (msgp, "select: = %d:\n\t", ready);
2467         _dbus_lock_sockets();
2468         for (i = 0; i < n_fds; i++)
2469           {
2470             DBusSocket *s;
2471             DBusPollFD *fdp = &fds[i];
2472
2473             _dbus_handle_to_socket_unlocked(fdp->fd, &s); 
2474
2475             if (FD_ISSET (s->fd, &read_set))
2476               msgp += sprintf (msgp, "R:%d ", s->fd);
2477
2478             if (FD_ISSET (s->fd, &write_set))
2479               msgp += sprintf (msgp, "W:%d ", s->fd);
2480
2481             if (FD_ISSET (s->fd, &err_set))
2482               msgp += sprintf (msgp, "E:%d\n\t", s->fd);
2483           }
2484         msgp += sprintf (msgp, "\n");
2485         _dbus_verbose ("%s",msg);
2486 #endif
2487
2488         for (i = 0; i < n_fds; i++)
2489           {
2490             DBusSocket *s;
2491             DBusPollFD *fdp = &fds[i];
2492
2493             _dbus_handle_to_socket_unlocked(fdp->fd, &s); 
2494
2495             fdp->revents = 0;
2496
2497             if (FD_ISSET (s->fd, &read_set))
2498               fdp->revents |= _DBUS_POLLIN;
2499
2500             if (FD_ISSET (s->fd, &write_set))
2501               fdp->revents |= _DBUS_POLLOUT;
2502
2503             if (FD_ISSET (s->fd, &err_set))
2504               fdp->revents |= _DBUS_POLLERR;
2505           }
2506         _dbus_unlock_sockets();
2507       }
2508   return ready;
2509 }
2510 #endif  // USE_CHRIS_IMPL
2511
2512
2513 /************************************************************************
2514  
2515  error handling
2516  
2517  ************************************************************************/
2518
2519
2520 /**
2521  * Assigns an error name and message corresponding to a Win32 error
2522  * code to a DBusError. Does nothing if error is #NULL.
2523  *
2524  * @param error the error.
2525  * @param code the Win32 error code
2526  */
2527 void
2528 _dbus_win_set_error_from_win_error (DBusError *error,
2529                                     int        code)
2530 {
2531   char *msg;
2532
2533   /* As we want the English message, use the A API */
2534   FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
2535                   FORMAT_MESSAGE_IGNORE_INSERTS |
2536                   FORMAT_MESSAGE_FROM_SYSTEM,
2537                   NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US),
2538                   (LPTSTR) &msg, 0, NULL);
2539   if (msg)
2540     {
2541       char *msg_copy;
2542
2543       msg_copy = dbus_malloc (strlen (msg));
2544       strcpy (msg_copy, msg);
2545       LocalFree (msg);
2546
2547       dbus_set_error (error, "win32.error", "%s", msg_copy);
2548     }
2549   else
2550     dbus_set_error_const (error, "win32.error", "Unknown error code or FormatMessage failed");
2551 }
2552
2553 void
2554 _dbus_win_warn_win_error (const char *message,
2555                           int         code)
2556 {
2557   DBusError error;
2558
2559   dbus_error_init (&error);
2560   _dbus_win_set_error_from_win_error (&error, code);
2561   _dbus_warn ("%s: %s\n", message, error.message);
2562   dbus_error_free (&error);
2563 }
2564
2565 /**
2566  * A wrapper around strerror() because some platforms
2567  * may be lame and not have strerror().
2568  *
2569  * @param error_number errno.
2570  * @returns error description.
2571  */
2572 const char*
2573 _dbus_strerror (int error_number)
2574 {
2575 #ifdef DBUS_WINCE
2576   // TODO
2577   return "unknown";
2578 #else
2579   const char *msg;
2580
2581   switch (error_number)
2582     {
2583     case WSAEINTR:
2584       return "Interrupted function call";
2585     case WSAEACCES:
2586       return "Permission denied";
2587     case WSAEFAULT:
2588       return "Bad address";
2589     case WSAEINVAL:
2590       return "Invalid argument";
2591     case WSAEMFILE:
2592       return "Too many open files";
2593     case WSAEWOULDBLOCK:
2594       return "Resource temporarily unavailable";
2595     case WSAEINPROGRESS:
2596       return "Operation now in progress";
2597     case WSAEALREADY:
2598       return "Operation already in progress";
2599     case WSAENOTSOCK:
2600       return "Socket operation on nonsocket";
2601     case WSAEDESTADDRREQ:
2602       return "Destination address required";
2603     case WSAEMSGSIZE:
2604       return "Message too long";
2605     case WSAEPROTOTYPE:
2606       return "Protocol wrong type for socket";
2607     case WSAENOPROTOOPT:
2608       return "Bad protocol option";
2609     case WSAEPROTONOSUPPORT:
2610       return "Protocol not supported";
2611     case WSAESOCKTNOSUPPORT:
2612       return "Socket type not supported";
2613     case WSAEOPNOTSUPP:
2614       return "Operation not supported";
2615     case WSAEPFNOSUPPORT:
2616       return "Protocol family not supported";
2617     case WSAEAFNOSUPPORT:
2618       return "Address family not supported by protocol family";
2619     case WSAEADDRINUSE:
2620       return "Address already in use";
2621     case WSAEADDRNOTAVAIL:
2622       return "Cannot assign requested address";
2623     case WSAENETDOWN:
2624       return "Network is down";
2625     case WSAENETUNREACH:
2626       return "Network is unreachable";
2627     case WSAENETRESET:
2628       return "Network dropped connection on reset";
2629     case WSAECONNABORTED:
2630       return "Software caused connection abort";
2631     case WSAECONNRESET:
2632       return "Connection reset by peer";
2633     case WSAENOBUFS:
2634       return "No buffer space available";
2635     case WSAEISCONN:
2636       return "Socket is already connected";
2637     case WSAENOTCONN:
2638       return "Socket is not connected";
2639     case WSAESHUTDOWN:
2640       return "Cannot send after socket shutdown";
2641     case WSAETIMEDOUT:
2642       return "Connection timed out";
2643     case WSAECONNREFUSED:
2644       return "Connection refused";
2645     case WSAEHOSTDOWN:
2646       return "Host is down";
2647     case WSAEHOSTUNREACH:
2648       return "No route to host";
2649     case WSAEPROCLIM:
2650       return "Too many processes";
2651     case WSAEDISCON:
2652       return "Graceful shutdown in progress";
2653     case WSATYPE_NOT_FOUND:
2654       return "Class type not found";
2655     case WSAHOST_NOT_FOUND:
2656       return "Host not found";
2657     case WSATRY_AGAIN:
2658       return "Nonauthoritative host not found";
2659     case WSANO_RECOVERY:
2660       return "This is a nonrecoverable error";
2661     case WSANO_DATA:
2662       return "Valid name, no data record of requested type";
2663     case WSA_INVALID_HANDLE:
2664       return "Specified event object handle is invalid";
2665     case WSA_INVALID_PARAMETER:
2666       return "One or more parameters are invalid";
2667     case WSA_IO_INCOMPLETE:
2668       return "Overlapped I/O event object not in signaled state";
2669     case WSA_IO_PENDING:
2670       return "Overlapped operations will complete later";
2671     case WSA_NOT_ENOUGH_MEMORY:
2672       return "Insufficient memory available";
2673     case WSA_OPERATION_ABORTED:
2674       return "Overlapped operation aborted";
2675 #ifdef WSAINVALIDPROCTABLE
2676
2677     case WSAINVALIDPROCTABLE:
2678       return "Invalid procedure table from service provider";
2679 #endif
2680 #ifdef WSAINVALIDPROVIDER
2681
2682     case WSAINVALIDPROVIDER:
2683       return "Invalid service provider version number";
2684 #endif
2685 #ifdef WSAPROVIDERFAILEDINIT
2686
2687     case WSAPROVIDERFAILEDINIT:
2688       return "Unable to initialize a service provider";
2689 #endif
2690
2691     case WSASYSCALLFAILURE:
2692       return "System call failure";
2693     }
2694   msg = strerror (error_number);
2695   if (msg == NULL)
2696     msg = "unknown";
2697
2698   return msg;
2699 #endif //DBUS_WINCE
2700 }
2701
2702
2703
2704 /* lan manager error codes */
2705 const char*
2706 _dbus_lm_strerror(int error_number)
2707 {
2708 #ifdef DBUS_WINCE
2709   // TODO
2710   return "unknown";
2711 #else
2712   const char *msg;
2713   switch (error_number)
2714     {
2715     case NERR_NetNotStarted:
2716       return "The workstation driver is not installed.";
2717     case NERR_UnknownServer:
2718       return "The server could not be located.";
2719     case NERR_ShareMem:
2720       return "An internal error occurred. The network cannot access a shared memory segment.";
2721     case NERR_NoNetworkResource:
2722       return "A network resource shortage occurred.";
2723     case NERR_RemoteOnly:
2724       return "This operation is not supported on workstations.";
2725     case NERR_DevNotRedirected:
2726       return "The device is not connected.";
2727     case NERR_ServerNotStarted:
2728       return "The Server service is not started.";
2729     case NERR_ItemNotFound:
2730       return "The queue is empty.";
2731     case NERR_UnknownDevDir:
2732       return "The device or directory does not exist.";
2733     case NERR_RedirectedPath:
2734       return "The operation is invalid on a redirected resource.";
2735     case NERR_DuplicateShare:
2736       return "The name has already been shared.";
2737     case NERR_NoRoom:
2738       return "The server is currently out of the requested resource.";
2739     case NERR_TooManyItems:
2740       return "Requested addition of items exceeds the maximum allowed.";
2741     case NERR_InvalidMaxUsers:
2742       return "The Peer service supports only two simultaneous users.";
2743     case NERR_BufTooSmall:
2744       return "The API return buffer is too small.";
2745     case NERR_RemoteErr:
2746       return "A remote API error occurred.";
2747     case NERR_LanmanIniError:
2748       return "An error occurred when opening or reading the configuration file.";
2749     case NERR_NetworkError:
2750       return "A general network error occurred.";
2751     case NERR_WkstaInconsistentState:
2752       return "The Workstation service is in an inconsistent state. Restart the computer before restarting the Workstation service.";
2753     case NERR_WkstaNotStarted:
2754       return "The Workstation service has not been started.";
2755     case NERR_BrowserNotStarted:
2756       return "The requested information is not available.";
2757     case NERR_InternalError:
2758       return "An internal error occurred.";
2759     case NERR_BadTransactConfig:
2760       return "The server is not configured for transactions.";
2761     case NERR_InvalidAPI:
2762       return "The requested API is not supported on the remote server.";
2763     case NERR_BadEventName:
2764       return "The event name is invalid.";
2765     case NERR_DupNameReboot:
2766       return "The computer name already exists on the network. Change it and restart the computer.";
2767     case NERR_CfgCompNotFound:
2768       return "The specified component could not be found in the configuration information.";
2769     case NERR_CfgParamNotFound:
2770       return "The specified parameter could not be found in the configuration information.";
2771     case NERR_LineTooLong:
2772       return "A line in the configuration file is too long.";
2773     case NERR_QNotFound:
2774       return "The printer does not exist.";
2775     case NERR_JobNotFound:
2776       return "The print job does not exist.";
2777     case NERR_DestNotFound:
2778       return "The printer destination cannot be found.";
2779     case NERR_DestExists:
2780       return "The printer destination already exists.";
2781     case NERR_QExists:
2782       return "The printer queue already exists.";
2783     case NERR_QNoRoom:
2784       return "No more printers can be added.";
2785     case NERR_JobNoRoom:
2786       return "No more print jobs can be added.";
2787     case NERR_DestNoRoom:
2788       return "No more printer destinations can be added.";
2789     case NERR_DestIdle:
2790       return "This printer destination is idle and cannot accept control operations.";
2791     case NERR_DestInvalidOp:
2792       return "This printer destination request contains an invalid control function.";
2793     case NERR_ProcNoRespond:
2794       return "The print processor is not responding.";
2795     case NERR_SpoolerNotLoaded:
2796       return "The spooler is not running.";
2797     case NERR_DestInvalidState:
2798       return "This operation cannot be performed on the print destination in its current state.";
2799     case NERR_QInvalidState:
2800       return "This operation cannot be performed on the printer queue in its current state.";
2801     case NERR_JobInvalidState:
2802       return "This operation cannot be performed on the print job in its current state.";
2803     case NERR_SpoolNoMemory:
2804       return "A spooler memory allocation failure occurred.";
2805     case NERR_DriverNotFound:
2806       return "The device driver does not exist.";
2807     case NERR_DataTypeInvalid:
2808       return "The data type is not supported by the print processor.";
2809     case NERR_ProcNotFound:
2810       return "The print processor is not installed.";
2811     case NERR_ServiceTableLocked:
2812       return "The service database is locked.";
2813     case NERR_ServiceTableFull:
2814       return "The service table is full.";
2815     case NERR_ServiceInstalled:
2816       return "The requested service has already been started.";
2817     case NERR_ServiceEntryLocked:
2818       return "The service does not respond to control actions.";
2819     case NERR_ServiceNotInstalled:
2820       return "The service has not been started.";
2821     case NERR_BadServiceName:
2822       return "The service name is invalid.";
2823     case NERR_ServiceCtlTimeout:
2824       return "The service is not responding to the control function.";
2825     case NERR_ServiceCtlBusy:
2826       return "The service control is busy.";
2827     case NERR_BadServiceProgName:
2828       return "The configuration file contains an invalid service program name.";
2829     case NERR_ServiceNotCtrl:
2830       return "The service could not be controlled in its present state.";
2831     case NERR_ServiceKillProc:
2832       return "The service ended abnormally.";
2833     case NERR_ServiceCtlNotValid:
2834       return "The requested pause or stop is not valid for this service.";
2835     case NERR_NotInDispatchTbl:
2836       return "The service control dispatcher could not find the service name in the dispatch table.";
2837     case NERR_BadControlRecv:
2838       return "The service control dispatcher pipe read failed.";
2839     case NERR_ServiceNotStarting:
2840       return "A thread for the new service could not be created.";
2841     case NERR_AlreadyLoggedOn:
2842       return "This workstation is already logged on to the local-area network.";
2843     case NERR_NotLoggedOn:
2844       return "The workstation is not logged on to the local-area network.";
2845     case NERR_BadUsername:
2846       return "The user name or group name parameter is invalid.";
2847     case NERR_BadPassword:
2848       return "The password parameter is invalid.";
2849     case NERR_UnableToAddName_W:
2850       return "@W The logon processor did not add the message alias.";
2851     case NERR_UnableToAddName_F:
2852       return "The logon processor did not add the message alias.";
2853     case NERR_UnableToDelName_W:
2854       return "@W The logoff processor did not delete the message alias.";
2855     case NERR_UnableToDelName_F:
2856       return "The logoff processor did not delete the message alias.";
2857     case NERR_LogonsPaused:
2858       return "Network logons are paused.";
2859     case NERR_LogonServerConflict:
2860       return "A centralized logon-server conflict occurred.";
2861     case NERR_LogonNoUserPath:
2862       return "The server is configured without a valid user path.";
2863     case NERR_LogonScriptError:
2864       return "An error occurred while loading or running the logon script.";
2865     case NERR_StandaloneLogon:
2866       return "The logon server was not specified. Your computer will be logged on as STANDALONE.";
2867     case NERR_LogonServerNotFound:
2868       return "The logon server could not be found.";
2869     case NERR_LogonDomainExists:
2870       return "There is already a logon domain for this computer.";
2871     case NERR_NonValidatedLogon:
2872       return "The logon server could not validate the logon.";
2873     case NERR_ACFNotFound:
2874       return "The security database could not be found.";
2875     case NERR_GroupNotFound:
2876       return "The group name could not be found.";
2877     case NERR_UserNotFound:
2878       return "The user name could not be found.";
2879     case NERR_ResourceNotFound:
2880       return "The resource name could not be found.";
2881     case NERR_GroupExists:
2882       return "The group already exists.";
2883     case NERR_UserExists:
2884       return "The user account already exists.";
2885     case NERR_ResourceExists:
2886       return "The resource permission list already exists.";
2887     case NERR_NotPrimary:
2888       return "This operation is only allowed on the primary domain controller of the domain.";
2889     case NERR_ACFNotLoaded:
2890       return "The security database has not been started.";
2891     case NERR_ACFNoRoom:
2892       return "There are too many names in the user accounts database.";
2893     case NERR_ACFFileIOFail:
2894       return "A disk I/O failure occurred.";
2895     case NERR_ACFTooManyLists:
2896       return "The limit of 64 entries per resource was exceeded.";
2897     case NERR_UserLogon:
2898       return "Deleting a user with a session is not allowed.";
2899     case NERR_ACFNoParent:
2900       return "The parent directory could not be located.";
2901     case NERR_CanNotGrowSegment:
2902       return "Unable to add to the security database session cache segment.";
2903     case NERR_SpeGroupOp:
2904       return "This operation is not allowed on this special group.";
2905     case NERR_NotInCache:
2906       return "This user is not cached in user accounts database session cache.";
2907     case NERR_UserInGroup:
2908       return "The user already belongs to this group.";
2909     case NERR_UserNotInGroup:
2910       return "The user does not belong to this group.";
2911     case NERR_AccountUndefined:
2912       return "This user account is undefined.";
2913     case NERR_AccountExpired:
2914       return "This user account has expired.";
2915     case NERR_InvalidWorkstation:
2916       return "The user is not allowed to log on from this workstation.";
2917     case NERR_InvalidLogonHours:
2918       return "The user is not allowed to log on at this time.";
2919     case NERR_PasswordExpired:
2920       return "The password of this user has expired.";
2921     case NERR_PasswordCantChange:
2922       return "The password of this user cannot change.";
2923     case NERR_PasswordHistConflict:
2924       return "This password cannot be used now.";
2925     case NERR_PasswordTooShort:
2926       return "The password does not meet the password policy requirements. Check the minimum password length, password complexity and password history requirements.";
2927     case NERR_PasswordTooRecent:
2928       return "The password of this user is too recent to change.";
2929     case NERR_InvalidDatabase:
2930       return "The security database is corrupted.";
2931     case NERR_DatabaseUpToDate:
2932       return "No updates are necessary to this replicant network/local security database.";
2933     case NERR_SyncRequired:
2934       return "This replicant database is outdated; synchronization is required.";
2935     case NERR_UseNotFound:
2936       return "The network connection could not be found.";
2937     case NERR_BadAsgType:
2938       return "This asg_type is invalid.";
2939     case NERR_DeviceIsShared:
2940       return "This device is currently being shared.";
2941     case NERR_NoComputerName:
2942       return "The computer name could not be added as a message alias. The name may already exist on the network.";
2943     case NERR_MsgAlreadyStarted:
2944       return "The Messenger service is already started.";
2945     case NERR_MsgInitFailed:
2946       return "The Messenger service failed to start.";
2947     case NERR_NameNotFound:
2948       return "The message alias could not be found on the network.";
2949     case NERR_AlreadyForwarded:
2950       return "This message alias has already been forwarded.";
2951     case NERR_AddForwarded:
2952       return "This message alias has been added but is still forwarded.";
2953     case NERR_AlreadyExists:
2954       return "This message alias already exists locally.";
2955     case NERR_TooManyNames:
2956       return "The maximum number of added message aliases has been exceeded.";
2957     case NERR_DelComputerName:
2958       return "The computer name could not be deleted.";
2959     case NERR_LocalForward:
2960       return "Messages cannot be forwarded back to the same workstation.";
2961     case NERR_GrpMsgProcessor:
2962       return "An error occurred in the domain message processor.";
2963     case NERR_PausedRemote:
2964       return "The message was sent, but the recipient has paused the Messenger service.";
2965     case NERR_BadReceive:
2966       return "The message was sent but not received.";
2967     case NERR_NameInUse:
2968       return "The message alias is currently in use. Try again later.";
2969     case NERR_MsgNotStarted:
2970       return "The Messenger service has not been started.";
2971     case NERR_NotLocalName:
2972       return "The name is not on the local computer.";
2973     case NERR_NoForwardName:
2974       return "The forwarded message alias could not be found on the network.";
2975     case NERR_RemoteFull:
2976       return "The message alias table on the remote station is full.";
2977     case NERR_NameNotForwarded:
2978       return "Messages for this alias are not currently being forwarded.";
2979     case NERR_TruncatedBroadcast:
2980       return "The broadcast message was truncated.";
2981     case NERR_InvalidDevice:
2982       return "This is an invalid device name.";
2983     case NERR_WriteFault:
2984       return "A write fault occurred.";
2985     case NERR_DuplicateName:
2986       return "A duplicate message alias exists on the network.";
2987     case NERR_DeleteLater:
2988       return "@W This message alias will be deleted later.";
2989     case NERR_IncompleteDel:
2990       return "The message alias was not successfully deleted from all networks.";
2991     case NERR_MultipleNets:
2992       return "This operation is not supported on computers with multiple networks.";
2993     case NERR_NetNameNotFound:
2994       return "This shared resource does not exist.";
2995     case NERR_DeviceNotShared:
2996       return "This device is not shared.";
2997     case NERR_ClientNameNotFound:
2998       return "A session does not exist with that computer name.";
2999     case NERR_FileIdNotFound:
3000       return "There is not an open file with that identification number.";
3001     case NERR_ExecFailure:
3002       return "A failure occurred when executing a remote administration command.";
3003     case NERR_TmpFile:
3004       return "A failure occurred when opening a remote temporary file.";
3005     case NERR_TooMuchData:
3006       return "The data returned from a remote administration command has been truncated to 64K.";
3007     case NERR_DeviceShareConflict:
3008       return "This device cannot be shared as both a spooled and a non-spooled resource.";
3009     case NERR_BrowserTableIncomplete:
3010       return "The information in the list of servers may be incorrect.";
3011     case NERR_NotLocalDomain:
3012       return "The computer is not active in this domain.";
3013 #ifdef NERR_IsDfsShare
3014
3015     case NERR_IsDfsShare:
3016       return "The share must be removed from the Distributed File System before it can be deleted.";
3017 #endif
3018
3019     case NERR_DevInvalidOpCode:
3020       return "The operation is invalid for this device.";
3021     case NERR_DevNotFound:
3022       return "This device cannot be shared.";
3023     case NERR_DevNotOpen:
3024       return "This device was not open.";
3025     case NERR_BadQueueDevString:
3026       return "This device name list is invalid.";
3027     case NERR_BadQueuePriority:
3028       return "The queue priority is invalid.";
3029     case NERR_NoCommDevs:
3030       return "There are no shared communication devices.";
3031     case NERR_QueueNotFound:
3032       return "The queue you specified does not exist.";
3033     case NERR_BadDevString:
3034       return "This list of devices is invalid.";
3035     case NERR_BadDev:
3036       return "The requested device is invalid.";
3037     case NERR_InUseBySpooler:
3038       return "This device is already in use by the spooler.";
3039     case NERR_CommDevInUse:
3040       return "This device is already in use as a communication device.";
3041     case NERR_InvalidComputer:
3042       return "This computer name is invalid.";
3043     case NERR_MaxLenExceeded:
3044       return "The string and prefix specified are too long.";
3045     case NERR_BadComponent:
3046       return "This path component is invalid.";
3047     case NERR_CantType:
3048       return "Could not determine the type of input.";
3049     case NERR_TooManyEntries:
3050       return "The buffer for types is not big enough.";
3051     case NERR_ProfileFileTooBig:
3052       return "Profile files cannot exceed 64K.";
3053     case NERR_ProfileOffset:
3054       return "The start offset is out of range.";
3055     case NERR_ProfileCleanup:
3056       return "The system cannot delete current connections to network resources.";
3057     case NERR_ProfileUnknownCmd:
3058       return "The system was unable to parse the command line in this file.";
3059     case NERR_ProfileLoadErr:
3060       return "An error occurred while loading the profile file.";
3061     case NERR_ProfileSaveErr:
3062       return "@W Errors occurred while saving the profile file. The profile was partially saved.";
3063     case NERR_LogOverflow:
3064       return "Log file %1 is full.";
3065     case NERR_LogFileChanged:
3066       return "This log file has changed between reads.";
3067     case NERR_LogFileCorrupt:
3068       return "Log file %1 is corrupt.";
3069     case NERR_SourceIsDir:
3070       return "The source path cannot be a directory.";
3071     case NERR_BadSource:
3072       return "The source path is illegal.";
3073     case NERR_BadDest:
3074       return "The destination path is illegal.";
3075     case NERR_DifferentServers:
3076       return "The source and destination paths are on different servers.";
3077     case NERR_RunSrvPaused:
3078       return "The Run server you requested is paused.";
3079     case NERR_ErrCommRunSrv:
3080       return "An error occurred when communicating with a Run server.";
3081     case NERR_ErrorExecingGhost:
3082       return "An error occurred when starting a background process.";
3083     case NERR_ShareNotFound:
3084       return "The shared resource you are connected to could not be found.";
3085     case NERR_InvalidLana:
3086       return "The LAN adapter number is invalid.";
3087     case NERR_OpenFiles:
3088       return "There are open files on the connection.";
3089     case NERR_ActiveConns:
3090       return "Active connections still exist.";
3091     case NERR_BadPasswordCore:
3092       return "This share name or password is invalid.";
3093     case NERR_DevInUse:
3094       return "The device is being accessed by an active process.";
3095     case NERR_LocalDrive:
3096       return "The drive letter is in use locally.";
3097     case NERR_AlertExists:
3098       return "The specified client is already registered for the specified event.";
3099     case NERR_TooManyAlerts:
3100       return "The alert table is full.";
3101     case NERR_NoSuchAlert:
3102       return "An invalid or nonexistent alert name was raised.";
3103     case NERR_BadRecipient:
3104       return "The alert recipient is invalid.";
3105     case NERR_AcctLimitExceeded:
3106       return "A user's session with this server has been deleted.";
3107     case NERR_InvalidLogSeek:
3108       return "The log file does not contain the requested record number.";
3109     case NERR_BadUasConfig:
3110       return "The user accounts database is not configured correctly.";
3111     case NERR_InvalidUASOp:
3112       return "This operation is not permitted when the Netlogon service is running.";
3113     case NERR_LastAdmin:
3114       return "This operation is not allowed on the last administrative account.";
3115     case NERR_DCNotFound:
3116       return "Could not find domain controller for this domain.";
3117     case NERR_LogonTrackingError:
3118       return "Could not set logon information for this user.";
3119     case NERR_NetlogonNotStarted:
3120       return "The Netlogon service has not been started.";
3121     case NERR_CanNotGrowUASFile:
3122       return "Unable to add to the user accounts database.";
3123     case NERR_TimeDiffAtDC:
3124       return "This server's clock is not synchronized with the primary domain controller's clock.";
3125     case NERR_PasswordMismatch:
3126       return "A password mismatch has been detected.";
3127     case NERR_NoSuchServer:
3128       return "The server identification does not specify a valid server.";
3129     case NERR_NoSuchSession:
3130       return "The session identification does not specify a valid session.";
3131     case NERR_NoSuchConnection:
3132       return "The connection identification does not specify a valid connection.";
3133     case NERR_TooManyServers:
3134       return "There is no space for another entry in the table of available servers.";
3135     case NERR_TooManySessions:
3136       return "The server has reached the maximum number of sessions it supports.";
3137     case NERR_TooManyConnections:
3138       return "The server has reached the maximum number of connections it supports.";
3139     case NERR_TooManyFiles:
3140       return "The server cannot open more files because it has reached its maximum number.";
3141     case NERR_NoAlternateServers:
3142       return "There are no alternate servers registered on this server.";
3143     case NERR_TryDownLevel:
3144       return "Try down-level (remote admin protocol) version of API instead.";
3145     case NERR_UPSDriverNotStarted:
3146       return "The UPS driver could not be accessed by the UPS service.";
3147     case NERR_UPSInvalidConfig:
3148       return "The UPS service is not configured correctly.";
3149     case NERR_UPSInvalidCommPort:
3150       return "The UPS service could not access the specified Comm Port.";
3151     case NERR_UPSSignalAsserted:
3152       return "The UPS indicated a line fail or low battery situation. Service not started.";
3153     case NERR_UPSShutdownFailed:
3154       return "The UPS service failed to perform a system shut down.";
3155     case NERR_BadDosRetCode:
3156       return "The program below returned an MS-DOS error code:";
3157     case NERR_ProgNeedsExtraMem:
3158       return "The program below needs more memory:";
3159     case NERR_BadDosFunction:
3160       return "The program below called an unsupported MS-DOS function:";
3161     case NERR_RemoteBootFailed:
3162       return "The workstation failed to boot.";
3163     case NERR_BadFileCheckSum:
3164       return "The file below is corrupt.";
3165     case NERR_NoRplBootSystem:
3166       return "No loader is specified in the boot-block definition file.";
3167     case NERR_RplLoadrNetBiosErr:
3168       return "NetBIOS returned an error:      The NCB and SMB are dumped above.";
3169     case NERR_RplLoadrDiskErr:
3170       return "A disk I/O error occurred.";
3171     case NERR_ImageParamErr:
3172       return "Image parameter substitution failed.";
3173     case NERR_TooManyImageParams:
3174       return "Too many image parameters cross disk sector boundaries.";
3175     case NERR_NonDosFloppyUsed:
3176       return "The image was not generated from an MS-DOS diskette formatted with /S.";
3177     case NERR_RplBootRestart:
3178       return "Remote boot will be restarted later.";
3179     case NERR_RplSrvrCallFailed:
3180       return "The call to the Remoteboot server failed.";
3181     case NERR_CantConnectRplSrvr:
3182       return "Cannot connect to the Remoteboot server.";
3183     case NERR_CantOpenImageFile:
3184       return "Cannot open image file on the Remoteboot server.";
3185     case NERR_CallingRplSrvr:
3186       return "Connecting to the Remoteboot server...";
3187     case NERR_StartingRplBoot:
3188       return "Connecting to the Remoteboot server...";
3189     case NERR_RplBootServiceTerm:
3190       return "Remote boot service was stopped; check the error log for the cause of the problem.";
3191     case NERR_RplBootStartFailed:
3192       return "Remote boot startup failed; check the error log for the cause of the problem.";
3193     case NERR_RPL_CONNECTED:
3194       return "A second connection to a Remoteboot resource is not allowed.";
3195     case NERR_BrowserConfiguredToNotRun:
3196       return "The browser service was configured with MaintainServerList=No.";
3197     case NERR_RplNoAdaptersStarted:
3198       return "Service failed to start since none of the network adapters started with this service.";
3199     case NERR_RplBadRegistry:
3200       return "Service failed to start due to bad startup information in the registry.";
3201     case NERR_RplBadDatabase:
3202       return "Service failed to start because its database is absent or corrupt.";
3203     case NERR_RplRplfilesShare:
3204       return "Service failed to start because RPLFILES share is absent.";
3205     case NERR_RplNotRplServer:
3206       return "Service failed to start because RPLUSER group is absent.";
3207     case NERR_RplCannotEnum:
3208       return "Cannot enumerate service records.";
3209     case NERR_RplWkstaInfoCorrupted:
3210       return "Workstation record information has been corrupted.";
3211     case NERR_RplWkstaNotFound:
3212       return "Workstation record was not found.";
3213     case NERR_RplWkstaNameUnavailable:
3214       return "Workstation name is in use by some other workstation.";
3215     case NERR_RplProfileInfoCorrupted:
3216       return "Profile record information has been corrupted.";
3217     case NERR_RplProfileNotFound:
3218       return "Profile record was not found.";
3219     case NERR_RplProfileNameUnavailable:
3220       return "Profile name is in use by some other profile.";
3221     case NERR_RplProfileNotEmpty:
3222       return "There are workstations using this profile.";
3223     case NERR_RplConfigInfoCorrupted:
3224       return "Configuration record information has been corrupted.";
3225     case NERR_RplConfigNotFound:
3226       return "Configuration record was not found.";
3227     case NERR_RplAdapterInfoCorrupted:
3228       return "Adapter ID record information has been corrupted.";
3229     case NERR_RplInternal:
3230       return "An internal service error has occurred.";
3231     case NERR_RplVendorInfoCorrupted:
3232       return "Vendor ID record information has been corrupted.";
3233     case NERR_RplBootInfoCorrupted:
3234       return "Boot block record information has been corrupted.";
3235     case NERR_RplWkstaNeedsUserAcct:
3236       return "The user account for this workstation record is missing.";
3237     case NERR_RplNeedsRPLUSERAcct:
3238       return "The RPLUSER local group could not be found.";
3239     case NERR_RplBootNotFound:
3240       return "Boot block record was not found.";
3241     case NERR_RplIncompatibleProfile:
3242       return "Chosen profile is incompatible with this workstation.";
3243     case NERR_RplAdapterNameUnavailable:
3244       return "Chosen network adapter ID is in use by some other workstation.";
3245     case NERR_RplConfigNotEmpty:
3246       return "There are profiles using this configuration.";
3247     case NERR_RplBootInUse:
3248       return "There are workstations, profiles, or configurations using this boot block.";
3249     case NERR_RplBackupDatabase:
3250       return "Service failed to backup Remoteboot database.";
3251     case NERR_RplAdapterNotFound:
3252       return "Adapter record was not found.";
3253     case NERR_RplVendorNotFound:
3254       return "Vendor record was not found.";
3255     case NERR_RplVendorNameUnavailable:
3256       return "Vendor name is in use by some other vendor record.";
3257     case NERR_RplBootNameUnavailable:
3258       return "(boot name, vendor ID) is in use by some other boot block record.";
3259     case NERR_RplConfigNameUnavailable:
3260       return "Configuration name is in use by some other configuration.";
3261     case NERR_DfsInternalCorruption:
3262       return "The internal database maintained by the Dfs service is corrupt.";
3263     case NERR_DfsVolumeDataCorrupt:
3264       return "One of the records in the internal Dfs database is corrupt.";
3265     case NERR_DfsNoSuchVolume:
3266       return "There is no DFS name whose entry path matches the input Entry Path.";
3267     case NERR_DfsVolumeAlreadyExists:
3268       return "A root or link with the given name already exists.";
3269     case NERR_DfsAlreadyShared:
3270       return "The server share specified is already shared in the Dfs.";
3271     case NERR_DfsNoSuchShare:
3272       return "The indicated server share does not support the indicated DFS namespace.";
3273     case NERR_DfsNotALeafVolume:
3274       return "The operation is not valid on this portion of the namespace.";
3275     case NERR_DfsLeafVolume:
3276       return "The operation is not valid on this portion of the namespace.";
3277     case NERR_DfsVolumeHasMultipleServers:
3278       return "The operation is ambiguous because the link has multiple servers.";
3279     case NERR_DfsCantCreateJunctionPoint:
3280       return "Unable to create a link.";
3281     case NERR_DfsServerNotDfsAware:
3282       return "The server is not Dfs Aware.";
3283     case NERR_DfsBadRenamePath:
3284       return "The specified rename target path is invalid.";
3285     case NERR_DfsVolumeIsOffline:
3286       return "The specified DFS link is offline.";
3287     case NERR_DfsNoSuchServer:
3288       return "The specified server is not a server for this link.";
3289     case NERR_DfsCyclicalName:
3290       return "A cycle in the Dfs name was detected.";
3291     case NERR_DfsNotSupportedInServerDfs:
3292       return "The operation is not supported on a server-based Dfs.";
3293     case NERR_DfsDuplicateService:
3294       return "This link is already supported by the specified server-share.";
3295     case NERR_DfsCantRemoveLastServerShare:
3296       return "Can't remove the last server-share supporting this root or link.";
3297     case NERR_DfsVolumeIsInterDfs:
3298       return "The operation is not supported for an Inter-DFS link.";
3299     case NERR_DfsInconsistent:
3300       return "The internal state of the Dfs Service has become inconsistent.";
3301     case NERR_DfsServerUpgraded:
3302       return "The Dfs Service has been installed on the specified server.";
3303     case NERR_DfsDataIsIdentical:
3304       return "The Dfs data being reconciled is identical.";
3305     case NERR_DfsCantRemoveDfsRoot:
3306       return "The DFS root cannot be deleted. Uninstall DFS if required.";
3307     case NERR_DfsChildOrParentInDfs:
3308       return "A child or parent directory of the share is already in a Dfs.";
3309     case NERR_DfsInternalError:
3310       return "Dfs internal error.";
3311       /* the following are not defined in mingw */
3312 #if 0
3313
3314     case NERR_SetupAlreadyJoined:
3315       return "This machine is already joined to a domain.";
3316     case NERR_SetupNotJoined:
3317       return "This machine is not currently joined to a domain.";
3318     case NERR_SetupDomainController:
3319       return "This machine is a domain controller and cannot be unjoined from a domain.";
3320     case NERR_DefaultJoinRequired:
3321       return "The destination domain controller does not support creating machine accounts in OUs.";
3322     case NERR_InvalidWorkgroupName:
3323       return "The specified workgroup name is invalid.";
3324     case NERR_NameUsesIncompatibleCodePage:
3325       return "The specified computer name is incompatible with the default language used on the domain controller.";
3326     case NERR_ComputerAccountNotFound:
3327       return "The specified computer account could not be found.";
3328     case NERR_PersonalSku:
3329       return "This version of Windows cannot be joined to a domain.";
3330     case NERR_PasswordMustChange:
3331       return "The password must change at the next logon.";
3332     case NERR_AccountLockedOut:
3333       return "The account is locked out.";
3334     case NERR_PasswordTooLong:
3335       return "The password is too long.";
3336     case NERR_PasswordNotComplexEnough:
3337       return "The password does not meet the complexity policy.";
3338     case NERR_PasswordFilterError:
3339       return "The password does not meet the requirements of the password filter DLLs.";
3340 #endif
3341
3342     }
3343   msg = strerror (error_number);
3344   if (msg == NULL)
3345     msg = "unknown";
3346
3347   return msg;
3348 #endif //DBUS_WINCE
3349 }
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359 /******************************************************************************
3360  
3361 Original CVS version of dbus-sysdeps.c
3362  
3363 ******************************************************************************/
3364 /* -*- mode: C; c-file-style: "gnu" -*- */
3365 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-Bus implementation)
3366  * 
3367  * Copyright (C) 2002, 2003  Red Hat, Inc.
3368  * Copyright (C) 2003 CodeFactory AB
3369  * Copyright (C) 2005 Novell, Inc.
3370  *
3371  * Licensed under the Academic Free License version 2.1
3372  * 
3373  * This program is free software; you can redistribute it and/or modify
3374  * it under the terms of the GNU General Public License as published by
3375  * the Free Software Foundation; either version 2 of the License, or
3376  * (at your option) any later version.
3377  *
3378  * This program is distributed in the hope that it will be useful,
3379  * but WITHOUT ANY WARRANTY; without even the implied warranty of
3380  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3381  * GNU General Public License for more details.
3382  * 
3383  * You should have received a copy of the GNU General Public License
3384  * along with this program; if not, write to the Free Software
3385  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
3386  *
3387  */
3388
3389
3390 /**
3391  * @addtogroup DBusInternalsUtils
3392  * @{
3393  */
3394
3395 int _dbus_mkdir (const char *path,
3396                  mode_t mode)
3397 {
3398   return _mkdir(path);
3399 }
3400
3401 /**
3402  * Exit the process, returning the given value.
3403  *
3404  * @param code the exit code
3405  */
3406 void
3407 _dbus_exit (int code)
3408 {
3409   _exit (code);
3410 }
3411
3412 /**
3413  * Creates a socket and connects to a socket at the given host 
3414  * and port. The connection fd is returned, and is set up as
3415  * nonblocking.
3416  *
3417  * @param host the host name to connect to, NULL for loopback
3418  * @param port the prot to connect to
3419  * @param error return location for error code
3420  * @returns connection file descriptor or -1 on error
3421  */
3422 int
3423 _dbus_connect_tcp_socket (const char     *host,
3424                           dbus_uint32_t   port,
3425                           DBusError      *error)
3426 {
3427   DBusSocket s;
3428   int handle;
3429   struct sockaddr_in addr;
3430   struct hostent *he;
3431   struct in_addr *haddr;
3432   struct in_addr ina;
3433
3434   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3435
3436   _dbus_win_startup_winsock ();
3437
3438   s.fd = socket (AF_INET, SOCK_STREAM, 0);
3439
3440   if (DBUS_SOCKET_IS_INVALID (s.fd))
3441     {
3442       DBUS_SOCKET_SET_ERRNO ();
3443       dbus_set_error (error,
3444                       _dbus_error_from_errno (errno),
3445                       "Failed to create socket: %s",
3446                       _dbus_strerror (errno));
3447
3448       return -1;
3449     }
3450
3451   if (host == NULL)
3452     {
3453       host = "localhost";
3454       ina.s_addr = htonl (INADDR_LOOPBACK);
3455       haddr = &ina;
3456     }
3457
3458   he = gethostbyname (host);
3459   if (he == NULL)
3460     {
3461       DBUS_SOCKET_SET_ERRNO ();
3462       dbus_set_error (error,
3463                       _dbus_error_from_errno (errno),
3464                       "Failed to lookup hostname: %s",
3465                       host);
3466       DBUS_CLOSE_SOCKET (s.fd);
3467       return -1;
3468     }
3469
3470   haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3471
3472   _DBUS_ZERO (addr);
3473   memcpy (&addr.sin_addr, haddr, sizeof(struct in_addr));
3474   addr.sin_family = AF_INET;
3475   addr.sin_port = htons (port);
3476
3477   if (DBUS_SOCKET_API_RETURNS_ERROR
3478       (connect (s.fd, (struct sockaddr*) &addr, sizeof (addr)) < 0))
3479     {
3480       DBUS_SOCKET_SET_ERRNO ();
3481       dbus_set_error (error,
3482                       _dbus_error_from_errno (errno),
3483                       "Failed to connect to socket %s:%d %s",
3484                       host, port, _dbus_strerror (errno));
3485
3486       DBUS_CLOSE_SOCKET (s.fd);
3487       s.fd = -1;
3488
3489       return -1;
3490     }
3491
3492   handle = _dbus_socket_to_handle (&s);
3493
3494   if (!_dbus_set_fd_nonblocking (handle, error))
3495     {
3496       _dbus_close_socket (handle, NULL);
3497       handle = -1;
3498
3499       return -1;
3500     }
3501
3502   return handle;
3503 }
3504
3505 void
3506 _dbus_daemon_init(const char *host, dbus_uint32_t port);
3507 /**
3508  * Creates a socket and binds it to the given port,
3509  * then listens on the socket. The socket is
3510  * set to be nonblocking. 
3511  * In case of port=0 a random free port is used and 
3512  * returned in the port parameter. 
3513  *
3514  * @param host the interface to listen on, NULL for loopback, empty for any
3515  * @param port the port to listen on, if zero a free port will be used 
3516  * @param error return location for errors
3517  * @returns the listening file descriptor or -1 on error
3518  */
3519
3520 int
3521 _dbus_listen_tcp_socket (const char     *host,
3522                          dbus_uint32_t  *port,
3523                          DBusError      *error)
3524 {
3525   DBusSocket slisten;
3526   int handle;
3527   struct sockaddr_in addr;
3528   struct hostent *he;
3529   struct in_addr *haddr;
3530   socklen_t len = (socklen_t) sizeof (struct sockaddr);
3531   struct in_addr ina;
3532
3533
3534   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3535
3536   _dbus_win_startup_winsock ();
3537
3538   slisten.fd = socket (AF_INET, SOCK_STREAM, 0);
3539
3540   if (DBUS_SOCKET_IS_INVALID (slisten.fd))
3541     {
3542       DBUS_SOCKET_SET_ERRNO ();
3543       dbus_set_error (error, _dbus_error_from_errno (errno),
3544                       "Failed to create socket \"%s:%d\": %s",
3545                       host, port, _dbus_strerror (errno));
3546       return -1;
3547     }
3548   if (host == NULL)
3549     {
3550       host = "localhost";
3551       ina.s_addr = htonl (INADDR_LOOPBACK);
3552       haddr = &ina;
3553     }
3554   else if (!host[0])
3555     {
3556       ina.s_addr = htonl (INADDR_ANY);
3557       haddr = &ina;
3558     }
3559   else
3560     {
3561       he = gethostbyname (host);
3562       if (he == NULL)
3563         {
3564           DBUS_SOCKET_SET_ERRNO ();
3565           dbus_set_error (error,
3566                           _dbus_error_from_errno (errno),
3567                           "Failed to lookup hostname: %s",
3568                           host);
3569           DBUS_CLOSE_SOCKET (slisten.fd);
3570           return -1;
3571         }
3572
3573       haddr = ((struct in_addr *) (he->h_addr_list)[0]);
3574     }
3575
3576   _DBUS_ZERO (addr);
3577   memcpy (&addr.sin_addr, haddr, sizeof (struct in_addr));
3578   addr.sin_family = AF_INET;
3579   addr.sin_port = htons (*port);
3580
3581   if (bind (slisten.fd, (struct sockaddr*) &addr, sizeof (struct sockaddr)))
3582     {
3583       DBUS_SOCKET_SET_ERRNO ();
3584       dbus_set_error (error, _dbus_error_from_errno (errno),
3585                       "Failed to bind socket \"%s:%d\": %s",
3586                       host, *port, _dbus_strerror (errno));
3587       DBUS_CLOSE_SOCKET (slisten.fd);
3588       return -1;
3589     }
3590
3591   if (DBUS_SOCKET_API_RETURNS_ERROR (listen (slisten.fd, 30 /* backlog */)))
3592     {
3593       DBUS_SOCKET_SET_ERRNO ();
3594       dbus_set_error (error, _dbus_error_from_errno (errno),
3595                       "Failed to listen on socket \"%s:%d\": %s",
3596                       host, *port, _dbus_strerror (errno));
3597       DBUS_CLOSE_SOCKET (slisten.fd);
3598       return -1;
3599     }
3600
3601   getsockname(slisten.fd, (struct sockaddr*) &addr, &len);
3602   *port = (dbus_uint32_t) ntohs(addr.sin_port);
3603   
3604   _dbus_daemon_init(host, ntohs(addr.sin_port));
3605
3606   handle = _dbus_socket_to_handle (&slisten);
3607
3608   if (!_dbus_set_fd_nonblocking (handle, error))
3609     {
3610       _dbus_close_socket (handle, NULL);
3611       return -1;
3612     }
3613
3614   return handle;
3615 }
3616
3617 /**
3618  * Accepts a connection on a listening socket.
3619  * Handles EINTR for you.
3620  *
3621  * @param listen_fd the listen file descriptor
3622  * @returns the connection fd of the client, or -1 on error
3623  */
3624 int
3625 _dbus_accept  (int listen_handle)
3626 {
3627   DBusSocket *slisten;
3628   DBusSocket sclient;
3629   struct sockaddr addr;
3630   socklen_t addrlen;
3631
3632   _dbus_handle_to_socket(listen_handle, &slisten);
3633
3634   addrlen = sizeof (addr);
3635
3636   //FIXME:  why do we not try it again on Windows?
3637 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3638 retry:
3639 #endif
3640
3641   sclient.fd = accept (slisten->fd, &addr, &addrlen);
3642
3643   if (DBUS_SOCKET_IS_INVALID (sclient.fd))
3644     {
3645       DBUS_SOCKET_SET_ERRNO ();
3646 #if !defined(DBUS_WIN) && !defined(DBUS_WINCE)
3647       if (errno == EINTR)
3648         goto retry;
3649 #else
3650       return -1;
3651 #endif
3652     }
3653
3654   return _dbus_socket_to_handle (&sclient);
3655 }
3656
3657
3658
3659 dbus_bool_t
3660 write_credentials_byte (int             server_fd,
3661                         DBusError      *error)
3662 {
3663   /* FIXME: for the session bus credentials shouldn't matter (?), but
3664    * for the system bus they are presumably essential. A rough outline
3665    * of a way to implement the credential transfer would be this:
3666    *
3667    * client waits to *read* a byte.
3668    *
3669    * server creates a named pipe with a random name, sends a byte
3670    * contining its length, and its name.
3671    *
3672    * client reads the name, connects to it (using Win32 API).
3673    *
3674    * server waits for connection to the named pipe, then calls
3675    * ImpersonateNamedPipeClient(), notes its now-current credentials,
3676    * calls RevertToSelf(), closes its handles to the named pipe, and
3677    * is done. (Maybe there is some other way to get the SID of a named
3678    * pipe client without having to use impersonation?)
3679    *
3680    * client closes its handles and is done.
3681    *
3682    */
3683
3684   return TRUE;
3685 }
3686
3687 /**
3688  * Reads a single byte which must be nul (an error occurs otherwise),
3689  * and reads unix credentials if available. Fills in pid/uid/gid with
3690  * -1 if no credentials are available. Return value indicates whether
3691  * a byte was read, not whether we got valid credentials. On some
3692  * systems, such as Linux, reading/writing the byte isn't actually
3693  * required, but we do it anyway just to avoid multiple codepaths.
3694  * 
3695  * Fails if no byte is available, so you must select() first.
3696  *
3697  * The point of the byte is that on some systems we have to
3698  * use sendmsg()/recvmsg() to transmit credentials.
3699  *
3700  * @param client_fd the client file descriptor
3701  * @param credentials struct to fill with credentials of client
3702  * @param error location to store error code
3703  * @returns #TRUE on success
3704  */
3705 dbus_bool_t
3706 _dbus_read_credentials_unix_socket  (int              client_fd,
3707                                      DBusCredentials *credentials,
3708                                      DBusError       *error)
3709 {
3710   /* FIXME bogus testing credentials */
3711   _dbus_credentials_from_current_process (credentials);
3712
3713   return TRUE;
3714 }
3715
3716 /**
3717 * Checks to make sure the given directory is 
3718 * private to the user 
3719 *
3720 * @param dir the name of the directory
3721 * @param error error return
3722 * @returns #FALSE on failure
3723 **/
3724 dbus_bool_t
3725 _dbus_check_dir_is_private_to_user (DBusString *dir, DBusError *error)
3726 {
3727   const char *directory;
3728   struct stat sb;
3729
3730   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3731
3732   return TRUE;
3733 }
3734
3735
3736 /**
3737  * Gets user info for the given user ID.
3738  *
3739  * @param info user info object to initialize
3740  * @param uid the user ID
3741  * @param error error return
3742  * @returns #TRUE on success
3743  */
3744 dbus_bool_t
3745 _dbus_user_info_fill_uid (DBusUserInfo *info,
3746                           dbus_uid_t    uid,
3747                           DBusError    *error)
3748 {
3749   return fill_user_info (info, uid,
3750                          NULL, error);
3751 }
3752
3753 /**
3754  * Gets user info for the given username.
3755  *
3756  * @param info user info object to initialize
3757  * @param username the username
3758  * @param error error return
3759  * @returns #TRUE on success
3760  */
3761 dbus_bool_t
3762 _dbus_user_info_fill (DBusUserInfo     *info,
3763                       const DBusString *username,
3764                       DBusError        *error)
3765 {
3766   return fill_user_info (info, DBUS_UID_UNSET,
3767                          username, error);
3768 }
3769
3770
3771 dbus_bool_t
3772 fill_user_info (DBusUserInfo       *info,
3773                 dbus_uid_t          uid,
3774                 const DBusString   *username,
3775                 DBusError          *error)
3776 {
3777   const char *username_c;
3778
3779   /* exactly one of username/uid provided */
3780   _dbus_assert (username != NULL || uid != DBUS_UID_UNSET);
3781   _dbus_assert (username == NULL || uid == DBUS_UID_UNSET);
3782
3783   info->uid = DBUS_UID_UNSET;
3784   info->primary_gid = DBUS_GID_UNSET;
3785   info->group_ids = NULL;
3786   info->n_group_ids = 0;
3787   info->username = NULL;
3788   info->homedir = NULL;
3789
3790   if (username != NULL)
3791     username_c = _dbus_string_get_const_data (username);
3792   else
3793     username_c = NULL;
3794
3795   if (uid != DBUS_UID_UNSET)
3796     {
3797       if (!fill_win_user_info_from_uid (uid, info, error))
3798         {
3799           _dbus_verbose("%s after fill_win_user_info_from_uid\n",__FUNCTION__);
3800           return FALSE;
3801         }
3802     }
3803   else
3804     {
3805       wchar_t *wname = _dbus_win_utf8_to_utf16 (username_c, error);
3806
3807       if (!wname)
3808         return FALSE;
3809
3810       if (!fill_win_user_info_from_name (wname, info, error))
3811         {
3812           dbus_free (wname);
3813           return FALSE;
3814         }
3815       dbus_free (wname);
3816     }
3817
3818   return TRUE;
3819 }
3820
3821
3822 /**
3823  * Appends the given filename to the given directory.
3824  *
3825  * @todo it might be cute to collapse multiple '/' such as "foo//"
3826  * concat "//bar"
3827  *
3828  * @param dir the directory name
3829  * @param next_component the filename
3830  * @returns #TRUE on success
3831  */
3832 dbus_bool_t
3833 _dbus_concat_dir_and_file (DBusString       *dir,
3834                            const DBusString *next_component)
3835 {
3836   dbus_bool_t dir_ends_in_slash;
3837   dbus_bool_t file_starts_with_slash;
3838
3839   if (_dbus_string_get_length (dir) == 0 ||
3840       _dbus_string_get_length (next_component) == 0)
3841     return TRUE;
3842
3843   dir_ends_in_slash =
3844     ('/' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1) ||
3845      '\\' == _dbus_string_get_byte (dir, _dbus_string_get_length (dir) - 1));
3846
3847   file_starts_with_slash =
3848     ('/' == _dbus_string_get_byte (next_component, 0) ||
3849      '\\' == _dbus_string_get_byte (next_component, 0));
3850
3851   if (dir_ends_in_slash && file_starts_with_slash)
3852     {
3853       _dbus_string_shorten (dir, 1);
3854     }
3855   else if (!(dir_ends_in_slash || file_starts_with_slash))
3856     {
3857       if (!_dbus_string_append_byte (dir, '\\'))
3858         return FALSE;
3859     }
3860
3861   return _dbus_string_copy (next_component, 0, dir,
3862                             _dbus_string_get_length (dir));
3863 }
3864
3865
3866
3867
3868 /**
3869  * Gets our process ID
3870  * @returns process ID
3871  */
3872 unsigned long
3873 _dbus_getpid (void)
3874 {
3875   return GetCurrentProcessId ();
3876 }
3877
3878 /** nanoseconds in a second */
3879 #define NANOSECONDS_PER_SECOND       1000000000
3880 /** microseconds in a second */
3881 #define MICROSECONDS_PER_SECOND      1000000
3882 /** milliseconds in a second */
3883 #define MILLISECONDS_PER_SECOND      1000
3884 /** nanoseconds in a millisecond */
3885 #define NANOSECONDS_PER_MILLISECOND  1000000
3886 /** microseconds in a millisecond */
3887 #define MICROSECONDS_PER_MILLISECOND 1000
3888
3889 /**
3890  * Sleeps the given number of milliseconds.
3891  * @param milliseconds number of milliseconds
3892  */
3893 void
3894 _dbus_sleep_milliseconds (int milliseconds)
3895 {
3896   Sleep (milliseconds);
3897 }
3898
3899
3900 /**
3901  * Get current time, as in gettimeofday().
3902  *
3903  * @param tv_sec return location for number of seconds
3904  * @param tv_usec return location for number of microseconds
3905  */
3906 void
3907 _dbus_get_current_time (long *tv_sec,
3908                         long *tv_usec)
3909 {
3910   FILETIME ft;
3911   dbus_uint64_t *time64 = (dbus_uint64_t *) &ft;
3912
3913   GetSystemTimeAsFileTime (&ft);
3914
3915   /* Convert from 100s of nanoseconds since 1601-01-01
3916   * to Unix epoch. Yes, this is Y2038 unsafe.
3917   */
3918   *time64 -= DBUS_INT64_CONSTANT (116444736000000000);
3919   *time64 /= 10;
3920
3921   if (tv_sec)
3922     *tv_sec = *time64 / 1000000;
3923
3924   if (tv_usec)
3925     *tv_usec = *time64 % 1000000;
3926 }
3927
3928
3929 /**
3930  * signal (SIGPIPE, SIG_IGN);
3931  */
3932 void
3933 _dbus_disable_sigpipe (void)
3934 {
3935     _dbus_verbose("FIXME: implement _dbus_disable_sigpipe (void)\n");
3936 }
3937
3938 /**
3939  * Gets the credentials of the current process.
3940  *
3941  * @param credentials credentials to fill in.
3942  */
3943 void
3944 _dbus_credentials_from_current_process (DBusCredentials *credentials)
3945 {
3946   credentials->pid = _dbus_getpid ();
3947   credentials->uid = _dbus_getuid ();
3948   credentials->gid = _dbus_getgid ();
3949 }
3950
3951 /**
3952  * Appends the contents of the given file to the string,
3953  * returning error code. At the moment, won't open a file
3954  * more than a megabyte in size.
3955  *
3956  * @param str the string to append to
3957  * @param filename filename to load
3958  * @param error place to set an error
3959  * @returns #FALSE if error was set
3960  */
3961 dbus_bool_t
3962 _dbus_file_get_contents (DBusString       *str,
3963                          const DBusString *filename,
3964                          DBusError        *error)
3965 {
3966   DBusFile file;
3967   struct stat sb;
3968   int orig_len;
3969   int total;
3970   const char *filename_c;
3971
3972   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
3973
3974   filename_c = _dbus_string_get_const_data (filename);
3975
3976   /* O_BINARY useful on Cygwin and Win32 */
3977   if (!_dbus_open_file (&file, filename_c, O_RDONLY | O_BINARY, -1))
3978     {
3979       dbus_set_error (error, _dbus_error_from_errno (errno),
3980                       "Failed to open \"%s\": %s",
3981                       filename_c,
3982                       _dbus_strerror (errno));
3983       return FALSE;
3984     }
3985
3986   if (!_dbus_fstat (&file, &sb))
3987     {
3988       dbus_set_error (error, _dbus_error_from_errno (errno),
3989                       "Failed to stat \"%s\": %s",
3990                       filename_c,
3991                       _dbus_strerror (errno));
3992
3993       _dbus_verbose ("fstat() failed: %s",
3994                      _dbus_strerror (errno));
3995
3996       _dbus_close_file (&file, NULL);
3997
3998       return FALSE;
3999     }
4000
4001   if (sb.st_size > _DBUS_ONE_MEGABYTE)
4002     {
4003       dbus_set_error (error, DBUS_ERROR_FAILED,
4004                       "File size %lu of \"%s\" is too large.",
4005                       (unsigned long) sb.st_size, filename_c);
4006       _dbus_close_file (&file, NULL);
4007       return FALSE;
4008     }
4009
4010   total = 0;
4011   orig_len = _dbus_string_get_length (str);
4012   if (sb.st_size > 0 && S_ISREG (sb.st_mode))
4013     {
4014       int bytes_read;
4015
4016       while (total < (int) sb.st_size)
4017         {
4018           bytes_read = _dbus_read_file (&file, str,
4019                                         sb.st_size - total);
4020           if (bytes_read <= 0)
4021             {
4022               dbus_set_error (error, _dbus_error_from_errno (errno),
4023                               "Error reading \"%s\": %s",
4024                               filename_c,
4025                               _dbus_strerror (errno));
4026
4027               _dbus_verbose ("read() failed: %s",
4028                              _dbus_strerror (errno));
4029
4030               _dbus_close_file (&file, NULL);
4031               _dbus_string_set_length (str, orig_len);
4032               return FALSE;
4033             }
4034           else
4035             total += bytes_read;
4036         }
4037
4038       _dbus_close_file (&file, NULL);
4039       return TRUE;
4040     }
4041   else if (sb.st_size != 0)
4042     {
4043       _dbus_verbose ("Can only open regular files at the moment.\n");
4044       dbus_set_error (error, DBUS_ERROR_FAILED,
4045                       "\"%s\" is not a regular file",
4046                       filename_c);
4047       _dbus_close_file (&file, NULL);
4048       return FALSE;
4049     }
4050   else
4051     {
4052       _dbus_close_file (&file, NULL);
4053       return TRUE;
4054     }
4055 }
4056
4057 /**
4058  * Writes a string out to a file. If the file exists,
4059  * it will be atomically overwritten by the new data.
4060  *
4061  * @param str the string to write out
4062  * @param filename the file to save string to
4063  * @param error error to be filled in on failure
4064  * @returns #FALSE on failure
4065  */
4066 dbus_bool_t
4067 _dbus_string_save_to_file (const DBusString *str,
4068                            const DBusString *filename,
4069                            DBusError        *error)
4070 {
4071   DBusFile file;
4072   int bytes_to_write;
4073   const char *filename_c;
4074   DBusString tmp_filename;
4075   const char *tmp_filename_c;
4076   int total;
4077   dbus_bool_t need_unlink;
4078   dbus_bool_t retval;
4079
4080   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4081
4082   retval = FALSE;
4083   need_unlink = FALSE;
4084
4085   if (!_dbus_string_init (&tmp_filename))
4086     {
4087       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4088       return FALSE;
4089     }
4090
4091   if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
4092     {
4093       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4094       _dbus_string_free (&tmp_filename);
4095       return FALSE;
4096     }
4097
4098   if (!_dbus_string_append (&tmp_filename, "."))
4099     {
4100       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4101       _dbus_string_free (&tmp_filename);
4102       return FALSE;
4103     }
4104
4105 #define N_TMP_FILENAME_RANDOM_BYTES 8
4106   if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
4107     {
4108       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
4109       _dbus_string_free (&tmp_filename);
4110       return FALSE;
4111     }
4112
4113   filename_c = _dbus_string_get_const_data (filename);
4114   tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
4115
4116   if (!_dbus_open_file (&file, tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4117                         0600))
4118     {
4119       dbus_set_error (error, _dbus_error_from_errno (errno),
4120                       "Could not create %s: %s", tmp_filename_c,
4121                       _dbus_strerror (errno));
4122       goto out;
4123     }
4124
4125   need_unlink = TRUE;
4126
4127   total = 0;
4128   bytes_to_write = _dbus_string_get_length (str);
4129
4130   while (total < bytes_to_write)
4131     {
4132       int bytes_written;
4133
4134       bytes_written = _dbus_write_file (&file, str, total,
4135                                         bytes_to_write - total);
4136
4137       if (bytes_written <= 0)
4138         {
4139           dbus_set_error (error, _dbus_error_from_errno (errno),
4140                           "Could not write to %s: %s", tmp_filename_c,
4141                           _dbus_strerror (errno));
4142
4143           goto out;
4144         }
4145
4146       total += bytes_written;
4147     }
4148
4149   if (!_dbus_close_file (&file, NULL))
4150     {
4151       dbus_set_error (error, _dbus_error_from_errno (errno),
4152                       "Could not close file %s: %s",
4153                       tmp_filename_c, _dbus_strerror (errno));
4154
4155       goto out;
4156     }
4157
4158
4159   if ((unlink (filename_c) == -1 && errno != ENOENT) ||
4160        rename (tmp_filename_c, filename_c) < 0)
4161     {
4162       dbus_set_error (error, _dbus_error_from_errno (errno),
4163                       "Could not rename %s to %s: %s",
4164                       tmp_filename_c, filename_c,
4165                       _dbus_strerror (errno));
4166
4167       goto out;
4168     }
4169
4170   need_unlink = FALSE;
4171
4172   retval = TRUE;
4173
4174 out:
4175   /* close first, then unlink, to prevent ".nfs34234235" garbage
4176    * files
4177    */
4178
4179   if (_dbus_is_valid_file(&file))
4180     _dbus_close_file (&file, NULL);
4181
4182   if (need_unlink && unlink (tmp_filename_c) < 0)
4183     _dbus_verbose ("Failed to unlink temp file %s: %s\n",
4184                    tmp_filename_c, _dbus_strerror (errno));
4185
4186   _dbus_string_free (&tmp_filename);
4187
4188   if (!retval)
4189     _DBUS_ASSERT_ERROR_IS_SET (error);
4190
4191   return retval;
4192 }
4193
4194
4195 /** Creates the given file, failing if the file already exists.
4196  *
4197  * @param filename the filename
4198  * @param error error location
4199  * @returns #TRUE if we created the file and it didn't exist
4200  */
4201 dbus_bool_t
4202 _dbus_create_file_exclusively (const DBusString *filename,
4203                                DBusError        *error)
4204 {
4205   DBusFile file;
4206   const char *filename_c;
4207
4208   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4209
4210   filename_c = _dbus_string_get_const_data (filename);
4211
4212   if (!_dbus_open_file (&file, filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
4213                         0600))
4214     {
4215       dbus_set_error (error,
4216                       DBUS_ERROR_FAILED,
4217                       "Could not create file %s: %s\n",
4218                       filename_c,
4219                       _dbus_strerror (errno));
4220       return FALSE;
4221     }
4222
4223   if (!_dbus_close_file (&file, NULL))
4224     {
4225       dbus_set_error (error,
4226                       DBUS_ERROR_FAILED,
4227                       "Could not close file %s: %s\n",
4228                       filename_c,
4229                       _dbus_strerror (errno));
4230       return FALSE;
4231     }
4232
4233   return TRUE;
4234 }
4235
4236
4237 /**
4238  * Creates a directory; succeeds if the directory
4239  * is created or already existed.
4240  *
4241  * @param filename directory filename
4242  * @param error initialized error object
4243  * @returns #TRUE on success
4244  */
4245 dbus_bool_t
4246 _dbus_create_directory (const DBusString *filename,
4247                         DBusError        *error)
4248 {
4249   const char *filename_c;
4250
4251   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4252
4253   filename_c = _dbus_string_get_const_data (filename);
4254
4255   if (_dbus_mkdir (filename_c, 0700) < 0)
4256     {
4257       if (errno == EEXIST)
4258         return TRUE;
4259
4260       dbus_set_error (error, DBUS_ERROR_FAILED,
4261                       "Failed to create directory %s: %s\n",
4262                       filename_c, _dbus_strerror (errno));
4263       return FALSE;
4264     }
4265   else
4266     return TRUE;
4267 }
4268
4269
4270 static void
4271 pseudorandom_generate_random_bytes_buffer (char *buffer,
4272     int   n_bytes)
4273 {
4274   long tv_usec;
4275   int i;
4276
4277   /* fall back to pseudorandom */
4278   _dbus_verbose ("Falling back to pseudorandom for %d bytes\n",
4279                  n_bytes);
4280
4281   _dbus_get_current_time (NULL, &tv_usec);
4282   srand (tv_usec);
4283
4284   i = 0;
4285   while (i < n_bytes)
4286     {
4287       double r;
4288       unsigned int b;
4289
4290       r = rand ();
4291       b = (r / (double) RAND_MAX) * 255.0;
4292
4293       buffer[i] = b;
4294
4295       ++i;
4296     }
4297 }
4298
4299 static dbus_bool_t
4300 pseudorandom_generate_random_bytes (DBusString *str,
4301                                     int         n_bytes)
4302 {
4303   int old_len;
4304   char *p;
4305
4306   old_len = _dbus_string_get_length (str);
4307
4308   if (!_dbus_string_lengthen (str, n_bytes))
4309     return FALSE;
4310
4311   p = _dbus_string_get_data_len (str, old_len, n_bytes);
4312
4313   pseudorandom_generate_random_bytes_buffer (p, n_bytes);
4314
4315   return TRUE;
4316 }
4317
4318 /**
4319  * Gets the temporary files directory by inspecting the environment variables 
4320  * TMPDIR, TMP, and TEMP in that order. If none of those are set "/tmp" is returned
4321  *
4322  * @returns location of temp directory
4323  */
4324 const char*
4325 _dbus_get_tmpdir(void)
4326 {
4327   static const char* tmpdir = NULL;
4328
4329   if (tmpdir == NULL)
4330     {
4331       if (tmpdir == NULL)
4332         tmpdir = getenv("TMP");
4333       if (tmpdir == NULL)
4334         tmpdir = getenv("TEMP");
4335       if (tmpdir == NULL)
4336         tmpdir = getenv("TMPDIR");
4337       if (tmpdir == NULL)
4338           tmpdir = "C:\\Temp";
4339     }
4340
4341   _dbus_assert(tmpdir != NULL);
4342
4343   return tmpdir;
4344 }
4345
4346
4347 /**
4348  * Deletes the given file.
4349  *
4350  * @param filename the filename
4351  * @param error error location
4352  * 
4353  * @returns #TRUE if unlink() succeeded
4354  */
4355 dbus_bool_t
4356 _dbus_delete_file (const DBusString *filename,
4357                    DBusError        *error)
4358 {
4359   const char *filename_c;
4360
4361   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4362
4363   filename_c = _dbus_string_get_const_data (filename);
4364
4365   if (unlink (filename_c) < 0)
4366     {
4367       dbus_set_error (error, DBUS_ERROR_FAILED,
4368                       "Failed to delete file %s: %s\n",
4369                       filename_c, _dbus_strerror (errno));
4370       return FALSE;
4371     }
4372   else
4373     return TRUE;
4374 }
4375
4376 /**
4377  * Generates the given number of random bytes,
4378  * using the best mechanism we can come up with.
4379  *
4380  * @param str the string
4381  * @param n_bytes the number of random bytes to append to string
4382  * @returns #TRUE on success, #FALSE if no memory
4383  */
4384 dbus_bool_t
4385 _dbus_generate_random_bytes (DBusString *str,
4386                              int         n_bytes)
4387 {
4388   return pseudorandom_generate_random_bytes (str, n_bytes);
4389 }
4390
4391 #if !defined (DBUS_DISABLE_ASSERT) || defined(DBUS_BUILD_TESTS)
4392
4393 #ifdef _MSC_VER
4394 # ifdef BACKTRACES
4395 #  undef BACKTRACES
4396 # endif
4397 #else
4398 # define BACKTRACES
4399 #endif
4400
4401 #ifdef BACKTRACES
4402 /*
4403  * Backtrace Generator
4404  *
4405  * Copyright 2004 Eric Poech
4406  * Copyright 2004 Robert Shearman
4407  *
4408  * This library is free software; you can redistribute it and/or
4409  * modify it under the terms of the GNU Lesser General Public
4410  * License as published by the Free Software Foundation; either
4411  * version 2.1 of the License, or (at your option) any later version.
4412  *
4413  * This library is distributed in the hope that it will be useful,
4414  * but WITHOUT ANY WARRANTY; without even the implied warranty of
4415  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4416  * Lesser General Public License for more details.
4417  *
4418  * You should have received a copy of the GNU Lesser General Public
4419  * License along with this library; if not, write to the Free Software
4420  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4421  */
4422
4423 #include <winver.h>
4424 #include <imagehlp.h>
4425 #include <stdio.h>
4426
4427 #define DPRINTF _dbus_warn
4428
4429 #ifdef _MSC_VER
4430 #define BOOL int
4431
4432 #define __i386__
4433 #endif
4434
4435 //#define MAKE_FUNCPTR(f) static typeof(f) * p##f
4436
4437 //MAKE_FUNCPTR(StackWalk);
4438 //MAKE_FUNCPTR(SymGetModuleBase);
4439 //MAKE_FUNCPTR(SymFunctionTableAccess);
4440 //MAKE_FUNCPTR(SymInitialize);
4441 //MAKE_FUNCPTR(SymGetSymFromAddr);
4442 //MAKE_FUNCPTR(SymGetModuleInfo);
4443 static BOOL (WINAPI *pStackWalk)(
4444   DWORD MachineType,
4445   HANDLE hProcess,
4446   HANDLE hThread,
4447   LPSTACKFRAME StackFrame,
4448   PVOID ContextRecord,
4449   PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4450   PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4451   PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4452   PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4453 );
4454 static DWORD (WINAPI *pSymGetModuleBase)(
4455   HANDLE hProcess,
4456   DWORD dwAddr
4457 );
4458 static PVOID  (WINAPI *pSymFunctionTableAccess)(
4459   HANDLE hProcess,
4460   DWORD AddrBase
4461 );
4462 static BOOL  (WINAPI *pSymInitialize)(
4463   HANDLE hProcess,
4464   PSTR UserSearchPath,
4465   BOOL fInvadeProcess
4466 );
4467 static BOOL  (WINAPI *pSymGetSymFromAddr)(
4468   HANDLE hProcess,
4469   DWORD Address,
4470   PDWORD Displacement,
4471   PIMAGEHLP_SYMBOL Symbol
4472 );
4473 static BOOL  (WINAPI *pSymGetModuleInfo)(
4474   HANDLE hProcess,
4475   DWORD dwAddr,
4476   PIMAGEHLP_MODULE ModuleInfo
4477 );
4478 static DWORD  (WINAPI *pSymSetOptions)(
4479   DWORD SymOptions
4480 );
4481
4482
4483 static BOOL init_backtrace()
4484 {
4485     HMODULE hmodDbgHelp = LoadLibraryA("dbghelp");
4486 /*
4487     #define GETFUNC(x) \
4488     p##x = (typeof(x)*)GetProcAddress(hmodDbgHelp, #x); \
4489     if (!p##x) \
4490     { \
4491         return FALSE; \
4492     }
4493     */
4494
4495
4496 //    GETFUNC(StackWalk);
4497 //    GETFUNC(SymGetModuleBase);
4498 //    GETFUNC(SymFunctionTableAccess);
4499 //    GETFUNC(SymInitialize);
4500 //    GETFUNC(SymGetSymFromAddr);
4501 //    GETFUNC(SymGetModuleInfo);
4502
4503 #define FUNC(x) #x
4504
4505       pStackWalk = (BOOL  (WINAPI *)(
4506 DWORD MachineType,
4507 HANDLE hProcess,
4508 HANDLE hThread,
4509 LPSTACKFRAME StackFrame,
4510 PVOID ContextRecord,
4511 PREAD_PROCESS_MEMORY_ROUTINE ReadMemoryRoutine,
4512 PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
4513 PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
4514 PTRANSLATE_ADDRESS_ROUTINE TranslateAddress
4515 ))GetProcAddress (hmodDbgHelp, FUNC(StackWalk));
4516     pSymGetModuleBase=(DWORD  (WINAPI *)(
4517   HANDLE hProcess,
4518   DWORD dwAddr
4519 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleBase));
4520     pSymFunctionTableAccess=(PVOID  (WINAPI *)(
4521   HANDLE hProcess,
4522   DWORD AddrBase
4523 ))GetProcAddress (hmodDbgHelp, FUNC(SymFunctionTableAccess));
4524     pSymInitialize = (BOOL  (WINAPI *)(
4525   HANDLE hProcess,
4526   PSTR UserSearchPath,
4527   BOOL fInvadeProcess
4528 ))GetProcAddress (hmodDbgHelp, FUNC(SymInitialize));
4529     pSymGetSymFromAddr = (BOOL  (WINAPI *)(
4530   HANDLE hProcess,
4531   DWORD Address,
4532   PDWORD Displacement,
4533   PIMAGEHLP_SYMBOL Symbol
4534 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetSymFromAddr));
4535     pSymGetModuleInfo = (BOOL  (WINAPI *)(
4536   HANDLE hProcess,
4537   DWORD dwAddr,
4538   PIMAGEHLP_MODULE ModuleInfo
4539 ))GetProcAddress (hmodDbgHelp, FUNC(SymGetModuleInfo));
4540 pSymSetOptions = (DWORD  (WINAPI *)(
4541 DWORD SymOptions
4542 ))GetProcAddress (hmodDbgHelp, FUNC(SymSetOptions));
4543
4544
4545     pSymSetOptions(SYMOPT_UNDNAME);
4546
4547     pSymInitialize(GetCurrentProcess(), NULL, TRUE);
4548
4549     return TRUE;
4550 }
4551
4552 static void dump_backtrace_for_thread(HANDLE hThread)
4553 {
4554     STACKFRAME sf;
4555     CONTEXT context;
4556     DWORD dwImageType;
4557
4558     if (!pStackWalk)
4559         if (!init_backtrace())
4560             return;
4561
4562     /* can't use this function for current thread as GetThreadContext
4563      * doesn't support getting context from current thread */
4564     if (hThread == GetCurrentThread())
4565         return;
4566
4567     DPRINTF("Backtrace:\n");
4568
4569     memset(&context, 0, sizeof(context));
4570     context.ContextFlags = CONTEXT_FULL;
4571
4572     SuspendThread(hThread);
4573
4574     if (!GetThreadContext(hThread, &context))
4575     {
4576         DPRINTF("Couldn't get thread context (error %ld)\n", GetLastError());
4577         ResumeThread(hThread);
4578         return;
4579     }
4580
4581     memset(&sf, 0, sizeof(sf));
4582
4583 #ifdef __i386__
4584     sf.AddrFrame.Offset = context.Ebp;
4585     sf.AddrFrame.Mode = AddrModeFlat;
4586     sf.AddrPC.Offset = context.Eip;
4587     sf.AddrPC.Mode = AddrModeFlat;
4588     dwImageType = IMAGE_FILE_MACHINE_I386;
4589 #else
4590 # error You need to fill in the STACKFRAME structure for your architecture
4591 #endif
4592
4593     while (pStackWalk(dwImageType, GetCurrentProcess(),
4594                      hThread, &sf, &context, NULL, pSymFunctionTableAccess,
4595                      pSymGetModuleBase, NULL))
4596     {
4597         BYTE buffer[256];
4598         IMAGEHLP_SYMBOL * pSymbol = (IMAGEHLP_SYMBOL *)buffer;
4599         DWORD dwDisplacement;
4600
4601         pSymbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
4602         pSymbol->MaxNameLength = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL) + 1;
4603
4604         if (!pSymGetSymFromAddr(GetCurrentProcess(), sf.AddrPC.Offset,
4605                                 &dwDisplacement, pSymbol))
4606         {
4607             IMAGEHLP_MODULE ModuleInfo;
4608             ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
4609
4610             if (!pSymGetModuleInfo(GetCurrentProcess(), sf.AddrPC.Offset,
4611                                    &ModuleInfo))
4612                 DPRINTF("1\t%p\n", (void*)sf.AddrPC.Offset);
4613             else
4614                 DPRINTF("2\t%s+0x%lx\n", ModuleInfo.ImageName,
4615                     sf.AddrPC.Offset - ModuleInfo.BaseOfImage);
4616         }
4617         else if (dwDisplacement)
4618             DPRINTF("3\t%s+0x%lx\n", pSymbol->Name, dwDisplacement);
4619         else
4620             DPRINTF("4\t%s\n", pSymbol->Name);
4621     }
4622
4623     ResumeThread(hThread);
4624 }
4625
4626 static DWORD WINAPI dump_thread_proc(LPVOID lpParameter)
4627 {
4628     dump_backtrace_for_thread((HANDLE)lpParameter);
4629     return 0;
4630 }
4631
4632 /* cannot get valid context from current thread, so we have to execute
4633  * backtrace from another thread */
4634 static void dump_backtrace()
4635 {
4636     HANDLE hCurrentThread;
4637     HANDLE hThread;
4638     DWORD dwThreadId;
4639     DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
4640         GetCurrentProcess(), &hCurrentThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
4641     hThread = CreateThread(NULL, 0, dump_thread_proc, (LPVOID)hCurrentThread,
4642         0, &dwThreadId);
4643     WaitForSingleObject(hThread, INFINITE);
4644     CloseHandle(hThread);
4645     CloseHandle(hCurrentThread);
4646 }
4647
4648 void _dbus_print_backtrace(void)
4649 {
4650   init_backtrace();
4651   dump_backtrace();
4652 }
4653 #else
4654 void _dbus_print_backtrace(void)
4655 {
4656   _dbus_verbose ("  D-Bus not compiled with backtrace support\n");
4657 }
4658 #endif
4659
4660 /**
4661  * Sends a single nul byte with our UNIX credentials as ancillary
4662  * data.  Returns #TRUE if the data was successfully written.  On
4663  * systems that don't support sending credentials, just writes a byte,
4664  * doesn't send any credentials.  On some systems, such as Linux,
4665  * reading/writing the byte isn't actually required, but we do it
4666  * anyway just to avoid multiple codepaths.
4667  *
4668  * Fails if no byte can be written, so you must select() first.
4669  *
4670  * The point of the byte is that on some systems we have to
4671  * use sendmsg()/recvmsg() to transmit credentials.
4672  *
4673  * @param server_fd file descriptor for connection to server
4674  * @param error return location for error code
4675  * @returns #TRUE if the byte was sent
4676  */
4677 dbus_bool_t
4678 _dbus_send_credentials_unix_socket  (int              server_fd,
4679                                      DBusError       *error)
4680 {
4681   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4682
4683   if (write_credentials_byte (server_fd, error))
4684     return TRUE;
4685   else
4686     return FALSE;
4687 }
4688
4689 static dbus_uint32_t fromAscii(char ascii)
4690 {
4691     if(ascii >= '0' && ascii <= '9')
4692         return ascii - '0';
4693     if(ascii >= 'A' && ascii <= 'F')
4694         return ascii - 'A' + 10;
4695     if(ascii >= 'a' && ascii <= 'f')
4696         return ascii - 'a' + 10;
4697     return 0;    
4698 }
4699
4700 dbus_bool_t _dbus_read_local_machine_uuid   (DBusGUID         *machine_id,
4701                                              dbus_bool_t       create_if_not_found,
4702                                              DBusError        *error)
4703 {
4704 #ifdef DBUS_WINCE
4705         return TRUE;
4706   // TODO
4707 #else
4708     HW_PROFILE_INFOA info;
4709     char *lpc = &info.szHwProfileGuid[0];
4710     dbus_uint32_t u;
4711
4712     //  the hw-profile guid lives long enough
4713     if(!GetCurrentHwProfileA(&info))
4714       {
4715         dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); // FIXME
4716         return FALSE;  
4717       }
4718
4719     // Form: {12340001-4980-1920-6788-123456789012}
4720     lpc++;
4721     // 12340001
4722     u = ((fromAscii(lpc[0]) <<  0) |
4723          (fromAscii(lpc[1]) <<  4) |
4724          (fromAscii(lpc[2]) <<  8) |
4725          (fromAscii(lpc[3]) << 12) |
4726          (fromAscii(lpc[4]) << 16) |
4727          (fromAscii(lpc[5]) << 20) |
4728          (fromAscii(lpc[6]) << 24) |
4729          (fromAscii(lpc[7]) << 28));
4730     machine_id->as_uint32s[0] = u;
4731
4732     lpc += 9;
4733     // 4980-1920
4734     u = ((fromAscii(lpc[0]) <<  0) |
4735          (fromAscii(lpc[1]) <<  4) |
4736          (fromAscii(lpc[2]) <<  8) |
4737          (fromAscii(lpc[3]) << 12) |
4738          (fromAscii(lpc[5]) << 16) |
4739          (fromAscii(lpc[6]) << 20) |
4740          (fromAscii(lpc[7]) << 24) |
4741          (fromAscii(lpc[8]) << 28));
4742     machine_id->as_uint32s[1] = u;
4743     
4744     lpc += 10;
4745     // 6788-1234
4746     u = ((fromAscii(lpc[0]) <<  0) |
4747          (fromAscii(lpc[1]) <<  4) |
4748          (fromAscii(lpc[2]) <<  8) |
4749          (fromAscii(lpc[3]) << 12) |
4750          (fromAscii(lpc[5]) << 16) |
4751          (fromAscii(lpc[6]) << 20) |
4752          (fromAscii(lpc[7]) << 24) |
4753          (fromAscii(lpc[8]) << 28));
4754     machine_id->as_uint32s[2] = u;
4755     
4756     lpc += 9;
4757     // 56789012
4758     u = ((fromAscii(lpc[0]) <<  0) |
4759          (fromAscii(lpc[1]) <<  4) |
4760          (fromAscii(lpc[2]) <<  8) |
4761          (fromAscii(lpc[3]) << 12) |
4762          (fromAscii(lpc[4]) << 16) |
4763          (fromAscii(lpc[5]) << 20) |
4764          (fromAscii(lpc[6]) << 24) |
4765          (fromAscii(lpc[7]) << 28));
4766     machine_id->as_uint32s[3] = u;
4767 #endif
4768     return TRUE;
4769 }
4770
4771 static
4772 HANDLE _dbus_global_lock (const char *mutexname)
4773 {
4774   HANDLE mutex;
4775   DWORD gotMutex;
4776
4777   mutex = CreateMutex( NULL, FALSE, mutexname );
4778   if( !mutex )
4779     {
4780       return FALSE;
4781     }
4782
4783    gotMutex = WaitForSingleObject( mutex, INFINITE );
4784    switch( gotMutex )
4785      {
4786        case WAIT_ABANDONED:
4787                ReleaseMutex (mutex);
4788                CloseHandle (mutex);
4789                return 0;
4790        case WAIT_FAILED:
4791        case WAIT_TIMEOUT:
4792                return 0;
4793      }
4794
4795    return mutex;
4796 }
4797
4798 static
4799 void _dbus_global_unlock (HANDLE mutex)
4800 {
4801   ReleaseMutex (mutex);
4802   CloseHandle (mutex); 
4803 }
4804
4805 // for proper cleanup in dbus-daemon
4806 static HANDLE hDBusDaemonMutex = NULL;
4807 static HANDLE hDBusSharedMem = NULL;
4808 // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4809 static const char *cUniqueDBusInitMutex = "UniqueDBusInitMutex";
4810 // sync _dbus_get_autolaunch_address
4811 static const char *cDBusAutolaunchMutex = "DBusAutolaunchMutex";
4812 // mutex to determine if dbus-daemon is already started (per user)
4813 static const char *cDBusDaemonMutex = "DBusDaemonMutex";
4814 // named shm for dbus adress info (per user)
4815 static const char *cDBusDaemonAddressInfo = "DBusDaemonAddressInfo";
4816
4817 void
4818 _dbus_daemon_init(const char *host, dbus_uint32_t port)
4819 {
4820   HANDLE lock;
4821   const char *adr = NULL;
4822   char szUserName[64];
4823   DWORD dwUserNameSize = sizeof(szUserName);
4824   char szDBusDaemonMutex[128];
4825   char szDBusDaemonAddressInfo[128];
4826   char szAddress[128];
4827
4828   _dbus_assert(host);
4829   _dbus_assert(port);
4830
4831   _snprintf(szAddress, sizeof(szAddress) - 1, "tcp:host=%s,port=%d", host, port);
4832
4833   _dbus_assert( GetUserName(szUserName, &dwUserNameSize) != 0);
4834   _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4835             cDBusDaemonMutex, szUserName);
4836   _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4837             cDBusDaemonAddressInfo, szUserName);
4838
4839   // before _dbus_global_lock to keep correct lock/release order
4840   hDBusDaemonMutex = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4841
4842   _dbus_assert(WaitForSingleObject( hDBusDaemonMutex, 1000 ) == WAIT_OBJECT_0);
4843
4844   // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4845   lock = _dbus_global_lock( cUniqueDBusInitMutex );
4846
4847   // create shm
4848   hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
4849                                       0, strlen( szAddress ) + 1, szDBusDaemonAddressInfo );
4850   _dbus_assert( hDBusSharedMem );
4851
4852   adr = MapViewOfFile( hDBusSharedMem, FILE_MAP_WRITE, 0, 0, 0 );
4853
4854   _dbus_assert( adr );
4855
4856   strcpy(adr, szAddress);
4857
4858   // cleanup
4859   UnmapViewOfFile( adr );
4860
4861   _dbus_global_unlock( lock );
4862 }
4863
4864 void
4865 _dbus_daemon_release()
4866 {
4867   HANDLE lock;
4868
4869   // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4870   lock = _dbus_global_lock( cUniqueDBusInitMutex );
4871
4872   CloseHandle( hDBusSharedMem );
4873
4874   hDBusSharedMem = NULL;
4875
4876   ReleaseMutex( hDBusDaemonMutex );
4877
4878   CloseHandle( hDBusDaemonMutex );
4879
4880   hDBusDaemonMutex = NULL;
4881
4882   _dbus_global_unlock( lock );
4883 }
4884
4885 static dbus_bool_t
4886 _dbus_get_autolaunch_shm(DBusString *adress)
4887 {
4888   HANDLE sharedMem;
4889   const char *adr;
4890   char szUserName[64];
4891   DWORD dwUserNameSize = sizeof(szUserName);
4892   char szDBusDaemonAddressInfo[128];
4893
4894   if( !GetUserName(szUserName, &dwUserNameSize) )
4895       return FALSE;
4896   _snprintf(szDBusDaemonAddressInfo, sizeof(szDBusDaemonAddressInfo) - 1, "%s:%s",
4897             cDBusDaemonAddressInfo, szUserName);
4898
4899   // read shm
4900   do {
4901       // we know that dbus-daemon is available, so we wait until shm is available
4902       sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, szDBusDaemonAddressInfo );
4903       if( sharedMem == 0 )
4904           Sleep( 100 );
4905   } while( sharedMem == 0 );
4906
4907   if( sharedMem == 0 )
4908       return FALSE;
4909
4910   adr = MapViewOfFile( sharedMem, FILE_MAP_READ, 0, 0, 0 );
4911
4912   if( adr == 0 )
4913       return FALSE;
4914
4915   _dbus_string_init( adress );
4916
4917   _dbus_string_append( adress, adr ); 
4918
4919   // cleanup
4920   UnmapViewOfFile( adr );
4921
4922   CloseHandle( sharedMem );
4923
4924   return TRUE;
4925 }
4926
4927 static dbus_bool_t
4928 _dbus_daemon_already_runs (DBusString *adress)
4929 {
4930   HANDLE lock;
4931   HANDLE daemon;
4932   dbus_bool_t bRet = TRUE;
4933   char szUserName[64];
4934   DWORD dwUserNameSize = sizeof(szUserName);
4935   char szDBusDaemonMutex[128];
4936
4937   // sync _dbus_daemon_init, _dbus_daemon_uninit and _dbus_daemon_already_runs
4938   lock = _dbus_global_lock( cUniqueDBusInitMutex );
4939
4940   if( !GetUserName(szUserName, &dwUserNameSize) )
4941       return FALSE;
4942   _snprintf(szDBusDaemonMutex, sizeof(szDBusDaemonMutex) - 1, "%s:%s",
4943             cDBusDaemonMutex, szUserName);
4944
4945   // do checks
4946   daemon = CreateMutex( NULL, FALSE, szDBusDaemonMutex );
4947   if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT)
4948     {
4949       ReleaseMutex (daemon);
4950       CloseHandle (daemon);
4951
4952       _dbus_global_unlock( lock );
4953       return FALSE;
4954     }
4955
4956   // read shm
4957   bRet = _dbus_get_autolaunch_shm( adress );
4958
4959   // cleanup
4960   CloseHandle ( daemon );
4961
4962   _dbus_global_unlock( lock );
4963
4964   return bRet;
4965 }
4966
4967 dbus_bool_t
4968 _dbus_get_autolaunch_address (DBusString *address, 
4969                               DBusError *error)
4970 {
4971   HANDLE mutex;
4972   STARTUPINFOA si;
4973   PROCESS_INFORMATION pi;
4974   dbus_bool_t retval = FALSE;
4975   LPSTR lpFile;
4976   char dbus_exe_path[MAX_PATH];
4977   char dbus_args[MAX_PATH * 2];
4978
4979   mutex = _dbus_global_lock ( cDBusAutolaunchMutex );
4980
4981   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
4982
4983   if (_dbus_daemon_already_runs(address))
4984     {
4985         printf("dbus daemon already exists\n");
4986         retval = TRUE;
4987         goto out;
4988     }
4989
4990   if (!SearchPathA(NULL, "dbus-daemon.exe", NULL, sizeof(dbus_exe_path), dbus_exe_path, &lpFile))
4991     {
4992       printf ("could not find dbus-daemon executable\n");
4993       goto out;
4994     }
4995
4996   // Create process
4997   ZeroMemory( &si, sizeof(si) );
4998   si.cb = sizeof(si);
4999   ZeroMemory( &pi, sizeof(pi) );
5000
5001   _snprintf(dbus_args, sizeof(dbus_args) - 1, "\"%s\" %s", dbus_exe_path,  " --session");
5002
5003 //  argv[i] = "--config-file=bus\\session.conf";
5004   printf("create process \"%s\" %s\n", dbus_exe_path, dbus_args);
5005   if(CreateProcessA(dbus_exe_path, dbus_args, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
5006     {
5007       retval = TRUE;
5008
5009       // Wait until started (see _dbus_get_autolaunch_shm())
5010       WaitForInputIdle(pi.hProcess, INFINITE);
5011
5012       retval = _dbus_get_autolaunch_shm( address );
5013     } else {
5014       retval = FALSE;
5015     }
5016   
5017 out:
5018   if (retval)
5019     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
5020   else
5021     _DBUS_ASSERT_ERROR_IS_SET (error);
5022   
5023   _dbus_global_unlock (mutex);
5024
5025   return retval;
5026  }
5027
5028
5029 /** Makes the file readable by every user in the system.
5030  *
5031  * @param filename the filename
5032  * @param error error location
5033  * @returns #TRUE if the file's permissions could be changed.
5034  */
5035 dbus_bool_t
5036 _dbus_make_file_world_readable(const DBusString *filename,
5037                                DBusError *error)
5038 {
5039   // TODO
5040   return TRUE;
5041 }
5042
5043
5044 #define DBUS_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
5045
5046 // @TODO: this function is duplicated from dbus-sysdeps-unix.c
5047 //        and differs only in the path separator, may be we should 
5048 //        use a dbus path separator variable
5049
5050 #define _dbus_path_seperator ";"
5051
5052 static dbus_bool_t
5053 split_paths_and_append (DBusString *dirs, 
5054                         const char *suffix, 
5055                         DBusList **dir_list)
5056 {
5057    int start;
5058    int i;
5059    int len;
5060    char *cpath;
5061    const DBusString file_suffix;
5062
5063    start = 0;
5064    i = 0;
5065
5066    _dbus_string_init_const (&file_suffix, suffix);
5067
5068    len = _dbus_string_get_length (dirs);
5069
5070    while (_dbus_string_find (dirs, start, _dbus_path_seperator, &i))
5071      {
5072        DBusString path;
5073
5074        if (!_dbus_string_init (&path))
5075           goto oom;
5076
5077        if (!_dbus_string_copy_len (dirs,
5078                                    start,
5079                                    i - start,
5080                                    &path,
5081                                    0))
5082           {
5083             _dbus_string_free (&path);
5084             goto oom;
5085           }
5086
5087         _dbus_string_chop_white (&path);
5088
5089         /* check for an empty path */
5090         if (_dbus_string_get_length (&path) == 0)
5091           goto next;
5092
5093         if (!_dbus_concat_dir_and_file (&path,
5094                                         &file_suffix))
5095           {
5096             _dbus_string_free (&path);
5097             goto oom;
5098           }
5099
5100         if (!_dbus_string_copy_data(&path, &cpath))
5101           {
5102             _dbus_string_free (&path);
5103             goto oom;
5104           }
5105
5106         if (!_dbus_list_append (dir_list, cpath))
5107           {
5108             _dbus_string_free (&path);              
5109             dbus_free (cpath);
5110             goto oom;
5111           }
5112
5113        next:
5114         _dbus_string_free (&path);
5115         start = i + 1;
5116     } 
5117       
5118   if (start != len)
5119     { 
5120       DBusString path;
5121
5122       if (!_dbus_string_init (&path))
5123         goto oom;
5124
5125       if (!_dbus_string_copy_len (dirs,
5126                                   start,
5127                                   len - start,
5128                                   &path,
5129                                   0))
5130         {
5131           _dbus_string_free (&path);
5132           goto oom;
5133         }
5134
5135       if (!_dbus_concat_dir_and_file (&path,
5136                                       &file_suffix))
5137         {
5138           _dbus_string_free (&path);
5139           goto oom;
5140         }
5141
5142       if (!_dbus_string_copy_data(&path, &cpath))
5143         {
5144           _dbus_string_free (&path);
5145           goto oom;
5146         }
5147
5148       if (!_dbus_list_append (dir_list, cpath))
5149         {
5150           _dbus_string_free (&path);              
5151           dbus_free (cpath);
5152           goto oom;
5153         }
5154
5155       _dbus_string_free (&path); 
5156     }
5157
5158   return TRUE;
5159
5160  oom:
5161   _dbus_list_foreach (dir_list, (DBusForeachFunction)dbus_free, NULL); 
5162   _dbus_list_clear (dir_list);
5163   return FALSE;
5164 }
5165
5166 /**
5167  * Returns the standard directories for a session bus to look for service 
5168  * activation files 
5169  *
5170  * On Windows this should be data directories:
5171  *
5172  * %CommonProgramFiles%/dbus
5173  *
5174  * and
5175  *
5176  * DBUS_DATADIR
5177  *
5178  * @param dirs the directory list we are returning
5179  * @returns #FALSE on OOM 
5180  */
5181
5182 dbus_bool_t 
5183 _dbus_get_standard_session_servicedirs (DBusList **dirs)
5184 {
5185   const char *common_progs;
5186   DBusString servicedir_path;
5187
5188   if (!_dbus_string_init (&servicedir_path))
5189     return FALSE;
5190
5191   if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR";"))
5192         goto oom;
5193
5194   common_progs = _dbus_getenv ("CommonProgramFiles");
5195
5196   if (common_progs != NULL)
5197     {
5198       if (!_dbus_string_append (&servicedir_path, common_progs))
5199         goto oom;
5200
5201       if (!_dbus_string_append (&servicedir_path, ";"))
5202         goto oom;
5203     }
5204
5205   if (!split_paths_and_append (&servicedir_path, 
5206                                DBUS_STANDARD_SESSION_SERVICEDIR, 
5207                                dirs))
5208     goto oom;
5209
5210   _dbus_string_free (&servicedir_path);  
5211   return TRUE;
5212
5213  oom:
5214   _dbus_string_free (&servicedir_path);
5215   return FALSE;
5216 }
5217
5218 _DBUS_DEFINE_GLOBAL_LOCK (atomic);
5219
5220 /**
5221  * Atomically increments an integer
5222  *
5223  * @param atomic pointer to the integer to increment
5224  * @returns the value before incrementing
5225  *
5226  */
5227 dbus_int32_t
5228 _dbus_atomic_inc (DBusAtomic *atomic)
5229 {
5230   // +/- 1 is needed here!
5231   return InterlockedIncrement (&atomic->value) - 1;
5232 }
5233
5234 /**
5235  * Atomically decrement an integer
5236  *
5237  * @param atomic pointer to the integer to decrement
5238  * @returns the value before decrementing
5239  *
5240  */
5241 dbus_int32_t
5242 _dbus_atomic_dec (DBusAtomic *atomic)
5243 {
5244   // +/- 1 is needed here!
5245   return InterlockedDecrement (&atomic->value) + 1;
5246 }
5247
5248 #endif /* asserts or tests enabled */
5249
5250 /** @} end of sysdeps-win */
5251
5252 /* tests in dbus-sysdeps-util.c */