+int __prepare_valgrind_outputfile(bundle *kb)
+{
+ const char *str = NULL;
+ const char **str_array = NULL;
+ int len = 0;
+ int i;
+
+ if(bundle_get_type(kb, DLP_K_VALGRIND_ARG) & BUNDLE_TYPE_ARRAY) {
+ str_array = bundle_get_str_array(kb, DLP_K_VALGRIND_ARG, &len);
+ } else {
+ str = bundle_get_val(kb, DLP_K_VALGRIND_ARG);
+ if(str) {
+ str_array = &str;
+ len = 1;
+ }
+ }
+ if(str_array == NULL) return 0;
+
+ for (i = 0; i < len; i++) {
+ if(str_array[i] == NULL) break;
+ /* valgrind log file option */
+ if (strncmp(str_array[i], OPT_VALGRIND_LOGFILE
+ , strlen(OPT_VALGRIND_LOGFILE)) == 0)
+ {
+ if(strncmp(str_array[i], OPT_VALGRIND_LOGFILE_FIXED
+ , strlen(str_array[i])))
+ {
+ _E("wrong valgrind option(%s). It should be %s"
+ , str_array[i]
+ , OPT_VALGRIND_LOGFILE_FIXED);
+ return 1;
+ }else{
+ poll_outputfile |= POLL_VALGRIND_LOGFILE;
+ if(remove(PATH_VALGRIND_LOGFILE)){
+ _D("cannot remove %s"
+ , PATH_VALGRIND_LOGFILE);
+ }
+ }
+ }
+ /* valgrind xml file option */
+ else if (strncmp(str_array[i], OPT_VALGRIND_XMLFILE
+ , strlen(OPT_VALGRIND_XMLFILE)) == 0)
+ {
+ if(strncmp(str_array[i], OPT_VALGRIND_XMLFILE_FIXED
+ , strlen(str_array[i])))
+ {
+ _E("wrong valgrind option(%s). It should be %s"
+ , str_array[i]
+ , OPT_VALGRIND_XMLFILE_FIXED);
+ return 1;
+ }else{
+ poll_outputfile |= POLL_VALGRIND_XMLFILE;
+ if(remove(PATH_VALGRIND_XMLFILE)){
+ _D("cannot remove %s"
+ , PATH_VALGRIND_XMLFILE);
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+int __prepare_fork(bundle *kb, char *appid)
+{
+ const char *str = NULL;
+ const char **str_array = NULL;
+ int len = 0;
+ int i;
+
+ poll_outputfile = 0;
+ if(bundle_get_type(kb, AUL_K_SDK) & BUNDLE_TYPE_ARRAY) {
+ str_array = bundle_get_str_array(kb, AUL_K_SDK, &len);
+ } else {
+ str = bundle_get_val(kb, AUL_K_SDK);
+ if(str) {
+ str_array = &str;
+ len = 1;
+ }
+ }
+ if(str_array == NULL) return 0;
+
+ for (i = 0; i < len; i++) {
+ if(str_array[i] == NULL) break;
+ /* gdbserver */
+ if (strncmp(str_array[i], SDK_DEBUG, strlen(str_array[i])) == 0)
+ {
+ if(apply_smack_rules("sdbd",appid,"w")) {
+ _E("unable to set sdbd rules");
+ return 1;
+ }
+
+ // FIXME: set gdbfolder to 755 also
+ if(dlp_chmod(PATH_GDBSERVER
+ , S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP
+ |S_IROTH|S_IXOTH
+ , 1))
+ {
+ _D("unable to set 755 to %s", PATH_GDBSERVER);
+ }
+ }
+ /* valgrind */
+ else if (strncmp(str_array[i], SDK_VALGRIND
+ , strlen(str_array[i])) == 0)
+ {
+ if (__prepare_valgrind_outputfile(kb)) return 1;
+ }
+ }
+ return 0;
+}
+
+/* chmod and chsmack to read file without root privilege */
+void __chmod_chsmack_toread(const char * path)
+{
+ /* chmod */
+ if(dlp_chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 0))
+ {
+ _E("unable to set 644 to %s", path);
+ }else{
+ _D("set 644 to %s", path);
+ }
+
+ /* chsmack */
+ if(smack_setlabel(path, "*", SMACK_LABEL_ACCESS))
+ {
+ _E("failed chsmack -a \"*\" %s", path);
+ }else{
+ _D("chsmack -a \"*\" %s", path);
+ }
+
+ return;
+}
+
+/* waiting for creating outputfile by child process */
+void __waiting_outputfile()
+{
+ int wait_count = 0;
+ while(poll_outputfile && wait_count<10) {
+ /* valgrind log file */
+ if( (poll_outputfile & POLL_VALGRIND_LOGFILE)
+ && (access(PATH_VALGRIND_LOGFILE,F_OK)==0) )
+ {
+ __chmod_chsmack_toread(PATH_VALGRIND_LOGFILE);
+ poll_outputfile &= ~POLL_VALGRIND_LOGFILE;
+ }
+
+ /* valgrind xml file */
+ if( (poll_outputfile & POLL_VALGRIND_XMLFILE)
+ && (access(PATH_VALGRIND_XMLFILE,F_OK)==0) )
+ {
+ __chmod_chsmack_toread(PATH_VALGRIND_XMLFILE);
+ poll_outputfile &= ~POLL_VALGRIND_XMLFILE;
+ }
+
+ if(poll_outputfile) {
+ _D("-- now wait for creating the file --");
+ usleep(50 * 1000); /* 50ms sleep*/
+ wait_count++;
+ }
+ }
+
+ if(wait_count==10) _E("faild to waiting");
+ return;
+}
+