xref: /openbmc/openpower-proc-control/filedescriptor.cpp (revision e04c1173c1d76575fd1cf968d421bdf1dec82606)
1 /**
2  * Copyright (C) 2017 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "filedescriptor.hpp"
17 
18 #include <unistd.h>
19 
20 #include <phosphor-logging/elog-errors.hpp>
21 #include <phosphor-logging/elog.hpp>
22 #include <xyz/openbmc_project/Common/File/error.hpp>
23 
24 #include <stdexcept>
25 
26 namespace openpower
27 {
28 namespace util
29 {
30 
31 namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
32 
33 FileDescriptor::FileDescriptor(const std::string& path)
34 {
35     using namespace phosphor::logging;
36 
37     fd = open(path.c_str(), O_RDWR | O_SYNC);
38 
39     if (fd < 0)
40     {
41         using metadata = xyz::openbmc_project::Common::File::Open;
42 
43         elog<file_error::Open>(metadata::ERRNO(errno),
44                                metadata::PATH(path.c_str()));
45     }
46 }
47 
48 FileDescriptor::~FileDescriptor()
49 {
50     if (fd >= 0)
51     {
52         close(fd);
53     }
54 }
55 
56 } // namespace util
57 } // namespace openpower
58