2 * daemon.c: The handle daemon
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
14 #include <sys/types.h>
15 #include <sys/socket.h>
25 #include <mono/io-layer/io-layer.h>
26 #include <mono/io-layer/handles-private.h>
27 #include <mono/io-layer/wapi-private.h>
28 #include <mono/io-layer/daemon-messages.h>
29 #include <mono/io-layer/timefuncs-private.h>
30 #include <mono/io-layer/daemon-private.h>
34 /* The shared thread codepath doesn't seem to work yet... */
35 #undef _POSIX_THREAD_PROCESS_SHARED
37 /* Keep track of the number of clients */
39 /* Array to keep track of handles that have been referenced by the
42 static guint32 *daemon_handles=NULL;
43 /* The socket which we listen to new connections on */
46 /* Set to TRUE by the SIGCHLD signal handler */
47 static volatile gboolean check_processes=FALSE;
49 static gboolean fd_activity (GIOChannel *channel, GIOCondition condition,
53 /* Deletes the shared memory segment. If we're exiting on error,
54 * clients will get EPIPEs.
56 static void cleanup (void)
58 #ifdef NEED_LINK_UNLINK
59 unlink(_wapi_shared_data->daemon);
64 /* If there is only one socket, and no child processes, we can exit.
65 * We test for child processes by counting handle references held by
68 static void maybe_exit (void)
73 g_message (G_GNUC_PRETTY_FUNCTION ": Seeing if we should exit");
78 g_message (G_GNUC_PRETTY_FUNCTION ": Still got clients");
83 for(i=0; i<_WAPI_MAX_HANDLES; i++) {
84 if(daemon_handles[i]>0) {
86 g_message (G_GNUC_PRETTY_FUNCTION
87 ": Still got handle references");
94 g_message (G_GNUC_PRETTY_FUNCTION ": Byebye");
105 * Called if daemon receives a SIGTERM or SIGINT
107 static void signal_handler (int unused)
117 * Called if daemon receives a SIGCHLD, and notes that a process needs
118 * to be wait()ed for.
120 static void sigchld_handler (int unused)
122 /* Notice that a child process died */
123 check_processes=TRUE;
129 * Bind signals and attach to shared memory
131 static void startup (void)
135 sa.sa_handler=signal_handler;
136 sigemptyset (&sa.sa_mask);
138 sigaction (SIGINT, &sa, NULL);
139 sigaction (SIGTERM, &sa, NULL);
141 #ifndef HAVE_MSG_NOSIGNAL
142 sa.sa_handler=SIG_IGN;
143 sigaction (SIGPIPE, &sa, NULL);
146 sa.sa_handler=sigchld_handler;
147 sa.sa_flags=SA_NOCLDSTOP;
148 sigaction (SIGCHLD, &sa, NULL);
150 #ifdef NEED_LINK_UNLINK
151 /* Here's a more portable method... */
152 snprintf (_wapi_shared_data->daemon, MONO_SIZEOF_SUNPATH-1,
153 "/tmp/mono-handle-daemon-%d-%ld-%ld", getuid (), random (),
156 /* Leave the first byte NULL so we create the socket in the
157 * abstrace namespace, not on the filesystem. (Lets see how
158 * portable _that_ is :)
160 * The name is intended to be unique, not cryptographically
163 snprintf (_wapi_shared_data->daemon+1, MONO_SIZEOF_SUNPATH-2,
164 "mono-handle-daemon-%d-%d-%ld", getuid (), getpid (),
172 * @open_handles: An array of handles referenced by the calling client
173 * @handle: handle to inc refcnt
175 * Increase ref count of handle for the calling client. Handle 0 is
178 static void ref_handle (guint32 *open_handles, guint32 handle)
184 _wapi_shared_data->handles[handle].ref++;
185 open_handles[handle]++;
188 g_message (G_GNUC_PRETTY_FUNCTION
189 ": handle 0x%x ref now %d (%d this process)", handle,
190 _wapi_shared_data->handles[handle].ref,
191 open_handles[handle]);
197 * @open_handles: An array of handles referenced by the calling client
198 * @handle: handle to inc refcnt
199 * @daemon_initiated: set to %TRUE if the daemon is unreffing a
200 * handle. Controls whether or not to set the handle type to UNUSED.
201 * This must not happen here if a client process is unreffing a
202 * handle, because it has more cleaning up to do and must still be
203 * able to locate the handle and specify the type.
205 * Decrease ref count of handle for the calling client. If global ref
206 * count reaches 0 it is free'ed. Return TRUE if the local ref count
207 * is 0. Handle 0 is ignored.
209 static gboolean unref_handle (guint32 *open_handles, guint32 handle,
210 gboolean daemon_initiated)
212 gboolean destroy=FALSE;
218 if (open_handles[handle] == 0) {
219 g_warning(G_GNUC_PRETTY_FUNCTION
220 ": unref on %d called when ref was already 0",
225 _wapi_shared_data->handles[handle].ref--;
226 open_handles[handle]--;
229 g_message (G_GNUC_PRETTY_FUNCTION
230 ": handle 0x%x ref now %d (%d this process)", handle,
231 _wapi_shared_data->handles[handle].ref,
232 open_handles[handle]);
235 if(open_handles[handle]==0) {
236 /* This client has released the handle */
240 if(_wapi_shared_data->handles[handle].ref==0) {
241 if (open_handles[handle]!=0) {
242 g_warning (G_GNUC_PRETTY_FUNCTION ": per-process open_handles mismatch, set to %d, should be 0", open_handles[handle]);
246 g_message (G_GNUC_PRETTY_FUNCTION ": Destroying handle 0x%x",
250 _wapi_handle_ops_close_shared (GUINT_TO_POINTER (handle));
252 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
253 mono_mutex_destroy (&_wapi_shared_data->handles[handle].signal_mutex);
254 pthread_cond_destroy (&_wapi_shared_data->handles[handle].signal_cond);
257 memset (&_wapi_shared_data->handles[handle].u, '\0', sizeof(_wapi_shared_data->handles[handle].u));
258 if(daemon_initiated) {
259 _wapi_shared_data->handles[handle].type=WAPI_HANDLE_UNUSED;
263 if(open_handles==daemon_handles) {
264 /* The daemon released a reference, so see if it's
275 * @fd: Filehandle to add
277 * Create a new GIOChannel, and add it to the main loop event sources.
279 static void add_fd(int fd)
281 GIOChannel *io_channel;
284 io_channel=g_io_channel_unix_new (fd);
286 /* Turn off all encoding and buffering crap */
287 g_io_channel_set_encoding (io_channel, NULL, NULL);
288 g_io_channel_set_buffered (io_channel, FALSE);
290 refs=g_new0 (guint32, _WAPI_MAX_HANDLES);
291 if(daemon_handles==NULL) {
292 /* We rely on the daemon channel being created first.
293 * That's safe, because every other channel is the
294 * result of an accept() on the daemon channel.
299 g_io_add_watch (io_channel, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
307 * @channel: GIOChannel to close
309 * Closes the IO channel. Closes all handles that it may have open. If
310 * only main_sock is left, the daemon is shut down.
312 static void rem_fd(GIOChannel *channel, guint32 *open_handles)
314 guint32 handle_count;
317 if(g_io_channel_unix_get_fd (channel) == main_sock) {
318 /* We shouldn't be deleting the daemon's fd */
319 g_warning (G_GNUC_PRETTY_FUNCTION ": Deleting daemon fd!");
325 g_message (G_GNUC_PRETTY_FUNCTION ": Removing client fd %d",
326 g_io_channel_unix_get_fd (channel));
329 g_io_channel_shutdown (channel, TRUE, NULL);
331 for(i=0; i<_WAPI_MAX_HANDLES; i++) {
332 handle_count=open_handles[i];
334 for(j=0; j<handle_count; j++) {
336 g_message (G_GNUC_PRETTY_FUNCTION ": closing handle 0x%x for client at index %d", i, g_io_channel_unix_get_fd (channel));
338 /* Ignore the hint to the client to destroy
339 * the handle private data
341 unref_handle (open_handles, i, TRUE);
345 g_free (open_handles);
349 /* Just the master socket left, so see if we can
356 static gboolean process_compare (gpointer handle, gpointer user_data)
358 struct _WapiHandle_process *process_handle;
362 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
363 (gpointer *)&process_handle, NULL);
365 g_warning (G_GNUC_PRETTY_FUNCTION
366 ": error looking up process handle %p", handle);
370 pid=GPOINTER_TO_UINT (user_data);
371 if(process_handle->id==pid) {
378 static gboolean process_thread_compare (gpointer handle, gpointer user_data)
380 struct _WapiHandle_thread *thread_handle;
383 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
384 (gpointer *)&thread_handle, NULL);
386 g_warning (G_GNUC_PRETTY_FUNCTION
387 ": error looking up thread handle %p", handle);
391 if(thread_handle->process_handle==user_data) {
392 /* Signal the handle. Don't use
393 * _wapi_handle_set_signal_state() unless we have
394 * process-shared pthread support.
397 g_message (G_GNUC_PRETTY_FUNCTION ": Set thread handle %p signalled, because its process died", handle);
400 thread_handle->exitstatus=0;
402 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
403 _wapi_handle_lock_handle (handle);
404 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
405 _wapi_handle_unlock_handle (handle);
407 /* Just tweak the signal state directly. This is not
408 * recommended behaviour, but it works for threads
409 * because they can never become unsignalled. There
410 * are some nasty kludges in the handle waiting code
411 * to cope with missing condition signals for when
412 * process-shared pthread support is missing.
414 _wapi_shared_data->handles[GPOINTER_TO_UINT (handle)].signalled=TRUE;
415 #endif /* _POSIX_THREAD_PROCESS_SHARED */
418 /* Return false to keep searching */
422 /* Find the handle associated with pid, mark it dead and record exit
423 * status. Finds all thread handles associated with this process
424 * handle, and marks those signalled too, with exitstatus '0'. It
425 * also drops the daemon's reference to the handle, and the thread
426 * pointed at by main_thread.
428 static void process_post_mortem (pid_t pid, int status)
430 gpointer process_handle;
431 struct _WapiHandle_process *process_handle_data;
433 process_handle=_wapi_search_handle (WAPI_HANDLE_PROCESS,
435 GUINT_TO_POINTER (pid),
436 (gpointer *)&process_handle_data,
438 if(process_handle==0) {
439 g_warning (G_GNUC_PRETTY_FUNCTION
440 ": Couldn't find handle for process %d!", pid);
442 /* Signal the handle. Don't use
443 * _wapi_handle_set_signal_state() unless we have
444 * process-shared pthread support.
449 g_message (G_GNUC_PRETTY_FUNCTION
450 ": Set process %d exitstatus to %d", pid,
451 WEXITSTATUS (status));
454 /* Technically WEXITSTATUS is only valid if the
455 * process exited normally, but I don't care if the
456 * process caught a signal or not.
458 process_handle_data->exitstatus=WEXITSTATUS (status);
461 gettimeofday (&tv, NULL);
462 _wapi_timeval_to_filetime (&tv,
463 &process_handle_data->exit_time);
465 #if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED != -1
466 _wapi_handle_lock_handle (process_handle);
467 _wapi_handle_set_signal_state (process_handle, TRUE, TRUE);
468 _wapi_handle_unlock_handle (process_handle);
470 /* Just tweak the signal state directly. This is not
471 * recommended behaviour, but it works for processes
472 * because they can never become unsignalled. There
473 * are some nasty kludges in the handle waiting code
474 * to cope with missing condition signals for when
475 * process-shared pthread support is missing.
477 _wapi_shared_data->handles[GPOINTER_TO_UINT (process_handle)].signalled=TRUE;
478 #endif /* _POSIX_THREAD_PROCESS_SHARED */
481 /* Find all threads that have their process
482 * handle==process_handle. Ignore the return value, all the
483 * work will be done in the compare func
485 (void)_wapi_search_handle (WAPI_HANDLE_THREAD, process_thread_compare,
486 process_handle, NULL, NULL);
488 unref_handle (daemon_handles,
489 GPOINTER_TO_UINT (process_handle_data->main_thread),
491 unref_handle (daemon_handles, GPOINTER_TO_UINT (process_handle),
495 static void process_died (void)
500 check_processes=FALSE;
503 g_message (G_GNUC_PRETTY_FUNCTION ": Reaping processes");
507 pid=waitpid (-1, &status, WNOHANG);
508 if(pid==0 || pid==-1) {
509 /* Finished waiting. I was checking pid==-1
510 * separately but was getting ECHILD when
511 * there were no more child processes (which
512 * doesnt seem to conform to the man page)
516 /* pid contains the ID of a dead process */
518 g_message (G_GNUC_PRETTY_FUNCTION ": process %d reaped", pid);
520 process_post_mortem (pid, status);
528 * @channel: channel to send reply to
529 * @resp: Package to send
531 * Send a package to a client
533 static void send_reply (GIOChannel *channel, WapiHandleResponse *resp)
536 _wapi_daemon_response (g_io_channel_unix_get_fd (channel), resp);
541 * @channel: The client making the request
542 * @open_handles: An array of handles referenced by this client
543 * @type: type to init handle to
545 * Find a free handle and initialize it to 'type', increase refcnt and
546 * send back a reply to the client.
548 static void process_new (GIOChannel *channel, guint32 *open_handles,
552 WapiHandleResponse resp;
554 /* handle might be set to 0. This is handled at the client end */
555 handle=_wapi_handle_new_internal (type);
556 ref_handle (open_handles, handle);
559 g_message (G_GNUC_PRETTY_FUNCTION ": returning new handle 0x%x",
563 resp.type=WapiHandleResponseType_New;
564 resp.u.new.type=type;
565 resp.u.new.handle=handle;
567 send_reply (channel, &resp);
572 * @channel: The client making the request
573 * @open_handles: An array of handles referenced by this client
574 * @handle: handle no.
576 * Increase refcnt on a previously created handle and send back a
577 * response to the client.
579 static void process_open (GIOChannel *channel, guint32 *open_handles,
582 WapiHandleResponse resp;
583 struct _WapiHandleShared *shared=&_wapi_shared_data->handles[handle];
585 if(shared->type!=WAPI_HANDLE_UNUSED && handle!=0) {
586 ref_handle (open_handles, handle);
589 g_message (G_GNUC_PRETTY_FUNCTION
590 ": returning new handle 0x%x", handle);
593 resp.type=WapiHandleResponseType_Open;
594 resp.u.new.type=shared->type;
595 resp.u.new.handle=handle;
597 send_reply (channel, &resp);
602 resp.type=WapiHandleResponseType_Open;
605 send_reply (channel, &resp);
610 * @channel: The client making the request
611 * @open_handles: An array of handles referenced by this client
612 * @handle: handle no.
614 * Decrease refcnt on a previously created handle and send back a
615 * response to the client with notice of it being destroyed.
617 static void process_close (GIOChannel *channel, guint32 *open_handles,
620 WapiHandleResponse resp;
622 resp.type=WapiHandleResponseType_Close;
623 resp.u.close.destroy=unref_handle (open_handles, handle, FALSE);
626 g_message (G_GNUC_PRETTY_FUNCTION ": unreffing handle 0x%x", handle);
629 send_reply (channel, &resp);
634 * @channel: The client making the request
635 * @length: allocate this much scratch space
637 * Allocate some scratch space and send a reply to the client.
639 static void process_scratch (GIOChannel *channel, guint32 length)
641 WapiHandleResponse resp;
643 resp.type=WapiHandleResponseType_Scratch;
644 resp.u.scratch.idx=_wapi_handle_scratch_store_internal (length);
647 g_message (G_GNUC_PRETTY_FUNCTION ": allocating scratch index 0x%x",
651 send_reply (channel, &resp);
655 * process_scratch_free:
656 * @channel: The client making the request
657 * @scratch_idx: deallocate this scratch space
659 * Deallocate scratch space and send a reply to the client.
661 static void process_scratch_free (GIOChannel *channel, guint32 scratch_idx)
663 WapiHandleResponse resp;
665 resp.type=WapiHandleResponseType_ScratchFree;
666 _wapi_handle_scratch_delete_internal (scratch_idx);
669 g_message (G_GNUC_PRETTY_FUNCTION ": deleting scratch index 0x%x",
673 send_reply (channel, &resp);
677 * process_process_fork:
678 * @channel: The client making the request
679 * @open_handles: An array of handles referenced by this client
680 * @process_fork: Describes the process to fork
681 * @fds: stdin, stdout, and stderr for the new process
683 * Forks a new process, and returns the process and thread data to the
686 static void process_process_fork (GIOChannel *channel, guint32 *open_handles,
687 WapiHandleRequest_ProcessFork process_fork,
690 WapiHandleResponse resp;
691 guint32 process_handle, thread_handle;
692 struct _WapiHandle_process *process_handle_data;
693 struct _WapiHandle_thread *thread_handle_data;
696 resp.type=WapiHandleResponseType_ProcessFork;
698 /* Create handles first, so the child process can store exec
699 * errors. Either handle might be set to 0, if this happens
700 * just reply to the client without bothering to fork. The
701 * client must check if either handle is 0 and take
702 * appropriate error handling action.
704 process_handle=_wapi_handle_new_internal (WAPI_HANDLE_PROCESS);
705 ref_handle (daemon_handles, process_handle);
706 ref_handle (open_handles, process_handle);
708 thread_handle=_wapi_handle_new_internal (WAPI_HANDLE_THREAD);
709 ref_handle (daemon_handles, thread_handle);
710 ref_handle (open_handles, thread_handle);
712 if(process_handle==0 || thread_handle==0) {
713 /* unref_handle() copes with the handle being 0 */
714 unref_handle (daemon_handles, process_handle, TRUE);
715 unref_handle (open_handles, process_handle, TRUE);
716 unref_handle (daemon_handles, thread_handle, TRUE);
717 unref_handle (open_handles, thread_handle, TRUE);
721 char *cmd=NULL, *dir=NULL, **argv, **env;
726 /* Get usable copies of the cmd, dir and env now
727 * rather than in the child process. This is to
728 * prevent the race condition where the parent can
729 * return the reply to the client, which then promptly
730 * deletes the scratch data before the new process
731 * gets to see it. Also explode argv here so we can
732 * use it to set the process name.
734 cmd=_wapi_handle_scratch_lookup_as_string (process_fork.cmd);
735 dir=_wapi_handle_scratch_lookup_as_string (process_fork.dir);
736 env=_wapi_handle_scratch_lookup_string_array (process_fork.env);
738 ret=g_shell_parse_argv (cmd, NULL, &argv, &gerr);
740 /* FIXME: Could do something with the
743 process_handle_data->exec_errno=gerr->code;
746 g_message (G_GNUC_PRETTY_FUNCTION ": forking");
749 _wapi_lookup_handle (GUINT_TO_POINTER (process_handle),
751 (gpointer *)&process_handle_data,
754 _wapi_lookup_handle (GUINT_TO_POINTER (thread_handle),
756 (gpointer *)&thread_handle_data,
759 /* Fork, exec cmd with args and optional env,
760 * and return the handles with pid and blank
765 process_handle_data->exec_errno=errno;
770 /* should we detach from the process
771 * group? We're already running
772 * without a controlling tty...
775 /* Connect stdin, stdout and stderr */
780 if(process_fork.inherit!=TRUE) {
781 /* FIXME: do something here */
784 /* Close all file descriptors */
785 for(i=3; i<getdtablesize (); i++) {
790 g_message (G_GNUC_PRETTY_FUNCTION
791 ": exec()ing [%s] in dir [%s]",
795 while(argv[i]!=NULL) {
796 g_message ("arg %d: [%s]",
802 while(env[i]!=NULL) {
803 g_message ("env %d: [%s]",
811 if(chdir (dir)==-1) {
812 process_handle_data->exec_errno=errno;
817 execve (argv[0], argv, env);
820 process_handle_data->exec_errno=errno;
826 /* store process name, based on the last section of the cmd */
828 char *slash=strrchr (argv[0], '/');
831 process_handle_data->proc_name=_wapi_handle_scratch_store (slash+1, strlen (slash+1));
833 process_handle_data->proc_name=_wapi_handle_scratch_store (argv[0], strlen (argv[0]));
837 /* These seem to be the defaults on w2k */
838 process_handle_data->min_working_set=204800;
839 process_handle_data->max_working_set=1413120;
851 process_handle_data->id=pid;
852 process_handle_data->main_thread=GUINT_TO_POINTER (thread_handle);
854 gettimeofday (&tv, NULL);
855 _wapi_timeval_to_filetime (&tv,
856 &process_handle_data->create_time);
858 /* FIXME: if env==0, inherit the env from the current
861 process_handle_data->env=process_fork.env;
863 thread_handle_data->process_handle=GUINT_TO_POINTER (process_handle);
865 resp.u.process_fork.pid=pid;
868 resp.u.process_fork.process_handle=process_handle;
869 resp.u.process_fork.thread_handle=thread_handle;
871 send_reply (channel, &resp);
876 * @channel: The client to read the request from
877 * @open_handles: An array of handles referenced by this client
879 * Read a message (A WapiHandleRequest) from a client and dispatch
880 * whatever it wants to the process_* calls.
882 static void read_message (GIOChannel *channel, guint32 *open_handles)
884 WapiHandleRequest req;
885 int fds[3]={0, 1, 2};
887 gboolean has_fds=FALSE;
890 ret=_wapi_daemon_request (g_io_channel_unix_get_fd (channel), &req,
893 /* Other end went away */
895 g_message ("Read 0 bytes on fd %d, closing it",
896 g_io_channel_unix_get_fd (channel));
899 rem_fd (channel, open_handles);
904 case WapiHandleRequestType_New:
905 process_new (channel, open_handles, req.u.new.type);
907 case WapiHandleRequestType_Open:
909 g_assert(req.u.open.handle < _WAPI_MAX_HANDLES);
911 process_open (channel, open_handles, req.u.open.handle);
913 case WapiHandleRequestType_Close:
915 g_assert(req.u.close.handle < _WAPI_MAX_HANDLES);
917 process_close (channel, open_handles, req.u.close.handle);
919 case WapiHandleRequestType_Scratch:
920 process_scratch (channel, req.u.scratch.length);
922 case WapiHandleRequestType_ScratchFree:
923 process_scratch_free (channel, req.u.scratch_free.idx);
925 case WapiHandleRequestType_ProcessFork:
926 process_process_fork (channel, open_handles,
927 req.u.process_fork, fds);
929 case WapiHandleRequestType_Error:
932 /* Catch bogus requests */
933 /* FIXME: call rem_fd? */
939 g_message (G_GNUC_PRETTY_FUNCTION ": closing %d", fds[0]);
940 g_message (G_GNUC_PRETTY_FUNCTION ": closing %d", fds[1]);
941 g_message (G_GNUC_PRETTY_FUNCTION ": closing %d", fds[2]);
952 * @channel: The IO channel that is active
953 * @condition: The condition that has been satisfied
954 * @data: A pointer to an array of handles referenced by this client
956 * The callback called by the main loop when there is activity on an
959 static gboolean fd_activity (GIOChannel *channel, GIOCondition condition,
962 if(condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
964 g_message ("fd %d error", g_io_channel_unix_get_fd (channel));
967 rem_fd (channel, data);
971 if(condition & (G_IO_IN | G_IO_PRI)) {
972 if(g_io_channel_unix_get_fd (channel)==main_sock) {
974 struct sockaddr addr;
975 socklen_t addrlen=sizeof(struct sockaddr);
977 newsock=accept (main_sock, &addr, &addrlen);
979 g_critical ("accept error: %s",
986 g_message ("accept returning %d", newsock);
992 g_message ("reading data on fd %d",
993 g_io_channel_unix_get_fd (channel));
996 read_message (channel, data);
1001 return(FALSE); /* remove source */
1005 * _wapi_daemon_main:
1007 * Open socket, create shared mem segment and begin listening for
1010 void _wapi_daemon_main(gpointer shm)
1012 struct sockaddr_un main_socket_address;
1016 g_message ("Starting up...");
1019 _wapi_shared_data=shm;
1022 main_sock=socket(PF_UNIX, SOCK_STREAM, 0);
1024 main_socket_address.sun_family=AF_UNIX;
1025 memcpy(main_socket_address.sun_path, _wapi_shared_data->daemon,
1026 MONO_SIZEOF_SUNPATH);
1028 ret=bind(main_sock, (struct sockaddr *)&main_socket_address,
1029 sizeof(struct sockaddr_un));
1031 g_critical ("bind failed: %s", g_strerror (errno));
1032 _wapi_shared_data->daemon_running=DAEMON_DIED_AT_STARTUP;
1040 ret=listen(main_sock, 5);
1042 g_critical ("listen failed: %s", g_strerror (errno));
1043 _wapi_shared_data->daemon_running=DAEMON_DIED_AT_STARTUP;
1048 g_message("listening");
1053 /* We're finished setting up, let everyone else know we're
1054 * ready. From now on, it's up to us to delete the shared
1055 * memory segment when appropriate.
1057 _wapi_shared_data->daemon_running=DAEMON_RUNNING;
1060 if(check_processes==TRUE) {
1065 g_message ("polling");
1068 /* Block until something happens. We don't use
1069 * g_main_loop_run() because we rely on the SIGCHLD
1070 * signal interrupting poll() so we can reap child
1071 * processes as soon as they die, without burning cpu
1072 * time by polling the flag.
1074 g_main_context_iteration (g_main_context_default (), TRUE);