Skip to content

Commit

Permalink
Rplaced stderr writes function (Closes: #3158)
Browse files Browse the repository at this point in the history
Patch was created by Nicola Viganò. Thanks.

Signed-off-by: Nobuhiro Iwamatsu <[email protected]>
  • Loading branch information
iwamatsu committed Jun 24, 2012
1 parent 8630ce6 commit 3945a55
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 70 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ set(slim_srcs
panel.cpp
switchuser.cpp
util.cpp
log.cpp
png.c
jpeg.c
)
Expand Down
107 changes: 53 additions & 54 deletions app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ int conv(int num_msg, const struct pam_message **msg,
case PAM_TEXT_INFO:
// We simply write these to the log
// TODO: Maybe we should simply ignore them
cerr << APPNAME << ": " << msg[i]->msg << endl;
logStream << APPNAME << ": " << msg[i]->msg << endl;
break;
}
if (result!=PAM_SUCCESS) break;
Expand All @@ -110,7 +110,7 @@ int xioerror(Display *disp) {
}

void CatchSignal(int sig) {
cerr << APPNAME << ": unexpected signal " << sig << endl;
logStream << APPNAME << ": unexpected signal " << sig << endl;

if (LoginApp->isServerStarted())
LoginApp->StopServer();
Expand Down Expand Up @@ -151,7 +151,7 @@ App::App(int argc, char** argv)
testtheme = optarg;
testing = true;
if (testtheme == NULL) {
cerr << "The -p option requires an argument" << endl;
logStream << "The -p option requires an argument" << endl;
exit(ERR_EXIT);
}
break;
Expand All @@ -167,9 +167,9 @@ App::App(int argc, char** argv)
exit(OK_EXIT);
break;
case '?': // Illegal
cerr << endl;
logStream << endl;
case 'h': // Help
cerr << "usage: " << APPNAME << " [option ...]" << endl
logStream << "usage: " << APPNAME << " [option ...]" << endl
<< "options:" << endl
<< " -d: daemon mode" << endl
<< " -nodaemon: no-daemon mode" << endl
Expand All @@ -181,7 +181,7 @@ App::App(int argc, char** argv)
}
#ifndef XNEST_DEBUG
if (getuid() != 0 && !testing) {
cerr << APPNAME << ": only root can run this program" << endl;
logStream << APPNAME << ": only root can run this program" << endl;
exit(ERR_EXIT);
}
#endif /* XNEST_DEBUG */
Expand Down Expand Up @@ -230,7 +230,7 @@ void App::Run() {
pam.set_item(PAM::Authenticator::Requestor, "root");
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
exit(ERR_EXIT);
};
#endif
Expand All @@ -241,11 +241,11 @@ void App::Run() {
themefile = themedir + THEMESFILE;
if (!cfg->readConf(themefile)) {
if (themeName == "default") {
cerr << APPNAME << ": Failed to open default theme file "
logStream << APPNAME << ": Failed to open default theme file "
<< themefile << endl;
exit(ERR_EXIT);
} else {
cerr << APPNAME << ": Invalid theme in config: "
logStream << APPNAME << ": Invalid theme in config: "
<< themeName << endl;
themeName = "default";
}
Expand Down Expand Up @@ -276,7 +276,7 @@ void App::Run() {
// Daemonize
if (daemonmode) {
if (daemon(0, 0) == -1) {
cerr << APPNAME << ": " << strerror(errno) << endl;
logStream << APPNAME << ": " << strerror(errno) << endl;
exit(ERR_EXIT);
}
}
Expand All @@ -294,7 +294,7 @@ void App::Run() {

// Open display
if((Dpy = XOpenDisplay(DisplayName)) == 0) {
cerr << APPNAME << ": could not open display '"
logStream << APPNAME << ": could not open display '"
<< DisplayName << "'" << endl;
if (!testing) StopServer();
exit(ERR_EXIT);
Expand Down Expand Up @@ -425,11 +425,11 @@ bool App::AuthenticateUser(bool focuspass){
default:
break;
};
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
return false;
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
exit(ERR_EXIT);
};
return true;
Expand All @@ -441,7 +441,7 @@ bool App::AuthenticateUser(bool focuspass){
switch(LoginPanel->getAction()){
case Panel::Exit:
case Panel::Console:
cerr << APPNAME << ": Got a special command (" << LoginPanel->GetName() << ")" << endl;
logStream << APPNAME << ": Got a special command (" << LoginPanel->GetName() << ")" << endl;
return true; // <--- This is simply fake!
default:
break;
Expand Down Expand Up @@ -518,11 +518,11 @@ void App::Login() {
}
catch(PAM::Cred_Exception& e){
// Credentials couldn't be established
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
return;
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
exit(ERR_EXIT);
};
#else
Expand Down Expand Up @@ -560,7 +560,7 @@ void App::Login() {
pam.setenv("XAUTHORITY", xauthority.c_str());
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
exit(ERR_EXIT);
}
#endif
Expand All @@ -571,7 +571,7 @@ void App::Login() {
ck.open_session(DisplayName, pw->pw_uid);
}
catch(Ck::Exception &e) {
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
exit(ERR_EXIT);
}
#endif
Expand Down Expand Up @@ -669,7 +669,7 @@ void App::Login() {
ck.close_session();
}
catch(Ck::Exception &e) {
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
};
#endif

Expand All @@ -678,7 +678,7 @@ void App::Login() {
pam.close_session();
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
};
#endif

Expand Down Expand Up @@ -711,7 +711,7 @@ void App::Reboot() {
pam.end();
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
};
#endif

Expand All @@ -733,7 +733,7 @@ void App::Halt() {
pam.end();
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
};
#endif

Expand Down Expand Up @@ -777,7 +777,7 @@ void App::Exit() {
pam.end();
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
};
#endif

Expand Down Expand Up @@ -807,7 +807,7 @@ void App::RestartServer() {
pam.end();
}
catch(PAM::Exception& e){
cerr << APPNAME << ": " << e << endl;
logStream << APPNAME << ": " << e << endl;
};
#endif

Expand Down Expand Up @@ -860,9 +860,9 @@ int App::ServerTimeout(int timeout, char* text) {
break;
if(timeout) {
if(i == 0 && text != lasttext)
cerr << endl << APPNAME << ": waiting for " << text;
logStream << endl << APPNAME << ": waiting for " << text;
else
cerr << ".";
logStream << ".";
}
if(timeout)
sleep(1);
Expand All @@ -871,7 +871,7 @@ int App::ServerTimeout(int timeout, char* text) {
}

if(i > 0)
cerr << endl;
logStream << endl;
lasttext = text;

return (ServerPID != pidfound);
Expand All @@ -892,7 +892,7 @@ int App::WaitForServer() {
}
}

cerr << "Giving up." << endl;
logStream << "Giving up." << endl;

return 0;
}
Expand Down Expand Up @@ -956,7 +956,7 @@ int App::StartServer() {


execvp(server[0], server);
cerr << APPNAME << ": X server could not be started" << endl;
logStream << APPNAME << ": X server could not be started" << endl;
exit(ERR_EXIT);
break;

Expand All @@ -972,7 +972,7 @@ int App::StartServer() {

// Wait for server to start up
if(WaitForServer() == 0) {
cerr << APPNAME << ": unable to connect to X server" << endl;
logStream << APPNAME << ": unable to connect to X server" << endl;
StopServer();
ServerPID = -1;
exit(ERR_EXIT);
Expand All @@ -990,7 +990,7 @@ int App::StartServer() {

jmp_buf CloseEnv;
int IgnoreXIO(Display *d) {
cerr << APPNAME << ": connection to X server lost." << endl;
logStream << APPNAME << ": connection to X server lost." << endl;
longjmp(CloseEnv, 1);
}

Expand All @@ -1011,15 +1011,15 @@ void App::StopServer() {
// Send HUP to process group
errno = 0;
if((killpg(getpid(), SIGHUP) != 0) && (errno != ESRCH))
cerr << APPNAME << ": can't send HUP to process group " << getpid() << endl;
logStream << APPNAME << ": can't send HUP to process group " << getpid() << endl;

// Send TERM to server
if(ServerPID < 0)
return;
errno = 0;
if(killpg(ServerPID, SIGTERM) < 0) {
if(errno == EPERM) {
cerr << APPNAME << ": can't kill X server" << endl;
logStream << APPNAME << ": can't kill X server" << endl;
exit(ERR_EXIT);
}
if(errno == ESRCH)
Expand All @@ -1028,11 +1028,11 @@ void App::StopServer() {

// Wait for server to shut down
if(!ServerTimeout(10, (char *)"X server to shut down")) {
cerr << endl;
logStream << endl;
return;
}

cerr << endl << APPNAME << ": X server slow to shut down, sending KILL signal." << endl;
logStream << endl << APPNAME << ": X server slow to shut down, sending KILL signal." << endl;

// Send KILL to server
errno = 0;
Expand All @@ -1043,10 +1043,10 @@ void App::StopServer() {

// Wait for server to die
if(ServerTimeout(3, (char*)"server to die")) {
cerr << endl << APPNAME << ": can't kill server" << endl;
logStream << endl << APPNAME << ": can't kill server" << endl;
exit(ERR_EXIT);
}
cerr << endl;
logStream << endl;
}


Expand Down Expand Up @@ -1105,7 +1105,7 @@ void App::GetLock() {
// no lockfile present, create one
std::ofstream lockfile(cfg->getOption("lockfile").c_str(), ios_base::out);
if (!lockfile) {
cerr << APPNAME << ": Could not create lock file: " << cfg->getOption("lockfile").c_str() << std::endl;
logStream << APPNAME << ": Could not create lock file: " << cfg->getOption("lockfile").c_str() << std::endl;
exit(ERR_EXIT);
}
lockfile << getpid() << std::endl;
Expand All @@ -1119,13 +1119,13 @@ void App::GetLock() {
// see if process with this pid exists
int ret = kill(pid, 0);
if (ret == 0 || (ret == -1 && errno == EPERM) ) {
cerr << APPNAME << ": Another instance of the program is already running with PID " << pid << std::endl;
logStream << APPNAME << ": Another instance of the program is already running with PID " << pid << std::endl;
exit(0);
} else {
cerr << APPNAME << ": Stale lockfile found, removing it" << std::endl;
logStream << APPNAME << ": Stale lockfile found, removing it" << std::endl;
std::ofstream lockfile(cfg->getOption("lockfile").c_str(), ios_base::out);
if (!lockfile) {
cerr << APPNAME << ": Could not create new lock file: " << cfg->getOption("lockfile") << std::endl;
logStream << APPNAME << ": Could not create new lock file: " << cfg->getOption("lockfile") << std::endl;
exit(ERR_EXIT);
}
lockfile << getpid() << std::endl;
Expand All @@ -1147,24 +1147,23 @@ bool App::isServerStarted() {

// Redirect stdout and stderr to log file
void App::OpenLog() {
FILE *log = fopen (cfg->getOption("logfile").c_str(),"a");
if (!log) {
cerr << APPNAME << ": Could not accesss log file: " << cfg->getOption("logfile") << endl;

if ( !logStream.openLog( cfg->getOption("logfile").c_str() ) ) {
logStream << APPNAME << ": Could not accesss log file: " << cfg->getOption("logfile") << endl;
RemoveLock();
exit(ERR_EXIT);
}
fclose(log);
freopen (cfg->getOption("logfile").c_str(),"a",stdout);
setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
freopen (cfg->getOption("logfile").c_str(),"a",stderr);
setvbuf(stderr, NULL, _IONBF, BUFSIZ);
//
// I should set the buffers to imediate write, but I just flush on every
// << operation.
//
//
}


// Relases stdout/err
void App::CloseLog(){
fclose(stderr);
fclose(stdout);
// Simply closing the log
logStream.closeLog();
}

string App::findValidRandomTheme(const string& set)
Expand All @@ -1189,7 +1188,7 @@ string App::findValidRandomTheme(const string& set)
themefile = string(THEMESDIR) +"/" + name + THEMESFILE;
if (stat(themefile.c_str(), &buf) != 0) {
themes.erase(find(themes.begin(), themes.end(), name));
cerr << APPNAME << ": Invalid theme in config: "
logStream << APPNAME << ": Invalid theme in config: "
<< name << endl;
name = "";
}
Expand Down Expand Up @@ -1249,7 +1248,7 @@ char* App::StrConcat(const char* str1, const char* str2) {
void App::UpdatePid() {
std::ofstream lockfile(cfg->getOption("lockfile").c_str(), ios_base::out);
if (!lockfile) {
cerr << APPNAME << ": Could not update lock file: " << cfg->getOption("lockfile").c_str() << std::endl;
logStream << APPNAME << ": Could not update lock file: " << cfg->getOption("lockfile").c_str() << std::endl;
exit(ERR_EXIT);
}
lockfile << getpid() << std::endl;
Expand Down
Loading

0 comments on commit 3945a55

Please sign in to comment.