diff --git a/api/boinc_api.C b/api/boinc_api.C index 02eb6d90d6..04bfb10b6d 100644 --- a/api/boinc_api.C +++ b/api/boinc_api.C @@ -176,6 +176,64 @@ static int boinc_worker_thread_cpu_time(double& cpu) { return 0; } +// Apparently sprintf() isn't safe to use in a signal handler. +// So roll our own. +// +void itoa(int x, char* &p) { + if (x<0) { + x = -x; + *p++ = '-'; + } + int q = x/10; + int r = x-q*10; + if (q) { + itoa(q, p); + } + *p = '0'+r; + p++; + *p = 0; + +} + +// positive only +// +void dtoa(double x, char* p) { + int exp = 0; + while (x > 1) { + x /= 10; + exp++; + } + while (x<0.1) { + x *= 10; + exp--; + } + strcpy(p, "0."); + p += 2; + int n = (int)(x*1e8); + itoa(n, p); + if (exp) { + *p++ = 'e'; + itoa(exp, p); + } +} + +void append(const char* from, char* &to) { + while (*to++ = *from++) { + } +} + +void tag_double(const char* tag, double x, char* p) { + char buf[256]; + append("<", p); + append(tag, p); + append(">", p); + dtoa(x, buf); + append(buf, p); + append("\n", p); +} + // communicate to the core client (via shared mem) // the current CPU time and fraction done // @@ -186,31 +244,30 @@ static bool update_app_progress( if (standalone) return true; - sprintf(msg_buf, - "%.15e\n" - "%.15e\n", - cpu_t, cp_cpu_t - ); + tag_double("current_cpu_time", cpu_t, buf); + strcpy(msg_buf, buf); + tag_double("checkpoint_cpu_time", cp_cpu_t, buf); + strcat(msg_buf, buf); if (fraction_done >= 0) { double range = aid.fraction_done_end - aid.fraction_done_start; double fdone = aid.fraction_done_start + fraction_done*range; - sprintf(buf, "%2.8f\n", fdone); + tag_double("fraction_done", fdone, buf); strcat(msg_buf, buf); } if (rss) { - sprintf(buf, "%f\n", rss); + tag_double("rss_bytes", rss, buf); strcat(msg_buf, buf); } if (vm) { - sprintf(buf, "%f\n", vm); + tag_double("vm_bytes", vm, buf); strcat(msg_buf, buf); } if (fpops_per_cpu_sec) { - sprintf(buf, "%f\n", fpops_per_cpu_sec); + tag_double("fpops_per_cpu_sec", fpops_per_cpu_sec, buf); strcat(msg_buf, buf); } if (fpops_cumulative) { - sprintf(buf, "%f\n", fpops_cumulative); + tag_double("fpops_cumulative", fpops_cumulative, buf); strcat(msg_buf, buf); } return app_client_shm->shm->app_status.send_msg(msg_buf); diff --git a/checkin_notes b/checkin_notes index 083a9542e6..79b2d4968c 100755 --- a/checkin_notes +++ b/checkin_notes @@ -9782,3 +9782,9 @@ Rom 1 Aug 2005 librsaeuro.vcproj (Removed) win_build/installerv2/ boinc.ism + +David 1 Aug 2005 + - avoid using sprintf() in signal handler + + lib/ + boinc_api.C