Content of /tmp/runapp must be added in atomic way (b/17234823)

Change-Id: Ibee28c3b144102939a4e88b1c1a147df49e79440
diff --git a/src/server/main.c b/src/server/main.c
index 97a861d..0668040 100644
--- a/src/server/main.c
+++ b/src/server/main.c
@@ -191,20 +191,26 @@
   const char * const script_args[] = {"/etc/init.d/S99miniclient", "restart", 0};
 
   /* Write application information to /tmp/runapp */
-  FILE *runapp = fopen("/tmp/runapp","w");
+  FILE *runapp = fopen("/tmp/runapp.tmp","w");
   if (!runapp) {
-    fprintf(stderr, "Couldn't open /tmp/runapp file\n");
+    fprintf(stderr, "Couldn't open /tmp/runapp.tmp file\n");
     return kDIALStatusStopped;
   }
   for(int i = 0; args[i]; ++i) {
     int outputCharacters = fprintf(runapp, "%s ", args[i]);
     if (outputCharacters<0) {
-      fprintf(stderr, "Error writing to /tmp/runapp file\n");
+      fprintf(stderr, "Error writing to /tmp/runapp.tmp file\n");
+      fclose(runapp);
+      return kDIALStatusStopped;
     }
   }
   fsync(fileno(runapp));
   fclose(runapp);
   runapp=NULL;
+  if (rename("/tmp/runapp.tmp", "/tmp/runapp")) {
+    fprintf(stderr, "Error renaming /tmp/runapp.tmp file\n");
+    return kDIALStatusStopped;
+  }
 
 
   pid_t pid = fork();