source: src-qt4/PCDM/src/main.cpp @ 5e38281

releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1releng/10.1.1releng/10.1.2
Last change on this file since 5e38281 was 5e38281, checked in by Ken Moore <ken@…>, 15 months ago

Turn on a bit more log information for PCDM. This will help people find the logs as necessary.

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/* PCDM Login Manager:
2*  Written by Ken Moore (ken@pcbsd.org) 2012/2013
3*  Copyright(c) 2013 by the PC-BSD Project
4*  Available under the 3-clause BSD license
5*/
6
7#include <QtGui/QApplication>
8#include <QTranslator>
9#include <QLocale>
10#include <QDesktopWidget>
11#include <QFile>
12#include <QSplashScreen>
13#include <QTime>
14#include <QDebug>
15#include <QX11Info>
16
17#include <sys/types.h>
18#include <sys/wait.h>
19#include <unistd.h>
20
21#include "pcdm-gui.h"
22#include "pcdm-backend.h"
23#include "pcdm-config.h"
24#include "pcdm-xprocess.h"
25
26#define TMPLANGFILE QString("/tmp/.PCDMLang")
27#define TMPAUTOLOGINFILE QString("/tmp/.PCDMAutoLogin")
28#define TMPAUTHFILE QString("/tmp/.PCDMAuth")
29#define TMPSTOPFILE QString("/tmp/.PCDMstop")
30//Make sure that prefix is set
31//#ifndef prefix
32//#define prefix "/usr/local/"
33//#endif
34
35//bool USECLIBS=false;
36
37int runSingleSession(int argc, char *argv[]){
38  //QTime clock;
39  //clock.start();
40  Backend::checkLocalDirs();  // Create and fill "/usr/local/share/PCDM" if needed
41  Backend::openLogFile("/var/log/PCDM.log"); 
42  qDebug() << "PCDM Log File: /var/log/PCDM.log";
43  //qDebug() << "Backend Checks Finished:" << QString::number(clock.elapsed())+" ms";
44  //Setup the initial system environment (locale, keyboard)
45  QString lang, kmodel, klayout, kvariant;
46  Backend::readDefaultSysEnvironment(lang,kmodel,klayout,kvariant);
47  setenv("LANG", lang.toUtf8(), 0);
48  Backend::changeKbMap(kmodel,klayout,kvariant);
49  //Check for the flag to try and auto-login
50  bool ALtriggered = FALSE;
51  if(QFile::exists(TMPAUTOLOGINFILE)){ ALtriggered=TRUE; QFile::remove(TMPAUTOLOGINFILE); }
52 
53  QString changeLang; 
54  // Load the configuration file
55  QString confFile = "/usr/local/etc/pcdm.conf";
56  if(!QFile::exists(confFile)){ 
57    qDebug() << "PCDM: Configuration file missing:"<<confFile<<"\n  - Using default configuration";
58    QFile::copy(":samples/pcdm.conf", confFile);
59  }
60 
61  Config::loadConfigFile(confFile);
62  //qDebug() << "Config File Loaded:" << QString::number(clock.elapsed())+" ms";
63  // Startup the main application
64  QApplication a(argc,argv); 
65  int retCode = 0; //used for UI/application return
66  // Show our splash screen, so the user doesn't freak that that it takes a few seconds to show up
67  QSplashScreen splash;
68  if(!Config::splashscreen().isEmpty()){
69    splash.setPixmap( QPixmap(Config::splashscreen()) ); //load the splashscreen file
70  }
71  splash.show();
72  QCoreApplication::processEvents(); //Process the splash screen event immediately
73  //qDebug() << "SplashScreen Started:" << QString::number(clock.elapsed())+" ms";
74  //Initialize the xprocess
75  XProcess desktop;
76 
77  //*** STARTUP THE PROGRAM ***
78  bool goodAL = FALSE; //Flag for whether the autologin was successful
79  // Start the autologin procedure if applicable
80  if( ALtriggered && Config::useAutoLogin() ){
81    //Setup the Auto Login
82    QString user = Backend::getALUsername();
83    QString pwd = Backend::getALPassword();
84    QString dsk = Backend::getLastDE(user);
85    if( user.isEmpty() || dsk.isEmpty() ){
86         goodAL=FALSE;   
87    }else{
88        desktop.loginToXSession(user,pwd, dsk,lang);
89        splash.close();
90        if(desktop.isRunning()){
91          goodAL=TRUE; //flag this as a good login to skip the GUI
92        }
93    }
94  }
95  //qDebug() << "AutoLogin Finished:" << QString::number(clock.elapsed())+" ms";
96  if(!goodAL){
97    // ------START THE PCDM GUI-------
98   
99    // Check what directory our app is in
100    QString appDir = "/usr/local/share/PCDM";
101    // Load the translator
102    QTranslator translator;
103    QString langCode = lang;
104    //Check for a language change detected
105    if ( ! changeLang.isEmpty() )       
106       langCode = changeLang;
107    //Load the proper locale for the translator
108    if ( QFile::exists(appDir + "/i18n/PCDM_" + langCode + ".qm" ) ) {
109      translator.load( QString("PCDM_") + langCode, appDir + "/i18n/" );
110      a.installTranslator(&translator);
111      Backend::log("Loaded Translation:" + appDir + "/i18n/PCDM_" + langCode + ".qm");
112    } else {
113      Backend::log("Could not find: " + appDir + "/i18n/PCDM_" + langCode + ".qm");
114      langCode = "";
115    }
116    //qDebug() << "Translation Finished:" << QString::number(clock.elapsed())+" ms";
117
118    Backend::log("Starting up PCDM interface");
119    PCDMgui w;
120    //qDebug() << "Main GUI Created:" << QString::number(clock.elapsed())+" ms";
121    splash.finish(&w); //close the splash when the GUI starts up
122
123    // Set full-screen dimensions
124    QRect dimensions = QApplication::desktop()->screenGeometry();
125    int wid = dimensions.width();     // returns desktop width
126    int hig = dimensions.height();    // returns desktop height
127    w.setGeometry(0, 0, wid, hig);
128
129    //Set the proper size on the Application
130    w.setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnBottomHint);
131    w.setWindowState(Qt::WindowMaximized); //Qt::WindowFullScreen);
132
133    //Setup the signals/slots to startup the desktop session
134    //if(USECLIBS){ QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString)), &desktop,SLOT(setupDesktop(QString,QString,QString))); }
135    //else{
136            QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString,QString)), &desktop,SLOT(loginToXSession(QString,QString,QString,QString)) ); 
137    //}
138    //Setup the signals/slots for return information for the GUI
139    QObject::connect( &desktop, SIGNAL(InvalidLogin()), &w, SLOT(slotLoginFailure()) );
140    QObject::connect( &desktop, SIGNAL(started()), &w, SLOT(slotLoginSuccess()) );
141    QObject::connect( &desktop, SIGNAL(ValidLogin()), &w, SLOT(slotLoginSuccess()) );
142   
143    //qDebug() << "Showing GUI:" << QString::number(clock.elapsed())+" ms";
144    w.show();
145    retCode = a.exec();
146  }  // end of PCDM GUI running
147  //Wait for the desktop session to finish before exiting
148  //if(USECLIBS){ desktop.startDesktop(); }
149  //else{
150    desktop.waitForSessionClosed(); 
151  //}
152  splash.show(); //show the splash screen again
153  //Now wait a couple seconds for things to settle
154  QTime wTime = QTime::currentTime().addSecs(2);
155  while( QTime::currentTime() < wTime ){
156    QCoreApplication::processEvents(QEventLoop::AllEvents,100);
157  }
158  //check for shutdown process
159  if( QFile::exists(TMPSTOPFILE) || QFile::exists("/var/run/nologin") || retCode > 0){
160    splash.showMessage(QObject::tr("System Shutting Down"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white);
161    QCoreApplication::processEvents();
162    //Pause for a few seconds to prevent starting a new session during a shutdown
163    wTime = QTime::currentTime().addSecs(30);
164    while( QTime::currentTime() < wTime ){ 
165      //Keep processing events during the wait (for splashscreen)
166      QCoreApplication::processEvents(QEventLoop::AllEvents, 100); 
167    }
168    //set the return code for a shutdown
169    retCode = -1; //make sure it does not start a new session
170  }
171 
172  //Clean up Code
173  delete &desktop;
174  delete &a;
175  delete &splash;
176 
177 
178  return retCode;
179}
180
181int main(int argc, char *argv[])
182{
183 bool neverquit = TRUE;
184 bool runonce = FALSE;
185 if(argc==2){ if( QString(argv[1]) == "-once"){ runonce = TRUE; } }
186 
187 while(neverquit){
188  if(runonce){ neverquit = FALSE; }
189  qDebug() << " -- PCDM Session Starting...";
190  int sid = -1;
191  int pid = fork();
192  if(pid < 0){
193    qDebug() << "Error: Could not fork the PCDM session";
194    return -1;
195  }else if( pid ==0 ){
196    //New Child Process
197    sid = setsid(); //start a session
198    qDebug() << "-- Session ID:" << sid;
199    int retCode = runSingleSession(argc,argv);
200    qDebug() << "-- PCDM Session Ended --";
201    //check for special exit code
202    if(retCode != 0){ neverquit=FALSE; }
203    //Now kill the shild process (whole session)
204    qDebug() << "Exiting child process";
205    exit(3);
206  }else{ 
207    //Parent (calling) process
208    int status;
209    sleep(2);
210    waitpid(sid,&status,0); //wait for the child (session) to finish
211    //NOTE: the parent will eventually become a login-watcher daemon process that
212    //   can spawn multiple child sessions on different TTY displays
213  }
214  qDebug() << "-- PCDM Session Ended --";
215  if(QFile::exists("/var/run/nologin") || QFile::exists(TMPSTOPFILE) ){ neverquit = FALSE; } 
216 }
217 return 0;
218}
Note: See TracBrowser for help on using the repository browser.