Skip to content

Commit

Permalink
2002-07-10 Dennis Haney <davh@davh.dk>
Browse files Browse the repository at this point in the history
	* shared.c:
	* handles.c:
	* daemon.c: Lots of documentation, some added error checking, and
	code readability improvements.

	* daemon-messages.h: Add the Error request type to improve error
	checking.

	* daemon-messages.c: Do a bit more error checking on send() and
	recv(), and log errors with a higher severity level.

svn path=/trunk/mono/; revision=5683
  • Loading branch information
dickp committed Jul 10, 2002
1 parent 1d331d1 commit 8341073
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 35 deletions.
2 changes: 2 additions & 0 deletions docs/.cvsignore
@@ -0,0 +1,2 @@
Makefile
Makefile.in
98 changes: 98 additions & 0 deletions docs/mono_handle_d
@@ -0,0 +1,98 @@
=pod

=head1 Internal design document for the mono_handle_d

This document is designed to hold the design of the mono_handle_d and
not as an api reference.

=head2 Primary goal and purpose

The mono_handle_d is a process which takes care of the (de)allocation
of scratch shared memory and handles (of files, threads, mutexes,
sockets etc. see L<WapiHandleType>) and refcounts of the
filehandles. It is designed to be run by a user and to be fast, thus
minimal error checking on input is done and will most likely crash if
given a faulty package. No effort has been, or should be, made to have
the daemon talking to machine of different endianness/size of int.

=head2 How to start the daemon

To start the daemon you either run the mono_handle_d executable or try
to attach to the shared memory segment via L<_wapi_shm_attach> which
will start a daemon if one does not exist.

=head1 Internal details

The daemon works by opening a socket and listening to clients. These
clients send packages over the socket complying to L<struct
WapiHandleRequest>.

=head2 Possible requests

=over

=item WapiHandleRequest_New

Find a handle in the shared memory segment that is free and allocate
it to the specified type. To destroy use
L</WapiHandleRequest_Close>. A L<WapiHandleResponse> with
.type=WapiHandleResponseType_New will be sent back with .u.new.handle
set to the handle that was allocated. .u.new.type is the type that was
requested.

=item WapiHandleRequestType_Open

Increase the ref count of an already created handle. A
L<WapiHandleResponse> with .type=WapiHandleResponseType_Open will be sent
back with .u.new.handle set to the handle, .u.new.type is set to the
type of handle this is.

=item WapiHandleRequestType_Close

Decrease the ref count of an already created handle. A
L<WapiHandleResponse> with .type=WapiHandleResponseType_Close will be
sent back with .u.close.destroy set to TRUE if ref count for this
client reached 0.

=item WapiHandleRequestType_Scratch

Allocate a shared memory area of size .u.scratch.length in bytes. A
L<WapiHandleResponse> with .type=WapiHandleResponseType_Scratch will be
sent back with .u.scratch.idx set to the index into the shared
memory's scratch area where to memory begins. (works just like
malloc(3))

=item WapiHandleRequestType_Scratch

Deallocate a shared memory area, this must have been allocated before
deallocating. A L<WapiHandleResponse> with
.type=WapiHandleResponseType_ScratchFree will be sent back (works just
like free(3))

=back

=head1 Why a daemon

From an email:

Dennis: I just have one question about the daemon... Why does it
exist? Isn't it better performancewise to just protect the shared area
with a mutex when allocation a new handle/shared mem segment or
changing refcnt? It will however be a less resilient to clients that
crash (the deamon cleans up ref'd handles if socket closes)

Dick: It's precisely because with a mutex the shared memory segment
can be left in a locked state. Also, it's not so easy to clean up
shared memory without it (you can't just mark it deleted when creating
it, because you can't attach any more readers to the same segment
after that). I did some minimal performance testing, and I don't
think the daemon is particularly slow.


=head1 Authors

Documentaion: Dennis Haney

Implementation: Dick Porter

=cut
13 changes: 13 additions & 0 deletions mono/io-layer/ChangeLog
@@ -1,3 +1,16 @@
2002-07-10 Dennis Haney <davh@davh.dk>

* shared.c:
* handles.c:
* daemon.c: Lots of documentation, some added error checking, and
code readability improvements.

* daemon-messages.h: Add the Error request type to improve error
checking.

* daemon-messages.c: Do a bit more error checking on send() and
recv(), and log errors with a higher severity level.

2002-07-04 Dick Porter <dick@ximian.com>

* daemon.c (process_process_fork): Fix argument handling, due to
Expand Down
15 changes: 9 additions & 6 deletions mono/io-layer/daemon-messages.c
Expand Up @@ -22,7 +22,7 @@

/* Send request on fd, wait for response (called by applications, not
* the daemon)
*/
*/
void _wapi_daemon_request_response (int fd, WapiHandleRequest *req,
WapiHandleResponse *resp)
{
Expand All @@ -46,7 +46,7 @@ void _wapi_daemon_request_response (int fd, WapiHandleRequest *req,
#endif
if(ret!=sizeof(WapiHandleRequest)) {
if(errno==EPIPE) {
g_warning (G_GNUC_PRETTY_FUNCTION ": The handle daemon vanished!");
g_critical (G_GNUC_PRETTY_FUNCTION ": The handle daemon vanished!");
exit (-1);
} else {
g_warning (G_GNUC_PRETTY_FUNCTION ": Send error: %s",
Expand All @@ -63,7 +63,7 @@ void _wapi_daemon_request_response (int fd, WapiHandleRequest *req,
#endif
if(ret==-1) {
if(errno==EPIPE) {
g_warning (G_GNUC_PRETTY_FUNCTION ": The handle daemon vanished!");
g_critical (G_GNUC_PRETTY_FUNCTION ": The handle daemon vanished!");
exit (-1);
} else {
g_warning (G_GNUC_PRETTY_FUNCTION ": Send error: %s",
Expand All @@ -85,7 +85,10 @@ void _wapi_daemon_request (int fd, WapiHandleRequest *req)
#else
ret=recv (fd, req, sizeof(WapiHandleRequest), 0);
#endif
if(ret==-1) {
if(ret==-1 || ret!= sizeof(WapiHandleRequest)) {
/* Make sure we dont do anything with this response */
req->type=WapiHandleRequestType_Error;

#ifdef DEBUG
g_warning (G_GNUC_PRETTY_FUNCTION ": Recv error: %s",
strerror (errno));
Expand All @@ -104,11 +107,11 @@ void _wapi_daemon_response (int fd, WapiHandleResponse *resp)
#else
ret=send (fd, resp, sizeof(WapiHandleResponse), 0);
#endif
if(ret==-1) {
#ifdef DEBUG
if(ret==-1 || ret != sizeof(WapiHandleResponse)) {
g_warning (G_GNUC_PRETTY_FUNCTION ": Send error: %s",
strerror (errno));
#endif
/* The next loop around poll() should tidy up */
}
#endif
}
1 change: 1 addition & 0 deletions mono/io-layer/daemon-messages.h
Expand Up @@ -13,6 +13,7 @@
#include <mono/io-layer/wapi-private.h>

typedef enum {
WapiHandleRequestType_Error,
WapiHandleRequestType_New,
WapiHandleRequestType_Open,
WapiHandleRequestType_Close,
Expand Down
6 changes: 6 additions & 0 deletions mono/io-layer/daemon-private.h
Expand Up @@ -10,6 +10,12 @@
#ifndef _WAPI_DAEMON_PRIVATE_H_
#define _WAPI_DAEMON_PRIVATE_H_

typedef enum {
DAEMON_STARTING = 0,
DAEMON_RUNNING = 1,
DAEMON_DIED_AT_STARTUP = 2,
} _wapi_daemon_status;

extern void _wapi_daemon_main (void);

#endif /* _WAPI_DAEMON_PRIVATE_H_ */

0 comments on commit 8341073

Please sign in to comment.