void IO_FileChannel__ChannelDesc_CloseAndRegister(IO_FileChannel__Channel ch) { int res = close(ch->fd); if (res >= 0) { ch->fd = -1; IO__ChannelDesc_Close((IO__Channel)ch); if (ch->tmpIndex >= 0) { char* fname = (char*)OS_Path__Encode(ch->origName); char* tname = (char*)OS_Path__Encode((Object__String)ch->tmpName); #ifdef __MINGW32__ if (MoveFileEx(tname, fname, MOVEFILE_REPLACE_EXISTING) == 0) res = GetLastError(); else res = 0; #else res = rename(tname, fname); #endif remove_tmp_file(ch); } } if (res < 0) { IO_StdChannels__IOError(ch->tmpIndex<0?(Object__String)ch->tmpName:ch->origName); } }
pid_t create_subpipe(char const *const *argv, int fd[2]) { int argc; int from_in_fd; /* pipe from bison to m4. */ pid_t pid; pid = getpid(); /* * Save original stdin and stdout * for later restauration. */ old_stdin = dup(STDIN_FILENO); if (old_stdin < 0) error(EXIT_FAILURE, 0, _("saving stdin failed")); old_stdout = dup(STDOUT_FILENO); if (old_stdout < 0) error(EXIT_FAILURE, 0, _("saving stdout failed")); /* * Save argv for later use. */ for (argc = 0; argv[argc]; argc++) ; argc++; arguments = xmalloc(argc * sizeof(arguments[0])); for (argc = 0; argv[argc]; argc++) { arguments[argc] = xmalloc((strlen(argv[argc]) + 1) * sizeof(arguments[0][0])); strcpy(arguments[argc], argv[argc]); } arguments[argc] = NULL; /* * All bison's output will be gathered in this temporary file * and will be redirected to m4's stdin. */ from_in_fd = open(tmp_file_name[0], O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR); if (from_in_fd < 0) error(EXIT_FAILURE, 0, _("opening of tmpfile failed")); if (dup2(from_in_fd, STDOUT_FILENO) < 0) { remove_tmp_file(from_in_fd, tmp_file_name[0]); error(EXIT_FAILURE, 0, _("redirecting bison's stdout to the temporary file failed")); } close(from_in_fd); fd[0] = STDOUT_FILENO; return pid; }
static void cleanup_tmp_files() { char* tname; IO_FileChannel__Channel ch; while (tmp_files_used != 0) { /* close and unlink all temporary files, ignoring any errors */ ch = tmp_files[tmp_files_used-1]; close(ch->fd); tname = (char*)OOC_METHOD(ch->tmpName, Object__String8Desc_CharsLatin1)(ch->tmpName); unlink(tname); remove_tmp_file(ch); } }
void IO_FileChannel__ChannelDesc_Close(IO_FileChannel__Channel ch) { int res = close(ch->fd); if (res >= 0) { ch->fd = -1; IO__ChannelDesc_Close((IO__Channel)ch); if (ch->tmpIndex >= 0) { char* tname = (char*)OOC_METHOD(ch->tmpName,Object__String8Desc_CharsLatin1)(ch->tmpName); res = unlink(tname); remove_tmp_file(ch); } } if (res < 0) { IO_StdChannels__IOError(ch->tmpIndex<0?(Object__String)ch->tmpName:ch->origName); } }
static int hotplug_remove_device(void) { int ret; char serv[256]; char *busid; logmsg("bind-driver.c:: removing device" ); busid = get_busid_from_env(); if( busid == NULL ) { logmsg("error retrieving busid"); return -1; } logmsg("busid = '%s'", busid); ret = get_tmp_file(serv, busid); if(ret) { logerr("could not get server from /tmp file" ); return -1; } logmsg("export server is '%s'", serv); /* XXX * we don't do anything here. The server will know by itself, that the * device is no longer available, as the connection will break down, * and it will (afai guess) receive the 'UNAVAILABLE' signal. If we * try to use unexport_from at this point, the driver might already be * unavailable, which will result in a segmentation fault! */ /*ret = unexport_from(serv, busid); if(ret) { logerr("exporting of device failed"); return -1; }*/ remove_tmp_file(serv, busid); logmsg("device removed"); return 0; }
void end_of_output_subpipe(pid_t pid, int fd[2]) { char *program; int from_out_fd = open(tmp_file_name[0], O_RDONLY, S_IRUSR); /* pipe from bison to m4. */ int to_in_fd = open(tmp_file_name[1], O_WRONLY | O_CREAT | O_TRUNC, S_IWUSR); /* pipe from m4 to bison. */ int status; void (*previous_handler)(int); program = strrchr(arguments[0], '/'); if (program) program++; else program = arguments[0]; /* * Redirect bison's output to m4's stdin. */ if (from_out_fd < 0) error(EXIT_FAILURE, 0, _("opening of tmpfile failed")); if (dup2(from_out_fd, STDIN_FILENO) < 0) { remove_tmp_file(from_out_fd, tmp_file_name[0]); error(EXIT_FAILURE, 0, _("redirecting m4's stdin from the temporary file failed")); } close(from_out_fd); /* * All m4's output will be gathered in this temporary file * and will be redirected to bison's stdin. */ if (to_in_fd < 0) { remove_tmp_file(STDIN_FILENO, tmp_file_name[0]); error(EXIT_FAILURE, 0, _("opening of a temporary file failed")); } if (dup2(to_in_fd, STDOUT_FILENO) < 0) { remove_tmp_file(STDIN_FILENO, tmp_file_name[0]); remove_tmp_file(to_in_fd, tmp_file_name[1]); error(EXIT_FAILURE, 0, _("redirecting m4's stdout to a temporary file failed")); } close(to_in_fd); /* * Run m4. */ child_interrupted = 0; errno = 0; previous_handler = signal(SIGINT, signal_catcher); status = spawnvp(P_WAIT, program, arguments); signal(SIGINT, previous_handler); if (child_interrupted) { remove_tmp_file(STDIN_FILENO, tmp_file_name[0]); remove_tmp_file(STDOUT_FILENO, tmp_file_name[1]); error(EXIT_FAILURE, 0, _("subsidiary program `%s' interrupted"), program); } if (status) { remove_tmp_file(STDIN_FILENO, tmp_file_name[0]); remove_tmp_file(STDOUT_FILENO, tmp_file_name[1]); error(EXIT_FAILURE, 0, _(errno == ENOENT ? "subsidiary program `%s' not found" : status < 1 ? "subsidiary program `%s' failed" : "subsidiary program `%s' failed (status=%i, errno=%i)"), program, status, errno); } /* * Redirect m4's output to bison's stdin. */ if (dup2(old_stdout, STDOUT_FILENO) < 0) error(EXIT_FAILURE, 0, "restore of bison's stdout failed"); close(old_stdout); to_in_fd = open(tmp_file_name[1], O_RDONLY, S_IRUSR); /* pipe from m4 to bison. */ if (to_in_fd < 0) { remove_tmp_file(STDIN_FILENO, tmp_file_name[0]); error(EXIT_FAILURE, 0, _("opening of tmpfile failed")); } if (dup2(to_in_fd, STDIN_FILENO) < 0) { remove_tmp_file(STDIN_FILENO, tmp_file_name[0]); remove_tmp_file(to_in_fd, tmp_file_name[1]); error(EXIT_FAILURE, -1, "dup2"); error(EXIT_FAILURE, 0, _("redirecting bison's stdin from the temporary file failed")); } close(to_in_fd); fd[1] = STDIN_FILENO; }
static int child(arg_data *args, home_data *data, uid_t uid, gid_t gid) { int ret = 0; /* check the pid file */ ret = check_pid(args); if (args->vers != true && args->chck != true) { if (ret == 122) return ret; if (ret < 0) return ret; } #ifdef OS_LINUX /* setuid()/setgid() only apply the current thread so we must do it now */ if (linuxset_user_group(args->user, uid, gid) != 0) return 4; #endif /* Initialize the Java VM */ if (java_init(args, data) != true) { log_debug("java_init failed"); return 1; } else log_debug("java_init done"); /* Check wether we need to dump the VM version */ if (args->vers == true) { log_error("jsvc (Apache Commons Daemon) " JSVC_VERSION_STRING); log_error("Copyright (c) 1999-2011 Apache Software Foundation."); if (java_version() != true) { return -1; } else return 0; } /* Check wether we need to dump the VM version */ else if (args->vershow == true) { if (java_version() != true) { return 7; } } /* Do we have to do a "check-only" initialization? */ if (args->chck == true) { if (java_check(args) != true) return 2; printf("Service \"%s\" checked successfully\n", args->clas); return 0; } /* Load the service */ if (java_load(args) != true) { log_debug("java_load failed"); return 3; } else log_debug("java_load done"); /* Downgrade user */ #ifdef OS_LINUX if (args->user && set_caps(0) != 0) { log_debug("set_caps (0) failed"); return 4; } #else if (set_user_group(args->user, uid, gid) != 0) return 4; #endif /* Start the service */ umask(envmask); if (java_start() != true) { log_debug("java_start failed"); return 5; } else log_debug("java_start done"); /* Install signal handlers */ handler_hup = signal_set(SIGHUP, handler); handler_usr1 = signal_set(SIGUSR1, handler); handler_usr2 = signal_set(SIGUSR2, handler); handler_trm = signal_set(SIGTERM, handler); handler_int = signal_set(SIGINT, handler); controlled = getpid(); log_debug("Waiting for a signal to be delivered"); create_tmp_file(args); while (!stopping) { #if defined(OSD_POSIX) java_sleep(60); /* pause(); */ #else /* pause() is not threadsafe */ sleep(60); #endif if(doreopen) { doreopen = false; set_output(args->outfile, args->errfile, args->redirectstdin, args->procname); } if(dosignal) { dosignal = false; java_signal(); } } remove_tmp_file(args); log_debug("Shutdown or reload requested: exiting"); /* Stop the service */ if (java_stop() != true) return 6; if (doreload == true) ret = 123; else ret = 0; /* Destroy the service */ java_destroy(); /* Destroy the Java VM */ if (JVM_destroy(ret) != true) return 7; return ret; }