int main(void) { fopen("", "r"); fopen64("", "r"); freopen("", "r", NULL); freopen64("", "r", NULL); open("", O_RDONLY); open64("", O_RDONLY); creat("", S_IRWXU); creat64("", S_IRWXU); unlink(""); rename("", ""); #ifdef AT_FUNCTIONS unlinkat(0, "", 0); renameat(0, "", 0, ""); openat(0, "", 0); openat64(0, "", 0); #endif return 0; }
TEST(STDIO_TEST, fopen64_freopen64) { FILE* fp = fopen64("/proc/version", "r"); ASSERT_TRUE(fp != nullptr); fp = freopen64("/proc/version", "re", fp); ASSERT_TRUE(fp != nullptr); fclose(fp); }
void log_rotate(void) { uint64_t date; FILE *orig_out, *orig_err; chr_t *log_file = MEMORYBUF(1024); if (magma.output.file && magma.output.path && (date = time_datestamp()) != log_date) { orig_out = stdout; orig_err = stderr; log_date = date; if (snprintf(log_file, 1024, "%s%smagmad.%lu.log", magma.output.path, (*(ns_length_get(magma.output.path) + magma.output.path) == '/') ? "" : "/", date) >= 1024) { log_critical("Log file path exceeded available buffer. { file = %s%smagmad.%lu.log }", magma.output.path, (*(ns_length_get(magma.output.path) + magma.output.path) == '/') ? "" : "/", date); return; } pthread_mutex_lock(&log_mutex); if (!(stdout = freopen64(log_file, "a", stdout))) { stdout = orig_out; log_critical("Unable to rotate the error log. { file = %s }", log_file); pthread_mutex_unlock(&log_mutex); return; } if (!(stderr = freopen64(log_file, "a", stderr))) { fclose(stdout); stdout = orig_out; stderr = orig_err; log_critical("Unable to rotate the error log. { file = %s }", log_file); pthread_mutex_unlock(&log_mutex); return; } pthread_mutex_unlock(&log_mutex); } return; }
bool_t log_start(void) { FILE *file_out, *file_err; chr_t *log_file = MEMORYBUF(1024); // File logging. if (magma.output.file && magma.output.path) { log_date = time_datestamp(); if (snprintf(log_file, 1024, "%s%smagmad.%lu.log", magma.output.path, (*(ns_length_get(magma.output.path) + magma.output.path) == '/') ? "" : "/", log_date) >= 1024) { log_critical("Log file path exceeded available buffer. { file = %s%smagmad.%lu.log }", magma.output.path, (*(ns_length_get(magma.output.path) + magma.output.path) == '/') ? "" : "/", log_date); return false; } if (folder_exists(NULLER(magma.output.path), false)) { log_critical("The path configured to hold the output log files does not exist. { path = %s }", magma.output.path); return false; } if (!(file_out = freopen64(log_file, "a+", stdout))) { log_critical("Unable to open the error log, sticking with standard out. { file = %s }", log_file); return false; } if (!(file_err = freopen64(log_file, "a+", stderr))) { log_critical("Unable to open the error log, sticking with standard error. { file = %s }", log_file); fclose(file_out); return false; } stdout = file_out; stderr = file_err; } fclose(stdin); return true; }
pid_t Activator::activate( const string& strExePath, const string& strPwdPath, const string &strRollLogPath, const vector<string>& vOptions, vector<string>& vEnvs ) { addActivatingRecord(); if(isActivatingLimited() == true) { LOG->error()<<"The server "<< strExePath <<" activating is limited! it will not auto start until after "+TC_Common::tostr(_iPunishInterval) +" seconds"<<endl; } if ( strExePath.empty() ) { throw runtime_error( "The server executable path is empty." ); } if(TC_File::isAbsolute(strExePath) == true && !TC_File::isFileExistEx(strExePath)) { throw runtime_error( "The server patch " + strExePath+" is not exist." ); } if(!TC_File::canExecutable(strExePath)) { TC_File::setExecutable(strExePath,true); } vector<string> vArgs; vArgs.push_back( strExePath); vArgs.insert( vArgs.end(), vOptions.begin(), vOptions.end() ); LOG->debug() << "activating server path " << strExePath << " " <<TC_Common::tostr(vArgs)<< endl; // // Activate and create. // Convert to standard argc/argv. // int argc = static_cast<int>( vArgs.size() ); char** argv = static_cast<char**>( malloc( ( argc + 1 ) * sizeof( char* ) ) ); int i = 0; for ( vector<string>::const_iterator p = vArgs.begin(); p != vArgs.end(); ++p, ++i ) { assert( i < argc ); argv[i] = strdup( p->c_str() ); } assert( i == argc ); argv[argc] = 0; // // Current directory // const char* pwdCStr = strPwdPath.c_str(); pid_t pid = fork(); if ( pid == -1 ) { LOG->debug() << strPwdPath << "|fork child process catch exception|errno=" << errno << endl; throw runtime_error( "fork child process catch exception" ); } if ( pid == 0 ) // Child process. { // // Close all file descriptors, except for standard input, // standard output, standard error // int maxFd = static_cast<int>( sysconf( _SC_OPEN_MAX ) ); for ( int fd = 3; fd < maxFd; ++fd ) { close( fd ); } //server stdcout 日志在滚动日志显示 if(_sRedirectPath != "") { if(( freopen64(_sRedirectPath.c_str(), "ab", stdout)) != NULL && ( freopen64(_sRedirectPath.c_str(), "ab", stderr)) != NULL) { cout <<argv[0]<<" redirect stdout and stderr to " << _sRedirectPath << endl; } else { cout << argv[0]<<" cannot redirect stdout and stderr to log file" << _sRedirectPath << "|errno=" <<strerror(errno) << endl; } } else { if(strRollLogPath != "" && ( freopen64(strRollLogPath.c_str(), "ab", stdout)) != NULL && ( freopen64(strRollLogPath.c_str(), "ab", stderr)) != NULL) { cout <<argv[0]<<" redirect stdout and stderr to " << strRollLogPath << endl; } else { cout << argv[0]<<" cannot redirect stdout and stderr to log file" << strRollLogPath << "|errno=" <<strerror(errno) << endl; } } for_each(vEnvs.begin(), vEnvs.end(), EnvironmentEval()); // // Change working directory. // if ( strlen( pwdCStr ) != 0 ) { if ( chdir( pwdCStr ) == -1 ) { cerr<<argv[0]<<" cannot change working directory to " << pwdCStr << "|errno=" << errno << endl; } } if ( execvp( argv[0], argv ) == -1 ) { cerr <<"cannot execute " << argv[0] << "|errno=" << strerror(errno) << endl; } exit(0); } else // Parent process. { for ( i = 0; argv[i]; i++ ) { free( argv[i] ); } free( argv ); } return pid; }