ref: refs/heads/master
commit b01e5838829f1f969c176ac24e34f7967dd4d6d2
Author: Michael Andres
Date: Tue Jul 21 17:43:03 2009 +0200
Install helper draft
---
CMakeLists.txt | 1 +
helper/CMakeLists.txt | 14 ++++++
helper/installHelper.cc | 74 ++++++++++++++++++++++++++++++
helper/installHelper.h | 69 ++++++++++++++++++++++++++++
helper/test.cc | 114 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 272 insertions(+), 0 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e7ab842..d7dcb8f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -98,6 +98,7 @@ MESSAGE(STATUS "version: ${VERSION}")
####################################################################
ADD_SUBDIRECTORY( src )
+ADD_SUBDIRECTORY( helper )
ADD_SUBDIRECTORY( doc )
ADD_SUBDIRECTORY( mof )
diff --git a/helper/CMakeLists.txt b/helper/CMakeLists.txt
new file mode 100644
index 0000000..e7ca22a
--- /dev/null
+++ b/helper/CMakeLists.txt
@@ -0,0 +1,14 @@
+## ############################################################
+
+FILE( GLOB ALLCC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc" )
+STRING( REPLACE ".cc" ";" APLLPROG ${ALLCC} )
+FOREACH( loop_var ${APLLPROG} )
+ ADD_EXECUTABLE( ${loop_var}
+ ${loop_var}.cc
+ )
+ TARGET_LINK_LIBRARIES( ${loop_var}
+ zypp
+ )
+ENDFOREACH( loop_var )
+
+## ############################################################
diff --git a/helper/installHelper.cc b/helper/installHelper.cc
new file mode 100644
index 0000000..83d106f
--- /dev/null
+++ b/helper/installHelper.cc
@@ -0,0 +1,74 @@
+#include <iostream>
+#include <fstream>
+
+#include
+#include
+#include
+#include
+#include "installHelper.h"
+
+using namespace zypp;
+using namespace cmpizypp;
+using namespace boost::interprocess;
+
+using std::endl;
+
+
+Comm & getshmem()
+{
+ static shared_memory_object shm( open_only, SHM_NAME, read_write );
+ //Map the whole shared memory in this process
+ static mapped_region region( shm, read_write );
+ return * static_cast(region.get_address());
+}
+
+
+int main( int argc, char * argv[] )
+try {
+ //zypp::base::LogControl::instance().logToStdErr();
+ INT << "===[START]==========================================" << endl;
+ ///////////////////////////////////////////////////////////////////
+
+ MIL << "InstallHelper " << getpid() << endl;
+
+ MIL << "Waiting for lock..." << endl;
+ std::string iFile;
+ {
+ CommAccess c;
+ MIL << c->pid << endl;
+ if ( c->pid != getpid() )
+ {
+ ERR << "Not my pid: " << c->pid << "(" << getpid() << ")" << endl;
+ return 1;
+ }
+
+ if ( * c->dataStr )
+ iFile = c->dataStr;
+ }
+
+ MIL << "read " << iFile << endl;
+ std::ifstream infile( iFile.c_str() );
+ for( iostr::EachLine in( infile ); in; in.next() )
+ {
+ DBG << *in << endl;
+ }
+
+
+
+
+
+
+
+ MIL << "Done" << endl;
+
+ ///////////////////////////////////////////////////////////////////
+ INT << "===[END]============================================" << endl << endl;
+ zypp::base::LogControl::instance().logNothing();
+ return 0;
+}
+catch ( const Exception & exp )
+{
+ INT << exp << endl << exp.historyAsString();
+}
+catch (...)
+{ INT << "exception" << endl; }
diff --git a/helper/installHelper.h b/helper/installHelper.h
new file mode 100644
index 0000000..8bfa668
--- /dev/null
+++ b/helper/installHelper.h
@@ -0,0 +1,69 @@
+#ifndef INSTALLHELPER_H
+#define INSTALLHELPER_H
+
+#include
+#include
+#include
+#include
+#include
+
+#define SHM_NAME "MySharedMemory"
+#define STR_SIZE 256
+
+#define MAX_ANSWERS 10
+
+namespace cmpizypp
+{
+ enum Answers
+ {
+ A_unknown = 0,
+ A_yes, A_no, A_cancel, A_abort
+ };
+
+ struct Comm
+ {
+ Comm() : lock(1) {}
+
+ boost::interprocess::interprocess_semaphore lock;
+
+ pid_t pid;
+ uint16_t status;
+ uint16_t percent;
+ uint16_t error;
+ char errorStr[STR_SIZE];
+ uint16_t answers[MAX_ANSWERS];
+ char questionStr[STR_SIZE];
+ char dataStr[STR_SIZE];
+ };
+
+
+ extern Comm & getshmem();
+
+ struct CommAccess : private boost::noncopyable
+ {
+ CommAccess()
+ : _c( getshmem() )
+ {
+ if ( ! _c.lock.timed_wait( boost::posix_time::from_time_t( ::time(0)+5 ) ) )
+ {
+ throw "got no lock";
+ }
+ }
+
+ ~CommAccess()
+ {
+ _c.lock.post();
+ }
+
+ const Comm * operator->() const
+ { return &_c; }
+
+ Comm * operator->()
+ { return &_c; }
+
+ private:
+ Comm & _c;
+ };
+
+}
+#endif
\ No newline at end of file
diff --git a/helper/test.cc b/helper/test.cc
new file mode 100644
index 0000000..ded03b1
--- /dev/null
+++ b/helper/test.cc
@@ -0,0 +1,114 @@
+#include
+#include
+
+#include <iostream>
+#include <fstream>
+
+#include
+#include
+#include
+
+#include "installHelper.h"
+
+using namespace zypp;
+using namespace cmpizypp;
+using namespace boost::interprocess;
+
+using std::endl;
+
+Comm & getshmem()
+{
+ static bool initialized = false;
+ if ( ! initialized )
+ {
+ // Create a shared memory object.
+ MIL << "Create shared_memory_object " << SHM_NAME << endl;
+ }
+ static ::mode_t mask = ::umask( 0077 );
+ static shared_memory_object shm ( create_only, SHM_NAME, read_write );
+ if ( ! initialized )
+ {
+ ::umask( mask );
+ // Set size
+ shm.truncate( 1000 );
+ }
+ // Map the whole shared memory in this process
+ static mapped_region region( shm, read_write );
+ if ( ! initialized )
+ {
+ // Write all the memory to '0'
+ std::memset( region.get_address(), 0, region.get_size() );
+ // Initialize
+ new ( region.get_address() ) Comm;
+ initialized = true;
+ }
+ return *static_cast(region.get_address());
+}
+
+int main( int argc, char * argv[] )
+try {
+ //zypp::base::LogControl::instance().logToStdErr();
+ INT << "===[START]==========================================" << endl;
+ ///////////////////////////////////////////////////////////////////
+
+ struct shm_remove
+ {
+ shm_remove() {
+ MIL << "Remove shared_memory_object " << SHM_NAME << endl;
+ shared_memory_object::remove(SHM_NAME);
+ }
+ ~shm_remove()
+ {
+ MIL << "Remove shared_memory_object " << SHM_NAME << endl;
+ shared_memory_object::remove(SHM_NAME);
+ }
+ } remover;
+
+ // write task list
+ std::string path( TmpFile().path() );
+ std::ofstream o( path.c_str() );
+ o << "hi" << endl;
+ o.close();
+
+ pid_t p = 0;
+ {
+ CommAccess c;
+
+ // Launch child process
+ p = fork();
+ if ( p == 0 )
+ {
+ execvp( "./installHelper", NULL );
+ _exit( 128 );
+ }
+
+ // parent
+ MIL << "installHelper started (" << p << ")" << endl;
+ c->pid = p;
+ strncpy( c->dataStr, path.c_str(), STR_SIZE );
+ }
+
+ int ret = 0;
+ int status = 0;
+ do
+ {
+ MIL << "ping..." << endl;
+ sleep( 5 );
+ ret = waitpid( p, &status, WNOHANG );
+ }
+ while ( ret == 0 || ( ret == -1 && errno == EINTR ) );
+ MIL << "installHelper exited!" << endl;
+
+ ///////////////////////////////////////////////////////////////////
+ INT << "===[END]============================================" << endl << endl;
+ zypp::base::LogControl::instance().logNothing();
+ return 0;
+}
+catch ( const Exception & exp )
+{
+ INT << exp << endl << exp.historyAsString();
+}
+catch (const char * i)
+{ INT << "Exception :( " << i << endl; }
+catch (...)
+{ INT << "Exception :(" << endl; }
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org