Changeset 577dd9b


Ignore:
Timestamp:
Jul 16, 2013 6:21:01 AM (9 months ago)
Author:
Ken Moore <ken@…>
Branches:
master, 9.2-release, releng/10.0, releng/10.0.1
Children:
5b07f4a
Parents:
1d21143
git-author:
Ken Moore <ken@…> (07/16/13 06:18:38)
git-committer:
Ken Moore <ken@…> (07/16/13 06:21:01)
Message:

Setup the PCDM main session process to run fork and setsid to start the process.
Also add the beginning of the changes to the xprocess class to change over to running C process commands rather than using a QProcess for starting the desktop session.
You just need to change the USECLIBS flag at the top of main.cpp to switch which one to use.

Location:
src-qt4/PCDM/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src-qt4/PCDM/src/main.cpp

    rd4bc04b r577dd9b  
    1414#include <QDebug> 
    1515#include <QX11Info> 
    16 //#include <X11/Xlib.h> 
     16 
     17#include <sys/types.h> 
     18#include <sys/wait.h> 
     19#include <unistd.h> 
    1720 
    1821#include "pcdm-gui.h" 
     
    2831//#define prefix "/usr/local/" 
    2932//#endif 
     33 
     34bool USECLIBS=false; 
    3035 
    3136int runSingleSession(int argc, char *argv[]){ 
     
    7479         goodAL=FALSE;    
    7580    }else{ 
    76         desktop.loginToXSession(user,pwd, Backend::getUserHomeDir(user), dsk); 
     81        desktop.loginToXSession(user,pwd, dsk); 
    7782        splash.close(); 
    7883        if(desktop.isRunning()){ 
     
    121126 
    122127    //Setup the signals/slots to startup the desktop session 
    123     QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString,QString)), &desktop,SLOT(loginToXSession(QString,QString,QString,QString)) ); 
     128    if(USECLIBS){ QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString)), &desktop,SLOT(setupDesktop(QString,QString,QString))); } 
     129    else{ QObject::connect( &w,SIGNAL(xLoginAttempt(QString,QString,QString)), &desktop,SLOT(loginToXSession(QString,QString,QString)) ); } 
    124130    //Setup the signals/slots for return information for the GUI 
    125131    QObject::connect( &desktop, SIGNAL(InvalidLogin()), &w, SLOT(slotLoginFailure()) ); 
    126132    QObject::connect( &desktop, SIGNAL(started()), &w, SLOT(slotLoginSuccess()) ); 
     133    QObject::connect( &desktop, SIGNAL(ValidLogin()), &w, SLOT(slotLoginSuccess()) ); 
    127134     
    128135    //qDebug() << "Showing GUI:" << QString::number(clock.elapsed())+" ms"; 
     
    132139  int retcode = 0; 
    133140  //Wait for the desktop session to finish before exiting 
    134   desktop.waitForSessionClosed(); 
     141  if(USECLIBS){ desktop.startDesktop(); } 
     142  else{ desktop.waitForSessionClosed(); } 
    135143  splash.show(); //show the splash screen again 
    136144  splash.showMessage(QObject::tr("System Shutting Down"), Qt::AlignHCenter | Qt::AlignBottom, Qt::white); 
     
    151159  delete &a; 
    152160  delete &splash; 
    153   //XSetCloseDownMode(QX11Info::display(), DestroyAll); 
    154   //XCLoseDisplay(QX11Info::display()); 
    155161   
    156162   
     
    161167{ 
    162168 bool neverquit = TRUE; 
    163  bool runonce = TRUE; //looping is currently not working yet - needs to restart X each time? 
     169 bool runonce = FALSE; 
    164170 if(argc==2){ if( QString(argv[1]) == "--once"){ runonce = TRUE; } } 
    165171   
     
    167173  if(runonce){ neverquit = FALSE; } 
    168174  qDebug() << " -- PCDM Session Starting..."; 
    169   int retCode = runSingleSession(argc,argv); 
    170   if(retCode != 0){ neverquit=FALSE; } 
     175  int sid = -1; 
     176  int pid = fork(); 
     177  if(pid < 0){ 
     178    qDebug() << "Error: Could not fork the PCDM session"; 
     179    return -1; 
     180  }else if( pid ==0 ){ 
     181    //New Child Process 
     182    sid = setsid(); //start a session 
     183    qDebug() << "-- Session ID:" << sid; 
     184    int retCode = runSingleSession(argc,argv); 
     185    qDebug() << "-- PCDM Session Ended --"; 
     186    //check for special exit code 
     187    if(retCode != 0){ neverquit=FALSE; } 
     188    //Now kill the shild process (whole session) 
     189    qDebug() << "Exiting child process"; 
     190    exit(3); 
     191  }else{  
     192    //Parent (calling) process 
     193    int status; 
     194    sleep(2); 
     195    waitpid(sid,&status,0); //wait for the child (session) to finish 
     196  } 
     197  qDebug() << "-- PCDM Session Ended --"; 
     198  if(QFile::exists("/var/run/nologin")){ neverquit = FALSE; }  
    171199 } 
    172200 return 0; 
  • src-qt4/PCDM/src/pcdm-backend.cpp

    r90dc161 r577dd9b  
    7676QString Backend::getUsernameFromDisplayname(QString dspname){ 
    7777  int i = displaynameList.indexOf(dspname); 
    78   return usernameList[i]; 
     78  if(i == -1){ i = usernameList.indexOf(dspname); } 
     79   
     80  if(i == -1){ return ""; } 
     81  else{ return usernameList[i]; } 
    7982} 
    8083 
  • src-qt4/PCDM/src/pcdm-gui.cpp

    r90dc161 r577dd9b  
    1515#include "fancySwitcher.h" 
    1616 
    17 bool DEBUG_MODE=FALSE; 
     17bool DEBUG_MODE=TRUE; 
    1818QString VIRTUALKBDBIN="/usr/local/bin/xvkbd -compact"; 
    1919 
     
    257257    desktop = deSwitcher->currentItem(); 
    258258  } 
    259   QString homedir = Backend::getUserHomeDir(username); 
    260259  //Disable user input while confirming login 
    261260  loginW->setEnabled(FALSE); 
     
    263262  toolbar->setEnabled(FALSE); 
    264263  //Try to login 
    265   emit xLoginAttempt(username, password, homedir, desktop); 
     264  emit xLoginAttempt(username, password, desktop); 
    266265  //Return signals are connected to the slotLogin[Success/Failure] functions 
    267266   
  • src-qt4/PCDM/src/pcdm-gui.h

    r90dc161 r577dd9b  
    8585 
    8686signals: 
    87     void xLoginAttempt(QString, QString, QString, QString); 
     87    void xLoginAttempt(QString, QString, QString); 
    8888 
    8989}; 
  • src-qt4/PCDM/src/pcdm-xprocess.cpp

    r90dc161 r577dd9b  
    3030} 
    3131 
    32 void XProcess::loginToXSession(QString username, QString password, QString homedir, QString desktop){ 
     32void XProcess::loginToXSession(QString username, QString password, QString desktop){ 
    3333  //Setup the variables 
    3434  xuser = username; 
    3535  xpwd = password; 
    36   xhome = homedir; 
     36  xhome = Backend::getUserHomeDir(xuser); 
    3737  xcmd = Backend::getDesktopBinary(desktop); 
    3838  xde = desktop; 
     
    4949  // CAUTION!!  
    5050  // This function will pause the calling program to wait for the session to end! 
    51   if( isRunning() ){ 
    52     this->waitForFinished(-1); 
    53   } 
     51  if( isRunning() ){ this->waitForFinished(-1); } 
     52 
    5453} 
    5554 
     
    7574  Backend::saveLoginInfo(Backend::getDisplayNameFromUsername(xuser),xde); 
    7675   
     76 QString cmd; 
    7777  // Configure the DE startup command 
    78   QString cmd = "su "+xuser+" -c \""; //switch user command to start process properly 
     78  cmd = "su "+xuser+" -c \""; //switch user command to start QProcess properly 
    7979  //  - Setup to run the user's <home-dir>/.xprofile startup script 
    8080  if(QFile::exists(xhome+"/.xprofile")){ 
     
    8383  //  - Add the DE startup command to the end 
    8484  cmd.append("dbus-launch --exit-with-session "+xcmd); 
    85   //  - Finish up the command formatting 
    86   cmd.append("\""); 
    87    
    88   //Backend::log("Startup command: "+cmd); 
     85  //cmd.append("; kill -l KILL"); //to clean up the session afterwards 
    8986  // Get the current locale code 
    9087  QLocale mylocale; 
    9188  QString langCode = mylocale.name(); 
     89   
     90  //  - Finish up the command formatting 
     91  cmd.append("\""); 
     92   
     93  //Backend::log("Startup command: "+cmd); 
    9294  // Setup the process environment 
    9395  QProcessEnvironment environ = QProcessEnvironment::systemEnvironment(); //current environment 
     
    112114  this->start(cmd); 
    113115  return TRUE; 
     116  
    114117} 
    115118 
    116119void XProcess::slotCleanup(int exitCode, QProcess::ExitStatus status){ 
    117120  pam_shutdown(); //make sure that PAM shuts down properly       
     121} 
     122 
     123//Start the desktop in the current process with C functions 
     124void XProcess::startDesktop(){ 
     125  //Check for PAM username/password validity 
     126  if( !pam_checkPW() ){ qDebug() << "Invalid username/password"; pam_shutdown(); return; } 
     127  //Startup the PAM session 
     128  if( !pam_startSession() ){ qDebug() << "Unable to open PAM session"; pam_shutdown(); return; } 
     129  pam_session_open = TRUE; //flag that pam has an open session 
     130   
     131  //Save the current user/desktop as the last login 
     132  Backend::saveLoginInfo(Backend::getDisplayNameFromUsername(xuser),xde); 
     133   
     134  QString cmd; 
     135  // Configure the DE startup command 
     136  cmd = "su "+xuser+" -c \""; //switch user command to start QProcess properly 
     137  //  - Setup to run the user's <home-dir>/.xprofile startup script 
     138  if(QFile::exists(xhome+"/.xprofile")){ 
     139    cmd.append("(/bin/sh "+xhome+"/.xprofile) &; ");  //make sure to start it in parallel 
     140  } 
     141  //  - Add the DE startup command to the end 
     142  cmd.append("dbus-launch --exit-with-session "+xcmd); 
     143  //cmd.append("; kill -l KILL"); //to clean up the session afterwards 
     144  // Get the current locale code 
     145  QLocale mylocale; 
     146  QString langCode = mylocale.name(); 
     147   
     148  //Alternate way of starting a process using c library functions 
     149  
     150     //setup the environment variables 
     151     setenv("LOGNAME",xuser.toUtf8(),1); 
     152     setenv("USERNAME",xuser.toUtf8(),1); 
     153     QString pth = QString(getenv("PATH"))+":"+xhome+"/bin"; 
     154     setenv("PATH",pth.toUtf8(),1); 
     155     if(langCode.toLower()=="c"){} 
     156     else if(QString(getenv("MM_CHARSET")).isEmpty() ){ langCode.append("."+QString(getenv("MM_CHARSET"))); } 
     157     else{ langCode.append(".UTF-8"); } 
     158     setenv("LANG",langCode.toUtf8(),1); 
     159     setenv("MAIL",QString("/var/mail/"+xuser).toUtf8(),1); 
     160     setenv("GROUP",xuser.toUtf8(),1); 
     161     setenv("SHLVL","0",1); 
     162     chdir(xhome.toUtf8()); //move to home dir 
     163      
     164     //Now start the process 
     165     qDebug() << "Start the desktop"; 
     166     system(cmd.toUtf8()); 
     167} 
     168   
     169void XProcess::setupDesktop(QString user, QString pwd, QString desktop){ 
     170  //Setup internal variables 
     171  xuser = Backend::getUsernameFromDisplayname(user); 
     172  xpwd = pwd; 
     173  xhome = Backend::getUserHomeDir(xuser); 
     174  xcmd = Backend::getDesktopBinary(desktop); 
     175  xde = desktop; 
     176  //Also check password  validity 
     177  bool ok = pam_checkPW(); 
     178  pam_shutdown(); 
     179  if(ok){ emit ValidLogin(); } 
     180  else{ emit InvalidLogin(); } 
     181} 
     182   
     183   
     184 
     185//Stand-alone function to check a username/password combination for validity 
     186void XProcess::checkPW(QString user, QString pwd){ 
     187  xuser = Backend::getUsernameFromDisplayname(user);  
     188  xpwd = pwd; 
     189  bool ok = pam_checkPW(); 
     190  pam_shutdown(); 
     191  xuser.clear(); 
     192  xpwd.clear(); 
     193  if(ok){ emit ValidLogin(); } 
     194  else{ emit InvalidLogin(); } 
    118195} 
    119196 
  • src-qt4/PCDM/src/pcdm-xprocess.h

    r90dc161 r577dd9b  
    3232        bool isRunning(); 
    3333        void waitForSessionClosed(); 
     34         
     35        void startDesktop();  
     36         
    3437   
    3538  public slots: 
    36         void loginToXSession(QString, QString, QString, QString); //user, pwd, homedir, desktop 
    37          
     39        void loginToXSession(QString, QString, QString); //user, pwd, desktop 
     40        void checkPW(QString,QString); //user, pwd 
     41        void setupDesktop(QString,QString,QString); //user, pwd, desktop 
     42         
    3843  private: 
     44        int fstatus; //fork status 
     45        pid_t fpid;  //fork pid 
    3946        QString xuser, xcmd, xhome, xpwd, xde; 
    4047        bool startXSession(); 
     
    5360 
    5461  signals: 
     62        void ValidLogin(); 
    5563        void InvalidLogin(); 
    5664 
Note: See TracChangeset for help on using the changeset viewer.