1    	/****************************************************************************
2    	 *   Copyright (C) 2006-2008 by Jason Ansel, Kapil Arya, and Gene Cooperman *
3    	 *   jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu              *
4    	 *                                                                          *
5    	 *   This file is part of the dmtcp/src module of DMTCP (DMTCP:dmtcp/src).  *
6    	 *                                                                          *
7    	 *  DMTCP:dmtcp/src is free software: you can redistribute it and/or        *
8    	 *  modify it under the terms of the GNU Lesser General Public License as   *
9    	 *  published by the Free Software Foundation, either version 3 of the      *
10   	 *  License, or (at your option) any later version.                         *
11   	 *                                                                          *
12   	 *  DMTCP:dmtcp/src is distributed in the hope that it will be useful,      *
13   	 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
14   	 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
15   	 *  GNU Lesser General Public License for more details.                     *
16   	 *                                                                          *
17   	 *  You should have received a copy of the GNU Lesser General Public        *
18   	 *  License along with DMTCP:dmtcp/src.  If not, see                        *
19   	 *  <http://www.gnu.org/licenses/>.                                         *
20   	 ****************************************************************************/
21   	
22   	#ifndef SYSVIPC_H
23   	#define SYSVIPC_H
24   	
25   	#include "dmtcpalloc.h"
26   	#include "dmtcpworker.h"
27   	#include <vector>
28   	#include <sys/types.h>
29   	#include <sys/socket.h>
30   	#include <map>
31   	#include  "../jalib/jbuffer.h"
32   	#include  "../jalib/jserialize.h"
33   	#include  "../jalib/jassert.h"
34   	#include  "../jalib/jconvert.h"
35   	#include "../jalib/jalloc.h"
36   	#include <sys/types.h>
37   	#include <sys/stat.h>
38   	#include <unistd.h>
39   	#include <sys/ipc.h>
40   	#include <sys/shm.h>
41   	
42   	namespace dmtcp
43   	{
44   	  class ShmSegment;
45   	
46   	  class SysVIPC
47   	  {
48   	    enum SysVIPCTyle {
49   	      SHM = 0x10000,
50   	      TYPEMASK = SHM
51   	    };
52   	
53   	    public:
54   	#ifdef JALIB_ALLOCATOR
55   	      static void* operator new(size_t nbytes, void* p) { return p; }
56   	      static void* operator new(size_t nbytes) { JALLOC_HELPER_NEW(nbytes); }
57   	      static void  operator delete(void* p) { JALLOC_HELPER_DELETE(p); }
58   	#endif
59   	
60   	      SysVIPC();
61   	
62   	      static SysVIPC& instance();
63   	
64   	      int  originalToCurrentShmid(int shmid);
65   	      int  currentToOriginalShmid(int shmid);
66   	      bool isConflictingShmid(int shmid);
67   	
68   	      void prepareForLeaderElection();
69   	      void leaderElection();
70   	      void preCheckpoint();
71   	      void postCheckpoint();
72   	      void postRestart();
73   	      void preResume();
74   	
75   	      dmtcp::vector<int> getShmids();
76   	
77   	      int  shmaddrToShmid(const void* shmaddr);
78   	      void removeStaleShmObjects();
79   	
80   	      void serializeOriginalShmids();
81   	      void InsertIntoShmidMapFile(int originalShmid, int currentShmid);
82   	      void readPidMapsFromFile();
83   	
84   	      void on_shmget(key_t key, size_t size, int shmflg, int shmid);
85   	      void on_shmat(int shmid, const void *shmaddr, int shmflg, void* newaddr);
86   	      void on_shmdt(const void *shmaddr);
87   	
88   	      void writeShmidMapsToFile(int fd);
89   	      void readShmidMapsFromFile(int fd);
90   	      void serialize(jalib::JBinarySerializer& o);
91   	    private:
92   	      typedef dmtcp::map<int, ShmSegment>::iterator ShmIterator;
93   	      dmtcp::map<int, ShmSegment> _shm;
94   	      typedef dmtcp::map<int, int>::iterator ShmidMapIter;
95   	      dmtcp::map<int, int> _originalToCurrentShmids;
96   	  };
97   	
Event read_parm_fld: Reading a parameter field.
Event read_parm_fld: Reading a parameter field.
Event read_parm_fld: Reading a parameter field.
Event read_parm_fld: Reading a parameter field.
Event read_parm_fld: Reading a parameter field.
Event read_parm_fld: Reading a parameter field.
98   	  class ShmSegment
99   	  {
100  	    public:
101  	#ifdef JALIB_ALLOCATOR
102  	      static void* operator new(size_t nbytes, void* p) { return p; }
103  	      static void* operator new(size_t nbytes) { JALLOC_HELPER_NEW(nbytes); }
104  	      static void  operator delete(void* p) { JALLOC_HELPER_DELETE(p); }
105  	#endif
106  	
107  	      struct shmMetaInfo {
108  	        pid_t pid;
109  	        int   creatorSignature;
110  	      };
111  	
112  	      ShmSegment() { _originalShmid = -1; }
113  	      ShmSegment(int shmid);
114  	      ShmSegment(key_t key, int size, int shmflg, int shmid);
115  	
116  	      bool isStale();
117  	
118  	      int originalShmid() { return _originalShmid; }
119  	      int currentShmid()  { return _currentShmid; }
120  	
121  	      void updateCurrentShmid(int shmid) { _currentShmid = shmid; }
122  	
123  	      bool isValidShmaddr(const void* shmaddr);
124  	      bool isOwner() { return getpid() == _ownerInfo.pid; };
125  	
126  	      void prepareForLeaderElection();
127  	      void leaderElection();
128  	      void preCheckpoint();
129  	
130  	      void remapAll();
131  	      void recreateShmSegment();
132  	      void remapFirstAddrForOwnerOnRestart();
133  	
134  	      void on_shmget (key_t key, size_t size, int shmflg, int shmid);
135  	      void on_shmat  (void *shmaddr, int shmflg);
136  	      void on_shmdt  (const void *shmaddr);
137  	
138  	//      virtual void preCheckpoint ( const dmtcp::vector<int>& fds
139  	//                                   , KernelBufferDrainer& drain );
140  	//      virtual void postCheckpoint ( const dmtcp::vector<int>& fds );
141  	//      virtual void restore ( const dmtcp::vector<int>&, ConnectionRewirer& );
142  	//      virtual void restoreOptions ( const dmtcp::vector<int>& fds );
143  	//
144  	//      virtual void serializeSubClass ( jalib::JBinarySerializer& o );
145  	
146  	    private:
147  	      key_t   _key;
148  	      int     _shmgetFlags;
149  	      int     _originalShmid;
150  	      int     _currentShmid;
151  	      int     _size;
152  	      int     _creatorPid;
153  	      int     _dmtcpMappedAddr;
154  	      shmatt_t _nattch;
155  	      unsigned short _mode;
156  	      struct shmid_ds _shminfo;
157  	      struct  shmMetaInfo _originalInfo;
158  	      struct  shmMetaInfo _ownerInfo;
159  	      typedef dmtcp::map<void*, int> ShmaddrToFlag;
160  	      typedef dmtcp::map<void*, int>::iterator ShmaddrToFlagIter;
161  	      ShmaddrToFlag _shmaddrToFlag;
162  	  };
163  	}
164  	#endif