Tried to add an error message when crashing on a selector load because
[wine] / server / snapshot.c
1 /*
2  * Server-side snapshots
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * FIXME: only process snapshots implemented for now
7  */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "winerror.h"
14 #include "winnt.h"
15 #include "tlhelp32.h"
16
17 #include "handle.h"
18 #include "process.h"
19 #include "thread.h"
20
21
22 struct snapshot
23 {
24     struct object             obj;           /* object header */
25     struct process_snapshot  *process;       /* processes snapshot */
26     int                       process_count; /* count of processes */
27     int                       process_pos;   /* current position in proc snapshot */
28 };
29
30 static void snapshot_dump( struct object *obj, int verbose );
31 static void snapshot_destroy( struct object *obj );
32
33 static const struct object_ops snapshot_ops =
34 {
35     snapshot_dump,
36     no_add_queue,
37     NULL,  /* should never get called */
38     NULL,  /* should never get called */
39     NULL,  /* should never get called */
40     no_read_fd,
41     no_write_fd,
42     no_flush,
43     no_get_file_info,
44     snapshot_destroy
45 };
46
47
48 /* create a new snapshot */
49 static struct object *create_snapshot( int flags )
50 {
51     struct snapshot *snapshot;
52     if (!(snapshot = mem_alloc( sizeof(*snapshot) ))) return NULL;
53     init_object( &snapshot->obj, &snapshot_ops, NULL );
54     if (flags & TH32CS_SNAPPROCESS)
55         snapshot->process = process_snap( &snapshot->process_count );
56     else
57         snapshot->process_count = 0;
58
59     snapshot->process_pos = 0;
60     return &snapshot->obj;
61 }
62
63 /* get the next process in the snapshot */
64 static int snapshot_next_process( int handle, int reset, struct next_process_reply *reply )
65 {
66     struct snapshot *snapshot;
67     struct process_snapshot *ptr;
68     if (!(snapshot = (struct snapshot *)get_handle_obj( current->process, handle,
69                                                         0, &snapshot_ops )))
70         return 0;
71     if (!snapshot->process_count)
72     {
73         SET_ERROR( ERROR_INVALID_PARAMETER );  /* FIXME */
74         release_object( snapshot );
75         return 0;
76     }
77     if (reset) snapshot->process_pos = 0;
78     else if (snapshot->process_pos >= snapshot->process_count)
79     {
80         SET_ERROR( ERROR_NO_MORE_FILES );
81         release_object( snapshot );
82         return 0;
83     }
84     ptr = &snapshot->process[snapshot->process_pos++];
85     reply->pid      = ptr->process;
86     reply->threads  = ptr->threads;
87     reply->priority = ptr->priority;
88     release_object( snapshot );
89     return 1;
90 }
91
92 static void snapshot_dump( struct object *obj, int verbose )
93 {
94     struct snapshot *snapshot = (struct snapshot *)obj;
95     assert( obj->ops == &snapshot_ops );
96     fprintf( stderr, "Snapshot: %d processes\n",
97              snapshot->process_count );
98 }
99
100 static void snapshot_destroy( struct object *obj )
101 {
102     int i;
103     struct snapshot *snapshot = (struct snapshot *)obj;
104     assert( obj->ops == &snapshot_ops );
105     if (snapshot->process_count)
106     {
107         for (i = 0; i < snapshot->process_count; i++)
108             release_object( snapshot->process[i].process );
109         free( snapshot->process );
110     }
111     free( snapshot );
112 }
113
114 /* create a snapshot */
115 DECL_HANDLER(create_snapshot)
116 {
117     struct object *obj;
118     struct create_snapshot_reply reply = { -1 };
119
120     if ((obj = create_snapshot( req->flags )))
121     {
122         reply.handle = alloc_handle( current->process, obj, 0, req->inherit );
123         release_object( obj );
124     }
125     send_reply( current, -1, 1, &reply, sizeof(reply) );
126 }
127
128 /* get the next process from a snapshot */
129 DECL_HANDLER(next_process)
130 {
131     struct next_process_reply reply;
132     snapshot_next_process( req->handle, req->reset, &reply );
133     send_reply( current, -1, 1, &reply, sizeof(reply) );
134 }