myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [3072] Use `getpwnam' when it is available instead of


From: Giuseppe Scrivano
Subject: [myserver-commit] [3072] Use `getpwnam' when it is available instead of parsing the `/etc/ passwd' file.
Date: Sun, 03 May 2009 14:06:50 +0000

Revision: 3072
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3072
Author:   gscrivano
Date:     2009-05-03 14:06:50 +0000 (Sun, 03 May 2009)
Log Message:
-----------
Use `getpwnam' when it is available instead of parsing the `/etc/passwd' file.

Modified Paths:
--------------
    trunk/myserver/configure.in
    trunk/myserver/include/base/home_dir/home_dir.h
    trunk/myserver/src/base/home_dir/home_dir.cpp
    trunk/myserver/tests/test_homedir.cpp
    trunk/myserver/tests/test_socket.cpp
    trunk/myserver/tests/test_ssl_socket.cpp

Modified: trunk/myserver/configure.in
===================================================================
--- trunk/myserver/configure.in 2009-05-03 14:06:45 UTC (rev 3071)
+++ trunk/myserver/configure.in 2009-05-03 14:06:50 UTC (rev 3072)
@@ -96,7 +96,9 @@
 
 AC_CHECK_FUNCS(snprintf, AC_DEFINE(SNPRINTF, 1, [Define if the snprintf 
function is present]) )
 
+AC_CHECK_FUNCS(getpwnam, AC_DEFINE(GETPWNAM, 1, [Define if the getpwnam 
function is present]) )
 
+
 case "${host}" in
     *-*-mingw32*)
      LDFLAGS="$LDFLAGS -lwsock32 -lgdi32 -lole32  -luuid -luserenv"

Modified: trunk/myserver/include/base/home_dir/home_dir.h
===================================================================
--- trunk/myserver/include/base/home_dir/home_dir.h     2009-05-03 14:06:45 UTC 
(rev 3071)
+++ trunk/myserver/include/base/home_dir/home_dir.h     2009-05-03 14:06:50 UTC 
(rev 3072)
@@ -1,7 +1,7 @@
 /* -*- mode: c++ -*- */
 /*
 MyServer
-Copyright (C) 2006, 2008 Free Software Foundation, Inc.
+Copyright (C) 2006, 2008, 2009 Free Software Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -36,18 +36,18 @@
 
        void clear();
        int load();
-       const string *getHomeDir(string& userName);
-       void getHomeDir(string& userName, string& out);
 
-  int isLoaded(){return loaded;}
+       int getHomeDir (string& userName, string& out);
 
+  int isLoaded (){return loaded;}
+
 private:
   Mutex loadMutex;
   int loadImpl();
 
 #ifdef WIN32
   string data;
-#else
+#elif !GETPWNAM
        HashMap<string, string*> data;
 #endif
        time_t timestamp;

Modified: trunk/myserver/src/base/home_dir/home_dir.cpp
===================================================================
--- trunk/myserver/src/base/home_dir/home_dir.cpp       2009-05-03 14:06:45 UTC 
(rev 3071)
+++ trunk/myserver/src/base/home_dir/home_dir.cpp       2009-05-03 14:06:50 UTC 
(rev 3072)
@@ -1,6 +1,6 @@
 /*
 MyServer
-Copyright (C) 2006, 2008 Free Software Foundation, Inc.
+Copyright (C) 2006, 2008, 2009 Free Software Foundation, Inc.
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
@@ -22,6 +22,7 @@
 #include <include/base/utility.h>
 #include <include/base/file/files_utility.h>
 
+
 #ifdef WIN32
 #include <userenv.h>
 #endif
@@ -30,6 +31,11 @@
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdio.h>
+
+#ifdef GETPWNAM
+#include <pwd.h>
+#endif
+
 #ifndef WIN32
 #include <errno.h>
 #include <netdb.h>
@@ -55,7 +61,7 @@
 {
 #ifdef WIN32
   data.assign("");
-#else
+#elif !GETPWNAM
   data.clear();
 #endif
   timestamp = 0;
@@ -80,7 +86,7 @@
 {
 #ifdef WIN32
   data.assign("");
-#else
+#elif !GETPWNAM
   HashMap<string, string*>::Iterator i = data.begin();
   for( ; i != data.end(); i++)
   {
@@ -129,7 +135,7 @@
     }
   }
   data.assign(buf);
-#else
+#elif !GETPWNAM
   File usersFile;
   u_long size;
   char* buffer;
@@ -138,12 +144,12 @@
 
   clear();
 
-  if(usersFile.openFile("/etc/passwd", File::MYSERVER_OPEN_READ | 
-                        File::MYSERVER_OPEN_IFEXISTS))
+  if (usersFile.openFile("/etc/passwd", File::MYSERVER_OPEN_READ | 
+                         File::MYSERVER_OPEN_IFEXISTS))
     return 1;
   size = usersFile.getFileSize();
   timestamp = usersFile.getLastModTime();
-  if(size == (u_long) -1)
+  if (size == (u_long) -1)
   {
     usersFile.close();
     return 1;
@@ -151,63 +157,62 @@
 
   buffer = new char[size+1]; 
 
-  usersFile.read(buffer, size, &read);
+  usersFile.read (buffer, size, &read);
   buffer[read] = '\0';
 
- for(counter = 0; counter < read; counter++)
-   if(buffer[counter] == ':')
+ for (counter = 0; counter < read; counter++)
+   if (buffer[counter] == ':')
      buffer[counter] = '\0';
 
-
-  for(counter = 0; counter < size;)
+  for (counter = 0; counter < size;)
   {
     char *username = 0;
     char *home = 0;
     string sUsername;
     string *sHome;
     string *old;
-     
-    if(buffer[counter] == '#')
+
+    if (buffer[counter] == '#')
     {
-      while(buffer[counter++] != '\n');
+      while (buffer[counter++] != '\n');
       continue;
     }
 
      /* Username.  */
     username = &buffer[counter];
-    
-     while(buffer[counter++] != '\0');
+
+    while (buffer[counter++] != '\0');
     /* Password.  */
 
-     while(buffer[counter++] != '\0');
+    while (buffer[counter++] != '\0');
     /* User ID.  */
 
-     while(buffer[counter++] != '\0');
+    while (buffer[counter++] != '\0');
     /* Group ID.  */
 
-     while(buffer[counter++] != '\0');
+    while (buffer[counter++] != '\0');
     /* Info.  */
 
-     while(buffer[counter++] != '\0');
+    while (buffer[counter++] != '\0');
     /* Home.  */
 
     home = &buffer[counter++];
-    sUsername = string(username);
-    sHome = new string(home);
+    sUsername = string (username);
+    sHome = new string (home);
 
-    old = data.put(sUsername, sHome);
+    old = data.put (sUsername, sHome);
 
-    if(old)
+    if (old)
       delete old;
 
-    while(buffer[counter++] != '\0');
+    while (buffer[counter++] != '\0');
     /* Shell.  */
 
-    while(buffer[counter++] != '\n');
+    while (buffer[counter++] != '\n');
     /* Next tuple.  */
   }
   delete [] buffer;
-  usersFile.close();
+  usersFile.close ();
 #endif
 
   loaded = 1;
@@ -215,58 +220,54 @@
 }
 
 /*!
- *Get the home directory for a specified user.
+ *Get the home directory for a specified user and write
+ *it directly to the supplied buffer.
  *\param userName The user name.
+ *\param out The buffer where write.
+ *\return 0 on success.
  */
-const string *HomeDir::getHomeDir(string& userName)
+int HomeDir::getHomeDir(string& userName, string& out)
 {
 #ifdef WIN32
-  static string retString;
-  retString.assign(data);
-  retString.append("/");
-  retString.append(userName);
-  return &retString;
-#else
-  if(!loaded)
-    return 0;
-  /* TODO: don't check always but wait some time before.  */
-  if(FilesUtility::getLastModTime("/etc/passwd") != timestamp)
-    load();
+  out.assign (data);
+  out.append ("/");
+  out.append (userName);
+#elif GETPWNAM
+  struct passwd *p;
+  int ret = 0;
 
-  return data.get(userName);
-#endif
-}
+  loadMutex.lock ();
 
+  p = ::getpwnam (userName.c_str ());
+  if (p == NULL)
+    ret = 1;
+  else
+    out.assign (p->pw_dir);
 
-/*!
- *Get the home directory for a specified user and write
- *it directly to the supplied buffer.
- *\param userName The user name.
- *\param out The buffer where write.
- */
-void HomeDir::getHomeDir(string& userName, string& out)
-{
-#ifdef WIN32
-  out.assign(data);
-  out.append("/");
-  out.append(userName);
+  loadMutex.unlock ();
+
+  return ret;
 #else
   static u_long lastCheckTime = 0;
   string *res = 0;
-  if(!loaded)
-    return;
-  if(getTicks() - lastCheckTime > MYSERVER_SEC(1))
+
+  if (!loaded)
+    return 1;
+
+  if (getTicks () - lastCheckTime > MYSERVER_SEC (1))
   {
-    if(FilesUtility::getLastModTime("/etc/passwd") != timestamp)
-      load();
-    lastCheckTime = getTicks();
+    if(FilesUtility::getLastModTime ("/etc/passwd") != timestamp)
+      load ();
+    lastCheckTime = getTicks ();
   }
-  res = data.get(userName);
-  if(res)
+  res = data.get (userName);
+  if (res)
   {
-    out.assign(*res);
+    out.assign (*res);
   }
   else
-    out.assign("");
+    out.assign ("");
 #endif
+
+  return 0;
 }

Modified: trunk/myserver/tests/test_homedir.cpp
===================================================================
--- trunk/myserver/tests/test_homedir.cpp       2009-05-03 14:06:45 UTC (rev 
3071)
+++ trunk/myserver/tests/test_homedir.cpp       2009-05-03 14:06:50 UTC (rev 
3072)
@@ -57,18 +57,12 @@
      * if it doesn't handle this differently.  */
     username.assign("root");
 #endif
+    string dir;
 
-    const string *dir = homeDir->getHomeDir(username);
-    CPPUNIT_ASSERT(dir);
-    CPPUNIT_ASSERT(dir->length());
+    CPPUNIT_ASSERT_EQUAL (homeDir->getHomeDir(username, dir), 0);
 
-    string dirOut;
+    CPPUNIT_ASSERT(dir.length());
 
-    homeDir->getHomeDir(username, dirOut);
-
-    CPPUNIT_ASSERT(dirOut.length());
-
-
     homeDir->clear();
   }
 

Modified: trunk/myserver/tests/test_socket.cpp
===================================================================
--- trunk/myserver/tests/test_socket.cpp        2009-05-03 14:06:45 UTC (rev 
3071)
+++ trunk/myserver/tests/test_socket.cpp        2009-05-03 14:06:50 UTC (rev 
3072)
@@ -23,26 +23,14 @@
 #include "../include/base/socket/socket.h"
 #include "../include/base/thread/thread.h"
 
-extern "C"
-{
 #include <string.h>
 #include <errno.h>
-
-#ifndef WIN32
 #include <arpa/inet.h>
-#endif
-
 #include <iostream>
-}
 
 using namespace std;
 
-#ifdef WIN32
-unsigned int  __stdcall
-#else
-void *
-#endif
-testRecvClient ( void* );
+void* testRecvClient ( void* );
 
 class TestSocket : public CppUnit::TestFixture
 {
@@ -114,7 +102,7 @@
     
     CPPUNIT_ASSERT( obj->listen ( 1 ) != -1 );
     
-    CPPUNIT_ASSERT (!Thread::create (&tid, testRecvClient, &port ));
+    CPPUNIT_ASSERT_EQUAL( Thread::create ( &tid, testRecvClient, &port ), 0 );
     
     Socket s = obj->accept ( &sockIn, &sockInLen );
     
@@ -171,11 +159,7 @@
 
 CPPUNIT_TEST_SUITE_REGISTRATION( TestSocket );
 
-#ifdef WIN32
-unsigned int  __stdcall
-#else
-void *
-#endif
+void*
 testRecvClient ( void *arg )
 {
   Socket *obj2 = new Socket;
@@ -196,6 +180,8 @@
   
   CPPUNIT_ASSERT( obj2->send ( buf, strlen ( buf ), 0 ) != -1 );
   
+  sleep ( 2 );
+  
   CPPUNIT_ASSERT( obj2->shutdown ( SD_BOTH ) != -1 );
  
   CPPUNIT_ASSERT( obj2->close ( ) != -1 );

Modified: trunk/myserver/tests/test_ssl_socket.cpp
===================================================================
--- trunk/myserver/tests/test_ssl_socket.cpp    2009-05-03 14:06:45 UTC (rev 
3071)
+++ trunk/myserver/tests/test_ssl_socket.cpp    2009-05-03 14:06:50 UTC (rev 
3072)
@@ -28,17 +28,11 @@
 #include "../include/base/file/files_utility.h"
 #include "../include/base/thread/thread.h"
 
-extern "C"
-{
 #include <string.h>
 #include <errno.h>
-
-#ifndef WIN32
 #include <arpa/inet.h>
-#endif
-
 #include <iostream>
-}
+
 using namespace std;
 
 
@@ -89,13 +83,9 @@
 -----END CERTIFICATE-----\n";
 
 
-#ifdef WIN32
-unsigned int  __stdcall
-#else
-void *
-#endif
-testSslRecvClient ( void* ); //
 
+void* testSslRecvClient ( void* ); //
+
 class TestSslSocket : public CppUnit::TestFixture
 {
   CPPUNIT_TEST_SUITE( TestSslSocket );
@@ -211,11 +201,7 @@
 
 CPPUNIT_TEST_SUITE_REGISTRATION( TestSslSocket );
 
-#ifdef WIN32
-unsigned int  __stdcall
-#else
-void *
-#endif
+void*
 testSslRecvClient ( void *arg )
 {
   Socket *obj2 = new Socket;





reply via email to

[Prev in Thread] Current Thread [Next in Thread]