Release 1.5.29.
[wine] / server / snapshot.c
1 /*
2  * Server-side snapshots
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * FIXME: heap snapshots not implemented
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
40
41
42 struct snapshot
43 {
44     struct object             obj;           /* object header */
45     struct process_snapshot  *processes;     /* processes snapshot */
46     int                       process_count; /* count of processes */
47     int                       process_pos;   /* current position in proc snapshot */
48     struct thread_snapshot   *threads;       /* threads snapshot */
49     int                       thread_count;  /* count of threads */
50     int                       thread_pos;    /* current position in thread snapshot */
51 };
52
53 static void snapshot_dump( struct object *obj, int verbose );
54 static void snapshot_destroy( struct object *obj );
55
56 static const struct object_ops snapshot_ops =
57 {
58     sizeof(struct snapshot),      /* size */
59     snapshot_dump,                /* dump */
60     no_get_type,                  /* get_type */
61     no_add_queue,                 /* add_queue */
62     NULL,                         /* remove_queue */
63     NULL,                         /* signaled */
64     NULL,                         /* satisfied */
65     no_signal,                    /* signal */
66     no_get_fd,                    /* get_fd */
67     no_map_access,                /* map_access */
68     default_get_sd,               /* get_sd */
69     default_set_sd,               /* set_sd */
70     no_lookup_name,               /* lookup_name */
71     no_open_file,                 /* open_file */
72     no_close_handle,              /* close_handle */
73     snapshot_destroy              /* destroy */
74 };
75
76
77 /* create a new snapshot */
78 static struct snapshot *create_snapshot( unsigned int flags )
79 {
80     struct snapshot *snapshot;
81
82     if (!(snapshot = alloc_object( &snapshot_ops ))) return NULL;
83
84     snapshot->process_pos = 0;
85     snapshot->process_count = 0;
86     if (flags & SNAP_PROCESS)
87         snapshot->processes = process_snap( &snapshot->process_count );
88
89     snapshot->thread_pos = 0;
90     snapshot->thread_count = 0;
91     if (flags & SNAP_THREAD)
92         snapshot->threads = thread_snap( &snapshot->thread_count );
93
94     return snapshot;
95 }
96
97 /* get the next process in the snapshot */
98 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
99 {
100     struct process_snapshot *ptr;
101     struct process_dll *exe_module;
102
103     if (!snapshot->process_count)
104     {
105         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
106         return 0;
107     }
108     if (snapshot->process_pos >= snapshot->process_count)
109     {
110         set_error( STATUS_NO_MORE_FILES );
111         return 0;
112     }
113     ptr = &snapshot->processes[snapshot->process_pos++];
114     reply->count    = ptr->count;
115     reply->pid      = get_process_id( ptr->process );
116     reply->ppid     = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
117     reply->threads  = ptr->threads;
118     reply->priority = ptr->priority;
119     reply->handles  = ptr->handles;
120     reply->unix_pid = ptr->process->unix_pid;
121     if ((exe_module = get_process_exe_module( ptr->process )) && exe_module->filename)
122     {
123         data_size_t len = min( exe_module->namelen, get_reply_max_size() );
124         set_reply_data( exe_module->filename, len );
125     }
126     return 1;
127 }
128
129 /* get the next thread in the snapshot */
130 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
131 {
132     struct thread_snapshot *ptr;
133
134     if (!snapshot->thread_count)
135     {
136         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
137         return 0;
138     }
139     if (snapshot->thread_pos >= snapshot->thread_count)
140     {
141         set_error( STATUS_NO_MORE_FILES );
142         return 0;
143     }
144     ptr = &snapshot->threads[snapshot->thread_pos++];
145     reply->count     = ptr->count;
146     reply->pid       = get_process_id( ptr->thread->process );
147     reply->tid       = get_thread_id( ptr->thread );
148     reply->base_pri  = ptr->priority;
149     reply->delta_pri = 0;  /* FIXME */
150     return 1;
151 }
152
153 static void snapshot_dump( struct object *obj, int verbose )
154 {
155     struct snapshot *snapshot = (struct snapshot *)obj;
156     assert( obj->ops == &snapshot_ops );
157     fprintf( stderr, "Snapshot: %d procs %d threads\n",
158              snapshot->process_count, snapshot->thread_count );
159 }
160
161 static void snapshot_destroy( struct object *obj )
162 {
163     int i;
164     struct snapshot *snapshot = (struct snapshot *)obj;
165     assert( obj->ops == &snapshot_ops );
166     if (snapshot->process_count)
167     {
168         for (i = 0; i < snapshot->process_count; i++)
169             release_object( snapshot->processes[i].process );
170         free( snapshot->processes );
171     }
172     if (snapshot->thread_count)
173     {
174         for (i = 0; i < snapshot->thread_count; i++)
175             release_object( snapshot->threads[i].thread );
176         free( snapshot->threads );
177     }
178 }
179
180 /* create a snapshot */
181 DECL_HANDLER(create_snapshot)
182 {
183     struct snapshot *snapshot;
184
185     reply->handle = 0;
186     if ((snapshot = create_snapshot( req->flags )))
187     {
188         reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
189         release_object( snapshot );
190     }
191 }
192
193 /* get the next process from a snapshot */
194 DECL_HANDLER(next_process)
195 {
196     struct snapshot *snapshot;
197
198     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
199                                                        0, &snapshot_ops )))
200     {
201         if (req->reset) snapshot->process_pos = 0;
202         snapshot_next_process( snapshot, reply );
203         release_object( snapshot );
204     }
205 }
206
207 /* get the next thread from a snapshot */
208 DECL_HANDLER(next_thread)
209 {
210     struct snapshot *snapshot;
211
212     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
213                                                        0, &snapshot_ops )))
214     {
215         if (req->reset) snapshot->thread_pos = 0;
216         snapshot_next_thread( snapshot, reply );
217         release_object( snapshot );
218     }
219 }