1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/object.hpp"
4 #include "base/value.hpp"
5 #include "base/dictionary.hpp"
6 #include "base/primitivetype.hpp"
7 #include "base/utility.hpp"
8 #include "base/timer.hpp"
9 #include "base/logger.hpp"
10 #include "base/exception.hpp"
11 #include <boost/lexical_cast.hpp>
12 #include <boost/thread/recursive_mutex.hpp>
13 #include <thread>
14
15 using namespace icinga;
16
17 DEFINE_TYPE_INSTANCE(Object);
18
19 #ifdef I2_LEAK_DEBUG
20 static std::mutex l_ObjectCountLock;
21 static std::map<String, int> l_ObjectCounts;
22 static Timer::Ptr l_ObjectCountTimer;
23 #endif /* I2_LEAK_DEBUG */
24
25 /**
26 * Constructor for the Object class.
27 */
Object()28 Object::Object()
29 {
30 m_References.store(0);
31
32 #ifdef I2_DEBUG
33 m_LockOwner.store(decltype(m_LockOwner.load())());
34 #endif /* I2_DEBUG */
35 }
36
37 /**
38 * Destructor for the Object class.
39 */
~Object()40 Object::~Object()
41 {
42 }
43
44 /**
45 * Returns a string representation for the object.
46 */
ToString() const47 String Object::ToString() const
48 {
49 return "Object of type '" + GetReflectionType()->GetName() + "'";
50 }
51
52 #ifdef I2_DEBUG
53 /**
54 * Checks if the calling thread owns the lock on this object.
55 *
56 * @returns True if the calling thread owns the lock, false otherwise.
57 */
OwnsLock() const58 bool Object::OwnsLock() const
59 {
60 return m_LockOwner.load() == std::this_thread::get_id();
61 }
62 #endif /* I2_DEBUG */
63
SetField(int id,const Value &,bool,const Value &)64 void Object::SetField(int id, const Value&, bool, const Value&)
65 {
66 if (id == 0)
67 BOOST_THROW_EXCEPTION(std::runtime_error("Type field cannot be set."));
68 else
69 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
70 }
71
GetField(int id) const72 Value Object::GetField(int id) const
73 {
74 if (id == 0)
75 return GetReflectionType()->GetName();
76 else
77 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
78 }
79
HasOwnField(const String & field) const80 bool Object::HasOwnField(const String& field) const
81 {
82 Type::Ptr type = GetReflectionType();
83
84 if (!type)
85 return false;
86
87 return type->GetFieldId(field) != -1;
88 }
89
GetOwnField(const String & field,Value * result) const90 bool Object::GetOwnField(const String& field, Value *result) const
91 {
92 Type::Ptr type = GetReflectionType();
93
94 if (!type)
95 return false;
96
97 int tid = type->GetFieldId(field);
98
99 if (tid == -1)
100 return false;
101
102 *result = GetField(tid);
103 return true;
104 }
105
GetFieldByName(const String & field,bool sandboxed,const DebugInfo & debugInfo) const106 Value Object::GetFieldByName(const String& field, bool sandboxed, const DebugInfo& debugInfo) const
107 {
108 Type::Ptr type = GetReflectionType();
109
110 if (!type)
111 return Empty;
112
113 int fid = type->GetFieldId(field);
114
115 if (fid == -1)
116 return GetPrototypeField(const_cast<Object *>(this), field, true, debugInfo);
117
118 if (sandboxed) {
119 Field fieldInfo = type->GetFieldInfo(fid);
120
121 if (fieldInfo.Attributes & FANoUserView)
122 BOOST_THROW_EXCEPTION(ScriptError("Accessing the field '" + field + "' for type '" + type->GetName() + "' is not allowed in sandbox mode.", debugInfo));
123 }
124
125 return GetField(fid);
126 }
127
SetFieldByName(const String & field,const Value & value,bool overrideFrozen,const DebugInfo & debugInfo)128 void Object::SetFieldByName(const String& field, const Value& value, bool overrideFrozen, const DebugInfo& debugInfo)
129 {
130 Type::Ptr type = GetReflectionType();
131
132 if (!type)
133 BOOST_THROW_EXCEPTION(ScriptError("Cannot set field on object.", debugInfo));
134
135 int fid = type->GetFieldId(field);
136
137 if (fid == -1)
138 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' does not exist.", debugInfo));
139
140 try {
141 SetField(fid, value);
142 } catch (const boost::bad_lexical_cast&) {
143 Field fieldInfo = type->GetFieldInfo(fid);
144 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
145 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
146 } catch (const std::bad_cast&) {
147 Field fieldInfo = type->GetFieldInfo(fid);
148 Type::Ptr ftype = Type::GetByName(fieldInfo.TypeName);
149 BOOST_THROW_EXCEPTION(ScriptError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "', expected '" + ftype->GetName() + "'", debugInfo));
150 }
151 }
152
Validate(int types,const ValidationUtils & utils)153 void Object::Validate(int types, const ValidationUtils& utils)
154 {
155 /* Nothing to do here. */
156 }
157
ValidateField(int id,const Lazy<Value> & lvalue,const ValidationUtils & utils)158 void Object::ValidateField(int id, const Lazy<Value>& lvalue, const ValidationUtils& utils)
159 {
160 /* Nothing to do here. */
161 }
162
NotifyField(int id,const Value & cookie)163 void Object::NotifyField(int id, const Value& cookie)
164 {
165 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
166 }
167
NavigateField(int id) const168 Object::Ptr Object::NavigateField(int id) const
169 {
170 BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
171 }
172
Clone() const173 Object::Ptr Object::Clone() const
174 {
175 BOOST_THROW_EXCEPTION(std::runtime_error("Object cannot be cloned."));
176 }
177
GetReflectionType() const178 Type::Ptr Object::GetReflectionType() const
179 {
180 return Object::TypeInstance;
181 }
182
GetPrototypeField(const Value & context,const String & field,bool not_found_error,const DebugInfo & debugInfo)183 Value icinga::GetPrototypeField(const Value& context, const String& field, bool not_found_error, const DebugInfo& debugInfo)
184 {
185 Type::Ptr ctype = context.GetReflectionType();
186 Type::Ptr type = ctype;
187
188 do {
189 Object::Ptr object = type->GetPrototype();
190
191 if (object && object->HasOwnField(field))
192 return object->GetFieldByName(field, false, debugInfo);
193
194 type = type->GetBaseType();
195 } while (type);
196
197 if (not_found_error)
198 BOOST_THROW_EXCEPTION(ScriptError("Invalid field access (for value of type '" + ctype->GetName() + "'): '" + field + "'", debugInfo));
199 else
200 return Empty;
201 }
202
203 #ifdef I2_LEAK_DEBUG
TypeAddObject(Object * object)204 void icinga::TypeAddObject(Object *object)
205 {
206 std::unique_lock<std::mutex> lock(l_ObjectCountLock);
207 String typeName = Utility::GetTypeName(typeid(*object));
208 l_ObjectCounts[typeName]++;
209 }
210
TypeRemoveObject(Object * object)211 void icinga::TypeRemoveObject(Object *object)
212 {
213 std::unique_lock<std::mutex> lock(l_ObjectCountLock);
214 String typeName = Utility::GetTypeName(typeid(*object));
215 l_ObjectCounts[typeName]--;
216 }
217
TypeInfoTimerHandler()218 static void TypeInfoTimerHandler()
219 {
220 std::unique_lock<std::mutex> lock(l_ObjectCountLock);
221
222 typedef std::map<String, int>::value_type kv_pair;
223 for (kv_pair& kv : l_ObjectCounts) {
224 if (kv.second == 0)
225 continue;
226
227 Log(LogInformation, "TypeInfo")
228 << kv.second << " " << kv.first << " objects";
229
230 kv.second = 0;
231 }
232 }
233
__anon2a4fe7030102() 234 INITIALIZE_ONCE([]() {
235 l_ObjectCountTimer = new Timer();
236 l_ObjectCountTimer->SetInterval(10);
237 l_ObjectCountTimer->OnTimerExpired.connect([](const Timer * const&) { TypeInfoTimerHandler(); });
238 l_ObjectCountTimer->Start();
239 });
240 #endif /* I2_LEAK_DEBUG */
241
intrusive_ptr_add_ref(Object * object)242 void icinga::intrusive_ptr_add_ref(Object *object)
243 {
244 #ifdef I2_LEAK_DEBUG
245 if (object->m_References.fetch_add(1) == 0u)
246 TypeAddObject(object);
247 #else /* I2_LEAK_DEBUG */
248 object->m_References.fetch_add(1);
249 #endif /* I2_LEAK_DEBUG */
250 }
251
intrusive_ptr_release(Object * object)252 void icinga::intrusive_ptr_release(Object *object)
253 {
254 auto previous (object->m_References.fetch_sub(1));
255
256 if (previous == 1u) {
257 #ifdef I2_LEAK_DEBUG
258 TypeRemoveObject(object);
259 #endif /* I2_LEAK_DEBUG */
260
261 delete object;
262 }
263 }
264
DefaultObjectFactoryCheckArgs(const std::vector<Value> & args)265 void icinga::DefaultObjectFactoryCheckArgs(const std::vector<Value>& args)
266 {
267 if (!args.empty())
268 BOOST_THROW_EXCEPTION(std::invalid_argument("Constructor does not take any arguments."));
269 }
270
RequireNotNullInternal(const intrusive_ptr<Object> & object,const char * description)271 void icinga::RequireNotNullInternal(const intrusive_ptr<Object>& object, const char *description)
272 {
273 if (!object)
274 BOOST_THROW_EXCEPTION(std::invalid_argument("Pointer must not be null: " + String(description)));
275 }
276