Removed extraneous ERR message.
[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 #include "request.h"
21
22
23 struct snapshot
24 {
25     struct object             obj;           /* object header */
26     struct process_snapshot  *process;       /* processes snapshot */
27     int                       process_count; /* count of processes */
28     int                       process_pos;   /* current position in proc snapshot */
29 };
30
31 static void snapshot_dump( struct object *obj, int verbose );
32 static void snapshot_destroy( struct object *obj );
33
34 static const struct object_ops snapshot_ops =
35 {
36     sizeof(struct snapshot),      /* size */
37     snapshot_dump,                /* dump */
38     no_add_queue,                 /* add_queue */
39     NULL,                         /* remove_queue */
40     NULL,                         /* signaled */
41     NULL,                         /* satisfied */
42     NULL,                         /* get_poll_events */
43     NULL,                         /* poll_event */
44     no_read_fd,                   /* get_read_fd */
45     no_write_fd,                  /* get_write_fd */
46     no_flush,                     /* flush */
47     no_get_file_info,             /* get_file_info */
48     snapshot_destroy              /* destroy */
49 };
50
51
52 /* create a new snapshot */
53 static struct snapshot *create_snapshot( int flags )
54 {
55     struct snapshot *snapshot;
56
57     if ((snapshot = alloc_object( &snapshot_ops, -1 )))
58     {
59         if (flags & TH32CS_SNAPPROCESS)
60             snapshot->process = process_snap( &snapshot->process_count );
61         else
62             snapshot->process_count = 0;
63         snapshot->process_pos = 0;
64     }
65     return snapshot;
66 }
67
68 /* get the next process in the snapshot */
69 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
70 {
71     struct process_snapshot *ptr;
72
73     if (!snapshot->process_count)
74     {
75         set_error( ERROR_INVALID_PARAMETER );  /* FIXME */
76         return 0;
77     }
78     if (req->reset) snapshot->process_pos = 0;
79     else if (snapshot->process_pos >= snapshot->process_count)
80     {
81         set_error( ERROR_NO_MORE_FILES );
82         return 0;
83     }
84     ptr = &snapshot->process[snapshot->process_pos++];
85     req->pid      = ptr->process;
86     req->threads  = ptr->threads;
87     req->priority = ptr->priority;
88     return 1;
89 }
90
91 static void snapshot_dump( struct object *obj, int verbose )
92 {
93     struct snapshot *snapshot = (struct snapshot *)obj;
94     assert( obj->ops == &snapshot_ops );
95     fprintf( stderr, "Snapshot: %d processes\n",
96              snapshot->process_count );
97 }
98
99 static void snapshot_destroy( struct object *obj )
100 {
101     int i;
102     struct snapshot *snapshot = (struct snapshot *)obj;
103     assert( obj->ops == &snapshot_ops );
104     if (snapshot->process_count)
105     {
106         for (i = 0; i < snapshot->process_count; i++)
107             release_object( snapshot->process[i].process );
108         free( snapshot->process );
109     }
110 }
111
112 /* create a snapshot */
113 DECL_HANDLER(create_snapshot)
114 {
115     struct snapshot *snapshot;
116
117     req->handle = -1;
118     if ((snapshot = create_snapshot( req->flags )))
119     {
120         req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
121         release_object( snapshot );
122     }
123 }
124
125 /* get the next process from a snapshot */
126 DECL_HANDLER(next_process)
127 {
128     struct snapshot *snapshot;
129
130     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
131                                                        0, &snapshot_ops )))
132     {
133         snapshot_next_process( snapshot, req );
134         release_object( snapshot );
135     }
136 }