Boost logo

Boost Users :

Subject: Re: [Boost-users] managed shared memory with basic_string
From: Bartek Taczała (w0lanto_at_[hidden])
Date: 2014-03-12 18:46:43


On Friday, March 07, 2014 04:01:32 PM Sean K wrote:
> now another problem has arisen since related to this managed shared memory.
>
> When the server is started and is running as root, the client cannot access
> or write to that shared memory unless the client is also running as root.
>
> How can that shared memory be accessed by a non-root ?
I assume you're working on POSIX like system. You can either:
* create shm from client not server
* use fchown on shm
* use fchmod on shm
* use chown,chmod utilities on /dev/shm/the_name_of_shm
B

>
> The server is allowed or must run as root so that it can do some root only
> operation with that string passed from the managed shared memory.
>
> On Fri, Mar 7, 2014 at 12:52 PM, Sean K <sk92129_at_[hidden]> wrote:
> > I figured it out.
> >
> > In the reader, I was using the wrong basic_string.
> >
> > by accident, I was using the std::basic_string. I should have been using
> > boost::interprocess::basic_string.
> >
> > I changed that. Then it worked.
> >
> > On Fri, Mar 7, 2014 at 12:37 PM, Sean K <sk92129_at_[hidden]> wrote:
> >> I am new to boost so please pardon if this question has already been
> >> asked.
> >>
> >> I was able to build a shared memory IPC client and server using "int"
> >> primitive type successfully.
> >>
> >> I need to send a fixed number of "char" (primitive type). At first I
> >> tried to define a struct of a fixed char
> >>
> >> struct {
> >>
> >> char data[1024];
> >>
> >> } dataset;
> >>
> >> typedef dataset mychararray;
> >>
> >> and use this like how this example shows for 'int"
> >>
> >>
> >> http://en.highscore.de/cpp/boost/interprocesscommunication.html#interproc
> >> esscommunication_managed_shared_memory
> >>
> >> But for some reason it did not compile due to the
> >> boost::interprocess::managed_shared_memory.construct () not accepting
> >> that kind of datatype.
> >>
> >> So, I looked at the basic_string implementation based on these two web
> >> sites.
> >> http://prakharprakhar.blogspot.com/2009/12/boost-shared-memory-vector.htm
> >> l
> >>
> >> http://en.highscore.de/cpp/boost/interprocesscommunication.html#interproc
> >> esscommunication_managed_shared_memory (basic_string variant)
> >>
> >> I am built a reader and writer separate app. The reader will look for
> >> content. If it does not find it, it will wait or sleep and try again,
> >> etc. If it finds something it will try to print the basic_string
> >> content
> >> that I sent over.
> >>
> >> I thought I had it correctly built but obviously it is not since the
> >> reader server crashes or cores when it tries to access the p.first
> >> content.
> >>
> >>
> >> Below are the two programs (along with the build.sh which compiles each).
> >>
> >> I am testing this as a 32 bit app on a 64 bit redhat5 and 32 bit
> >> centos6.
> >>
> >> You will notice in the strreader.cc that when I try to access the
> >> basic_string, that it crashes.
> >>
> >> I ran the reader (server) within the 32 bit eclipse debugger, and the
> >> memory addresses and content of the p BStringPair looks fine. Anybody
> >> have some ideas or can anybody try this and see what you can tell what is
> >> wrong.
> >>
> >> strreader.cc
> >>
> >>
> >>
> >> #include <iostream>
> >>
> >>
> >>
> >> #include <boost/interprocess/managed_shared_memory.hpp>
> >> #include <boost/thread/thread.hpp>
> >> #include <boost/interprocess/containers/vector.hpp>
> >> #include <boost/interprocess/allocators/allocator.hpp>
> >>
> >> #include <iostream>
> >> #include <string>
> >> #include <cstdlib>
> >>
> >> using namespace std;
> >> using namespace boost::interprocess;
> >>
> >>
> >> int main() {
> >>
> >> typedef boost::interprocess::allocator<char,
> >> boost::interprocess::managed_shared_memory::segment_manager>
> >> ShmemAllocator;>>
> >> typedef basic_string<char, std::char_traits<char>, ShmemAllocator>
> >>
> >> BString;
> >>
> >> typedef std::pair<BString *, std::size_t> BStringPair;
> >>
> >> int count = 0;
> >> shared_memory_object::remove("Highscore");
> >> while (true) {
> >>
> >> managed_shared_memory segment(open_or_create, "Highscore", 65536);
> >>
> >> if (segment.get_num_named_objects() > 0){
> >>
> >> BStringPair p = segment.find<BString> ("str");
> >>
> >> printf("stuff in memory\n");
> >>
> >> if (p.first ){
> >>
> >> // THIS IS WHERE IT CRASHES!!!
> >>
> >> //printf("%s \n",(p.first->c_str()) );
> >>
> >> //int strlen = p.first->size();
> >>
> >> }
> >> printf("p.second: %d \n", p.second);
> >>
> >> }
> >>
> >> shared_memory_object::remove("Highscore");
> >> //printf("one: %s", one.first);
> >>
> >> //printf("two: %s", two.first);
> >>
> >> int msecs = 2000;
> >>
> >> boost::this_thread::sleep(boost::posix_time::milliseconds(msecs));
> >>
> >> printf("sleeping...\n");
> >>
> >> }
> >>
> >> return 0;
> >> }
> >>
> >>
> >> build.sh for the reader
> >>
> >> g++ -m32 -fPIC -Xlinker -zmuldefs strreader.cc -o strreader
> >> -I/common/COTS/boost_1_42_0/include -L/common/COTS/boost_1_42_0/lib
> >> -lboost_thread -lboost_system -pthread
> >>
> >>
> >> strwriter.cpp
> >>
> >>
> >> #include <iostream>
> >>
> >>
> >>
> >> #include <boost/interprocess/managed_shared_memory.hpp>
> >> #include <boost/thread/thread.hpp>
> >> #include <boost/interprocess/containers/vector.hpp>
> >> #include <boost/interprocess/containers/string.hpp>
> >> #include <boost/interprocess/allocators/allocator.hpp>
> >>
> >> #include <iostream>
> >> #include <string>
> >> #include <cstdlib>
> >>
> >>
> >> using namespace std;
> >> using namespace boost::interprocess;
> >>
> >>
> >> int main() {
> >>
> >> typedef boost::interprocess::allocator<char,
> >> boost::interprocess::managed_shared_memory::segment_manager>
> >> ShmemAllocator;>>
> >> typedef boost::interprocess::basic_string<char, std::char_traits<char>,
> >>
> >> ShmemAllocator> BString;
> >>
> >>
> >> try {
> >>
> >> shared_memory_object::remove("Highscore");
> >>
> >> printf("cleared out memory\n");
> >>
> >>
> >> managed_shared_memory segment(open_or_create, "Highscore", 65536);
> >>
> >> printf("initialized managed shared memory \n");
> >>
> >> ShmemAllocator allocInst (segment.get_segment_manager());
> >>
> >> BString bstrData(segment.get_segment_manager());
> >>
> >> bstrData = "hello";
> >>
> >> std::string data = "Hello";
> >> //BString bstrToSend(data.begin(), data.end());
> >> //BString bstrToSend(data.c_str());
> >>
> >> BString *mystring = segment.construct<BString> ("str")(bstrData);
> >>
> >> //mystring="Hello";
> >>
> >> printf("inserted mystring into shmvector \n");
> >> } catch (boost::interprocess::bad_alloc e) {
> >>
> >> printf("we caught bad alloc\n");
> >>
> >> printf("explanation: %s", e.what());
> >> throw;
> >>
> >> } catch (...) {
> >>
> >> printf("caught unknown exception\n");
> >> throw;
> >>
> >> }
> >>
> >> printf("wrote number");
> >> return 0;
> >>
> >> }
> >>
> >> build.sh for the writer:
> >> g++ -m32 -fPIC -Xlinker -zmuldefs strwriter.cpp -o strwriter
> >> -I/common/COTS/boost_1_42_0/include -L/common/COTS/boost_1_42_0/lib
> >> -lboost_thread -lboost_system -pthread
> >>
> >> --
> >> Sean
> >
> > --
> > Sean


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net