aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/cygserver.h
blob: 09ab78a9ff005873b6b3b892fd58346a00ba5cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* cygserver.h

   Copyright 2001, 2002, 2003, 2004 Red Hat Inc.

   Written by Egor Duda <deo@logos-m.ru>

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#ifndef _CYGSERVER_H_
#define _CYGSERVER_H_

#ifdef __GNUC__
#define CYGSERVER_PACKED __attribute__ ((packed))
#else
#define CYGSERVER_PACKED
#endif

#define CYGWIN_SERVER_VERSION_MAJOR	1
#define CYGWIN_SERVER_VERSION_API	3
#define CYGWIN_SERVER_VERSION_MINOR	0
#define CYGWIN_SERVER_VERSION_PATCH	0

typedef enum {
  CYGSERVER_UNKNOWN = 0,
  CYGSERVER_OK,
  CYGSERVER_UNAVAIL
} cygserver_states;

/*---------------------------------------------------------------------------*
 * class client_request
 *---------------------------------------------------------------------------*/

class transport_layer_base;

#ifndef __INSIDE_CYGWIN__
class process_cache;
#endif

class client_request
{
protected:
  typedef enum {
    CYGSERVER_REQUEST_INVALID,
    CYGSERVER_REQUEST_GET_VERSION,
    CYGSERVER_REQUEST_SHUTDOWN,
    CYGSERVER_REQUEST_ATTACH_TTY,
    CYGSERVER_REQUEST_MSG,
    CYGSERVER_REQUEST_SEM,
    CYGSERVER_REQUEST_SHM,
    CYGSERVER_REQUEST_LAST
  } request_code_t;

  struct header_t
  {
    size_t msglen;
    union
    {
      request_code_t request_code;
      ssize_t error_code;
    };

    header_t () {};
    header_t (request_code_t, size_t);
  } CYGSERVER_PACKED;

public:
#ifndef __INSIDE_CYGWIN__
  static void handle_request (transport_layer_base *, process_cache *);
#endif

  client_request (request_code_t request_code,
		  void *buf = NULL,
		  size_t bufsiz = 0);
  virtual ~client_request ();

  request_code_t request_code () const { return _header.request_code; }

  ssize_t error_code () const { return _header.error_code; };
  void error_code (ssize_t error_code) { _header.error_code = error_code; };

  size_t msglen () const { return _header.msglen; };
  void msglen (size_t len) { _header.msglen = len; };

  int make_request ();

protected:
  virtual void send (transport_layer_base *);

private:
  header_t _header;
  void * const _buf;
  const size_t _buflen;

#ifndef __INSIDE_CYGWIN__
  void handle (transport_layer_base *, process_cache *);
  virtual void serve (transport_layer_base *, process_cache *) = 0;
#endif
};

/*---------------------------------------------------------------------------*
 * class client_request_get_version
 *---------------------------------------------------------------------------*/

class client_request_get_version : public client_request
{
private:
  struct request_get_version
  {
    DWORD major, api, minor, patch;
  } CYGSERVER_PACKED;

public:
  client_request_get_version ();
  bool check_version () const;

private:
  struct request_get_version version;

#ifndef __INSIDE_CYGWIN__
  virtual void serve (transport_layer_base *, process_cache *);
#endif
};

/*---------------------------------------------------------------------------*
 * class client_request_shutdown
 *
 * Nb. This whole class is only !__INSIDE_CYGWIN__ since it is used
 * solely by cygserver itself.
 *---------------------------------------------------------------------------*/

#ifndef __INSIDE_CYGWIN__

class client_request_shutdown : public client_request
{
public:
  client_request_shutdown ();

private:
  virtual void serve (transport_layer_base *, process_cache *);
};

#endif /* !__INSIDE_CYGWIN__ */

/*---------------------------------------------------------------------------*
 * class client_request_attach_tty
 *---------------------------------------------------------------------------*/

class client_request_attach_tty : public client_request
{
private:
  struct request_attach_tty
  {
    DWORD pid, master_pid;
    HANDLE from_master, to_master;
  } CYGSERVER_PACKED;

public:
#ifdef __INSIDE_CYGWIN__
  client_request_attach_tty (DWORD nmaster_pid,
			     HANDLE nfrom_master, HANDLE nto_master);
#else
  client_request_attach_tty ();
#endif

  HANDLE from_master () const { return req.from_master; };
  HANDLE to_master () const { return req.to_master; };

protected:
  virtual void send (transport_layer_base *);

private:
  struct request_attach_tty req;

#ifndef __INSIDE_CYGWIN__
  virtual void serve (transport_layer_base *, process_cache *);
#endif
};

#ifndef __INSIDE_CYGWIN__
extern PSID admininstrator_group_sid;
#endif

extern bool check_cygserver_available ();
extern void cygserver_init ();

#endif /* _CYGSERVER_H_ */