/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- _hasLock
- addFd
- removeFd
- saveOptions
- restoreOptions
- doLocking
- checkLocking
- serialize
1 /****************************************************************************
2 * Copyright (C) 2006-2013 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 #include <fcntl.h>
23 #include "connection.h"
24 #include "../jalib/jassert.h"
25 #include "../jalib/jserialize.h"
26
27 using namespace dmtcp;
28
29 Connection::Connection(uint32_t t)
30 : _id(ConnectionIdentifier::create())
31 , _type((ConnectionType) t)
32 , _fcntlFlags(-1)
33 , _fcntlOwner(-1)
34 , _fcntlSignal(-1)
35 , _hasLock(false)
36 {}
37
38 void Connection::addFd(int fd)
39 {
40 _fds.push_back(fd);
41 }
42
43 void Connection::removeFd(int fd)
44 {
45 JASSERT(_fds.size() > 0);
46 if (_fds.size() == 1) {
47 JASSERT(_fds[0] == fd);
48 _fds.clear();
49 } else {
50 for (size_t i = 0; i < _fds.size(); i++) {
51 if (_fds[i] == fd) {
52 _fds.erase(_fds.begin() + i);
53 break;
54 }
55 }
56 }
57 }
58
59 void Connection::saveOptions()
60 {
61 errno = 0;
62 _fcntlFlags = fcntl(_fds[0],F_GETFL);
63 JASSERT(_fcntlFlags >= 0) (_fds[0]) (_fcntlFlags) (_type) (JASSERT_ERRNO);
64 errno = 0;
65 _fcntlOwner = fcntl(_fds[0],F_GETOWN);
66 JASSERT(_fcntlOwner != -1) (_fcntlOwner) (JASSERT_ERRNO);
67 errno = 0;
68 _fcntlSignal = fcntl(_fds[0],F_GETSIG);
69 JASSERT(_fcntlSignal >= 0) (_fcntlSignal) (JASSERT_ERRNO);
70 }
71
72 void Connection::restoreOptions()
73 {
74 //restore F_GETFL flags
75 JASSERT(_fcntlFlags >= 0) (_fcntlFlags);
76 JASSERT(_fcntlOwner != -1) (_fcntlOwner);
77 JASSERT(_fcntlSignal >= 0) (_fcntlSignal);
78 errno = 0;
79 JASSERT(fcntl(_fds[0], F_SETFL, (int)_fcntlFlags) == 0)
80 (_fds[0]) (_fcntlFlags) (JASSERT_ERRNO);
81
82 errno = 0;
83 JASSERT(fcntl(_fds[0], F_SETOWN, (int)_fcntlOwner) == 0)
84 (_fds[0]) (_fcntlOwner) (JASSERT_ERRNO);
85
86 //FIXME: The comment below seems to be obsolete now.
87 // This JASSERT will almost always trigger until we fix the above mentioned
88 // bug.
89 //JASSERT(fcntl(_fds[0], F_GETOWN) == _fcntlOwner)
90 //(fcntl(_fds[0], F_GETOWN)) (_fcntlOwner) (VIRTUAL_TO_REAL_PID(_fcntlOwner));
91
92 errno = 0;
93 JASSERT(fcntl(_fds[0], F_SETSIG, (int)_fcntlSignal) == 0)
94 (_fds[0]) (_fcntlSignal) (JASSERT_ERRNO);
95 }
96
97 void Connection::doLocking()
98 {
99 errno = 0;
100 _hasLock = false;
101 JASSERT(fcntl(_fds[0], F_SETOWN, getpid()) == 0)
102 (_fds[0]) (JASSERT_ERRNO);
103 }
104
105 void Connection::checkLocking()
106 {
107 pid_t pid = fcntl(_fds[0], F_GETOWN);
108 JASSERT(pid != -1);
109 _hasLock = pid == getpid();
110 }
111
112 void Connection::serialize(jalib::JBinarySerializer& o)
113 {
114 JSERIALIZE_ASSERT_POINT("Connection");
115 o & _id & _type & _fcntlFlags & _fcntlOwner & _fcntlSignal;
116 o.serializeVector(_fds);
117 serializeSubClass(o);
118 }