1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef BOINC_APP_IPC_H
19 #define BOINC_APP_IPC_H
20 
21 #ifdef __cplusplus
22 
23 #include <vector>
24 #include <string>
25 #include <cstdio>
26 
27 #include "filesys.h"
28 #include "hostinfo.h"
29 #include "proxy_info.h"
30 #include "prefs.h"
31 #include "common_defs.h"
32 
33 // Communication between the core client and the BOINC app library.
34 // This code is linked into both core client and app lib.
35 //
36 // Some apps may involve separate "coordinator" and "worker" programs.
37 // The coordinator runs one or more worker programs in sequence,
38 // and don't do work themselves.
39 //
40 // Includes the following:
41 // - shared memory (APP_CLIENT_SHM)
42 // - main init file
43 // - fd init file
44 // - graphics init file
45 // - conversion of symbolic links
46 
47 // Shared memory is a set of MSG_CHANNELs.
48 // First byte of a channel is nonzero if
49 // the channel contains an unread data.
50 // This is set by the sender and cleared by the receiver.
51 // The sender doesn't write if the flag is set.
52 // Remaining 1023 bytes contain data.
53 //
54 #define MSG_CHANNEL_SIZE 1024
55 
56 struct MSG_CHANNEL {
57     char buf[MSG_CHANNEL_SIZE];
58     bool get_msg(char*);    // returns a message and clears pending flag
has_msgMSG_CHANNEL59     inline bool has_msg() {
60         return buf[0]?true:false;
61     }
62     bool send_msg(const char*);   // if there is not a message in the segment,
63                             // writes specified message and sets pending flag
64     void send_msg_overwrite(const char*);
65                             // write message, overwriting any msg already there
66 };
67 
68 struct SHARED_MEM {
69     MSG_CHANNEL process_control_request;
70         // core->app
71         // <quit/>
72         // <suspend/>
73         // <resume/>
74     MSG_CHANNEL process_control_reply;
75         // app->core
76     MSG_CHANNEL graphics_request;
77         // core->app
78         // not currently used
79     MSG_CHANNEL graphics_reply;
80         // app->core
81         // <web_graphics_url>
82         // <remote_desktop_addr>
83     MSG_CHANNEL heartbeat;
84         // core->app
85         // <heartbeat/>         sent every second, even while app is suspended
86         // <wss>                app's current working set size
87         // <max_wss>            max working set size
88     MSG_CHANNEL app_status;
89         // app->core
90         // status message every second, of the form
91         // <current_cpu_time>...
92         // <checkpoint_cpu_time>...
93         // <working_set_size>...
94         // <fraction_done> ...
95     MSG_CHANNEL trickle_up;
96         // app->core
97         // <have_new_trickle_up/>
98     MSG_CHANNEL trickle_down;
99         // core->app
100         // <have_new_trickle_down/>
101 };
102 
103 // MSG_QUEUE provides a queuing mechanism for shared-mem messages
104 // (which don't have one otherwise)
105 //
106 struct MSG_QUEUE {
107     std::vector<std::string> msgs;
108     char name[256];
109 	double last_block;	// last time we found message channel full
110 	void init(char*);
111     void msg_queue_send(const char*, MSG_CHANNEL& channel);
112     void msg_queue_poll(MSG_CHANNEL& channel);
113 	int msg_queue_purge(const char*);
114 	bool timeout(double);
115 };
116 
117 #define DEFAULT_CHECKPOINT_PERIOD               300
118 
119 #define SHM_PREFIX          "shm_"
120 #define QUIT_PREFIX         "quit_"
121 
122 class APP_CLIENT_SHM {
123 public:
124     SHARED_MEM *shm;
125 
126     void reset_msgs();        // resets all messages and clears their flags
127 
128     APP_CLIENT_SHM();
129 };
130 
131 #ifdef _WIN32
132     typedef char SHMEM_SEG_NAME[256];
133 #else
134     typedef int SHMEM_SEG_NAME;
135 #endif
136 
137 // parsed version of main init file
138 // If you add anything here, update copy()
139 //
140 struct APP_INIT_DATA {
141     int major_version;          // BOINC client version info
142     int minor_version;
143     int release;
144     int app_version;
145     char app_name[256];
146     char symstore[256];         // symstore URL (Windows)
147     char acct_mgr_url[256];
148         // if client is using account manager, its URL
149     char* project_preferences;
150         // project prefs XML
151     int userid;
152         // project's DB ID for this user/team/host
153     int teamid;
154     int hostid;
155     char user_name[256];
156     char team_name[256];
157     char project_dir[256];      // where project files are stored on host
158     char boinc_dir[MAXPATHLEN];        // BOINC data directory
159     char wu_name[256];          // workunit name
160     char result_name[256];
161     char authenticator[256];    // user's authenticator
162     int slot;                   // the slot this job is running in (0, 1, ...)
163     int client_pid;             // process ID of BOINC client
164     double user_total_credit;
165     double user_expavg_credit;
166     double host_total_credit;
167     double host_expavg_credit;
168     double resource_share_fraction;     // this project's resource share frac
169     HOST_INFO host_info;
170     PROXY_INFO proxy_info;      // in case app wants to use network
171     GLOBAL_PREFS global_prefs;
172     double starting_elapsed_time;   // elapsed time, counting previous episodes
173     bool using_sandbox;         // client is using account-based sandboxing
174     bool vm_extensions_disabled;
175         // client has already been notified that the VM extensions of
176         // the processor have been disabled
177 
178     // info about the WU
179     double rsc_fpops_est;
180     double rsc_fpops_bound;
181     double rsc_memory_bound;
182     double rsc_disk_bound;
183     double computation_deadline;
184 
185     // the following are used for compound apps,
186     // where each stage of the computation is a fixed fraction of the total.
187     //
188     double fraction_done_start;
189     double fraction_done_end;
190 
191     // info for GPU apps
192     //
193     char gpu_type[64];
194     int gpu_device_num;
195     int gpu_opencl_dev_index;
196     double gpu_usage;   // APP_VERSION.gpu_usage.usage
197 
198     // info for multicore apps: how many cores to use
199     //
200     double ncpus;
201 
202     // client configuration info
203     //
204     bool vbox_window;       // whether to open a console window for VM apps
205 
206     // list of files in the app version (for wrappers)
207     //
208     std::vector<std::string> app_files;
209 
210     // Items used by the BOINC runtime system
211     //
212     double checkpoint_period;     // recommended checkpoint period
213     SHMEM_SEG_NAME shmem_seg_name;
214     double wu_cpu_time;       // cpu time from previous episodes
215 
216     APP_INIT_DATA();
217     APP_INIT_DATA(const APP_INIT_DATA&);  // copy constructor
218     APP_INIT_DATA &operator=(const APP_INIT_DATA&);
219     void copy(const APP_INIT_DATA&);      // actually do the copy here
220     void clear();
221     ~APP_INIT_DATA();
222 };
223 
224 struct GRAPHICS_INFO {
225     int xsize;
226     int ysize;
227     double refresh_period;
228 };
229 
230 typedef struct GRAPHICS_INFO GRAPHICS_INFO;
231 
232 int write_init_data_file(FILE* f, APP_INIT_DATA&);
233 int parse_init_data_file(FILE* f, APP_INIT_DATA&);
234 int write_graphics_file(FILE* f, GRAPHICS_INFO* gi);
235 int parse_graphics_file(FILE* f, GRAPHICS_INFO* gi);
236 
237 // filenames used in the slot directory
238 //
239 #define INIT_DATA_FILE    "init_data.xml"
240 #define BOINC_FINISH_CALLED_FILE "boinc_finish_called"
241 #define TEMPORARY_EXIT_FILE "boinc_temporary_exit"
242 #define TRICKLE_UP_FILENAME "trickle_up.xml"
243 #define STDERR_FILE           "stderr.txt"
244 #define STDOUT_FILE           "stdout.txt"
245 #define LOCKFILE               "boinc_lockfile"
246 #define UPLOAD_FILE_REQ_PREFIX      "boinc_ufr_"
247 #define UPLOAD_FILE_STATUS_PREFIX   "boinc_ufs_"
248 
249 // other filenames
250 #define PROJECT_DIR "projects"
251 
252 extern int boinc_link(const char* phys_name, const char* logical_name);
253 extern int boinc_resolve_filename_s(const char*, std::string&);
254 extern std::string resolve_soft_link(const char* project_dir, const char* file);
255 extern void url_to_project_dir(char* url, char* dir, int dirsize);
256 
257 extern "C" {
258 #endif
259 
260 extern int boinc_resolve_filename(const char*, char*, int len);
261 
262 #ifdef __cplusplus
263 } // extern "C" {
264 #endif
265 
266 #endif
267