Cygwin: sched_setscheduler: allow changes of the priority
[newlib-cygwin.git] / winsup / cygwin / local_includes / pinfo.h
blob463f0e8518e3e51cd53db3b593244e092463b38d
1 /* pinfo.h: process table info
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 #pragma once
11 #include <sys/resource.h>
12 #include "thread.h"
14 union commune_result
16 struct {
17 char *s;
18 size_t n;
20 struct {
21 sigset_t pnd;
22 sigset_t blk;
23 sigset_t ign;
27 enum picom
29 PICOM_EXTRASTR = 0x80000000,
30 PICOM_CMDLINE = 1,
31 PICOM_CWD = 2,
32 PICOM_ROOT = 3,
33 PICOM_FDS = 4,
34 PICOM_FD = 5,
35 PICOM_PIPE_FHANDLER = 6,
36 PICOM_FILE_PATHCONV = 7,
37 PICOM_ENVIRON = 8,
38 PICOM_SIGINFO = 9
41 #define EXITCODE_SET 0x8000000
42 #define EXITCODE_NOSET 0x4000000
43 #define EXITCODE_RETRY 0x2000000
44 #define EXITCODE_OK 0x1000000
45 #define EXITCODE_FORK_FAILED 0x0800000
47 class fhandler_pipe;
49 class _pinfo
51 public:
52 /* Cygwin pid */
53 pid_t pid;
55 /* Various flags indicating the state of the process. See PID_
56 constants in <sys/cygwin.h>. */
57 DWORD process_state;
59 pid_t ppid; /* Parent process id. */
61 DWORD exitcode; /* set when process exits */
63 /* > 0 if started by a cygwin process */
64 DWORD cygstarted;
66 /* dwProcessId contains the processid used for sending signals. It
67 will be reset in a child process when it is capable of receiving
68 signals. */
69 DWORD dwProcessId;
71 /* Used to spawn a child for fork(), among other things. The other
72 members of _pinfo take only a bit over 200 bytes. So cut off a
73 couple of bytes from progname to allow the _pinfo structure not
74 to exceed 64K. Otherwise it blocks another 64K block of VM for
75 the process. */
76 WCHAR progname[NT_MAX_PATH - 512];
78 /* User information.
79 The information is derived from the GetUserName system call,
80 with the name looked up in /etc/passwd and assigned a default value
81 if not found. This data resides in the shared data area (allowing
82 tasks to store whatever they want here) so it's for informational
83 purposes only. */
84 uid_t uid; /* User ID */
85 gid_t gid; /* Group ID */
86 pid_t pgid; /* Process group ID */
87 pid_t sid; /* Session ID */
88 int ctty; /* Control tty */
89 bool has_pgid_children;/* True if we've forked or spawned children with our GID. */
91 /* Resources used by process. */
92 long start_time;
93 struct rusage rusage_self;
94 struct rusage rusage_children;
95 int nice;
97 /* Non-zero if process was stopped by a signal. */
98 char stopsig;
100 inline void set_has_pgid_children ()
102 if (pgid == pid)
103 has_pgid_children = 1;
106 inline void set_has_pgid_children (bool val) {has_pgid_children = val;}
108 commune_result commune_request (__uint32_t, ...);
109 bool alive ();
110 fhandler_pipe *pipe_fhandler (int64_t, size_t &);
111 void *file_pathconv (int, uint32_t, size_t &);
112 char *fd (int fd, size_t &);
113 char *fds (size_t &);
114 char *root (size_t &);
115 char *cwd (size_t &);
116 char *cmdline (size_t &);
117 char *environ (size_t &);
118 char *win_heap_info (size_t &);
119 int siginfo (sigset_t &, sigset_t &, sigset_t &);
120 bool set_ctty (class fhandler_termios *, int);
121 bool alert_parent (char);
122 int kill (siginfo_t&);
123 bool exists ();
124 const char *_ctty (char *);
126 /* signals */
127 HANDLE sendsig;
128 HANDLE exec_sendsig;
129 DWORD exec_dwProcessId;
130 public:
131 friend class pinfo_minimal;
134 DWORD commune_process (void *);
136 enum parent_alerter
138 __ALERT_REPARENT = 111, // arbitrary non-signal value
139 __ALERT_ALIVE = 112
142 class pinfo_minimal
144 HANDLE h;
145 public:
146 HANDLE hProcess;
147 HANDLE rd_proc_pipe;
148 pinfo_minimal (): h (NULL), hProcess (NULL), rd_proc_pipe (NULL) {}
149 void set_rd_proc_pipe (HANDLE& h) {rd_proc_pipe = h;}
150 void set_inheritance (bool);
151 friend class pinfo;
154 class pinfo: public pinfo_minimal
156 bool destroy;
157 HANDLE winpid_hdl;
158 _pinfo *procinfo;
159 public:
160 bool waiter_ready;
161 class cygthread *wait_thread;
163 void init (pid_t, DWORD, HANDLE);
164 pinfo (_pinfo *x = NULL)
165 : pinfo_minimal (), destroy (false), winpid_hdl (NULL), procinfo (x),
166 waiter_ready (false), wait_thread (NULL) {}
167 pinfo (pid_t n, DWORD flag = 0)
168 : pinfo_minimal (), destroy (false), winpid_hdl (NULL), procinfo (NULL),
169 waiter_ready (false), wait_thread (NULL)
171 init (n, flag, NULL);
173 pinfo (HANDLE, pinfo_minimal&, pid_t);
174 void thisproc (HANDLE);
175 void create_winpid_symlink ();
176 inline void _pinfo_release ();
177 void release ();
178 bool wait ();
179 ~pinfo ()
181 if (destroy && procinfo)
182 release ();
184 void exit (DWORD n) __attribute__ ((noreturn, ));
185 void maybe_set_exit_code_from_windows ();
186 void set_exit_code (DWORD n);
187 _pinfo *operator -> () const {return procinfo;}
188 int operator == (pinfo *x) const {return x->procinfo == procinfo;}
189 int operator == (pinfo &x) const {return x.procinfo == procinfo;}
190 int operator == (_pinfo *x) const {return x == procinfo;}
191 int operator == (void *x) const {return procinfo == x;}
192 _pinfo *operator * () const {return procinfo;}
193 operator _pinfo * () const {return procinfo;}
194 int operator !() const {return !procinfo;}
195 void preserve () { destroy = false; }
196 void allow_remove () { destroy = true; }
197 #ifndef SIG_BAD_MASK // kludge to ensure that sigproc.h included
198 // int attach () {system_printf ("attach is not here"); return 0;}
199 // int remember (bool) {system_printf ("remember is not here"); return 0;}
200 #else
201 int attach ()
203 int res = proc_subproc (PROC_ATTACH_CHILD, (uintptr_t) this);
204 destroy = res ? false : true;
205 return res;
207 int remember ()
209 int res = proc_subproc (PROC_ADD_CHILD, (uintptr_t) this);
210 destroy = res ? false : true;
211 return res;
213 #endif
214 HANDLE shared_handle () {return h;}
215 HANDLE shared_winpid_handle () {return winpid_hdl;}
216 void set_acl ();
217 friend class _pinfo;
218 friend class winpids;
219 private:
220 DWORD status_exit (DWORD);
223 #define MAX_PID 65536
225 #define ISSTATE(p, f) (!!((p)->process_state & f))
226 #define NOTSTATE(p, f) (!((p)->process_state & f))
228 class winpids
230 bool make_copy;
231 DWORD npidlist;
232 DWORD *pidlist;
233 pinfo *pinfolist;
234 DWORD pinfo_access; // access type for pinfo open
235 DWORD enum_processes (bool winpid);
236 DWORD enum_init (bool winpid);
237 void add (DWORD& nelem, bool, DWORD pid);
238 public:
239 DWORD npids;
240 inline void reset () { release (); npids = 0;}
241 void set (bool winpid);
242 winpids (): make_copy (true) {}
243 winpids (DWORD acc): make_copy (false), npidlist (0), pidlist (NULL),
244 pinfolist (NULL), pinfo_access (acc), npids (0)
246 set (0);
248 inline DWORD& winpid (int i) const {return pidlist[i];}
249 inline _pinfo *operator [] (int i) const {return (_pinfo *) pinfolist[i];}
250 ~winpids ();
251 void release ();
254 pid_t create_cygwin_pid ();
255 pid_t cygwin_pid (DWORD);
257 void pinfo_init (char **, int);
258 extern pinfo myself;
260 /* Helper class to allow convenient setting and unsetting a process_state
261 flag in myself. This is used in certain fhandler read/write methods
262 to set the PID_TTYIN/PID_TTYOU flags in myself->process_state. */
263 class push_process_state
265 private:
266 int flag;
267 public:
268 push_process_state (int add_flag)
270 flag = add_flag;
271 myself->process_state |= flag;
273 void pop () { myself->process_state &= ~(flag); }
274 ~push_process_state () { pop (); }
277 #define _P_VFORK 0
278 #define _P_SYSTEM 512
279 /* Add this flag in calls to child_info_spawn::worker if the calling function
280 is one of 'p' type functions: execlp, execvp, spawnlp, spawnvp. Per POSIX,
281 only these p-type functions fall back to call /bin/sh if the file is not a
282 binary. The setting of _P_PATH_TYPE_EXEC is used as a bool value in
283 av::fixup to decide if the file should be evaluated as a script, or if
284 ENOEXEC should be returned. */
285 #define _P_PATH_TYPE_EXEC 0x1000
287 /* Helper macro to mask actual mode and drop additional flags defined above. */
288 #define _P_MODE(x) ((x) & 0xfff)
290 #define __ctty() _ctty ((char *) alloca (sizeof ("ctty /dev/tty") + 20))
291 #define myctty() myself->__ctty ()
293 /* For mmaps across fork(). */
294 int fixup_mmaps_after_fork (HANDLE parent);
295 /* for shm areas across fork (). */
296 int fixup_shms_after_fork ();
298 void fill_rusage (struct rusage *, HANDLE);
299 void add_rusage (struct rusage *, struct rusage *);