DUNE-DAQ
DUNE Trigger and Data Acquisition software
Toggle main menu visibility
Loading...
Searching...
No Matches
dunedaq
sourcecode
okssystem
src
Descriptor.cpp
Go to the documentation of this file.
1
// DUNE DAQ modification notice:
2
// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3
// Fork baseline commit: system-00-00-20 (2020-09-25).
4
// Renamed since fork: yes (from src/Descriptor.cxx to src/Descriptor.cpp).
5
6
/*
7
* Descriptor.cxx
8
* OksSystem
9
*
10
* Created by Matthias Wiesmann on 08.02.05.
11
* Copyright 2005 CERN. All rights reserved.
12
*
13
*/
14
15
#include <fcntl.h>
16
#include <sys/stat.h>
17
#include <sys/types.h>
18
#include <unistd.h>
19
20
#include "
okssystem/Descriptor.hpp
"
21
#include "
okssystem/File.hpp
"
22
#include "
okssystem/exceptions.hpp
"
23
24
#include "
ers/ers.hpp
"
25
26
int
27
OksSystem::Descriptor::flags
(
bool
read_mode,
bool
write_mode)
28
{
29
if
(read_mode && write_mode) {
30
return
O_RDWR | O_CREAT;
31
}
32
if
(read_mode) {
33
return
O_RDONLY;
34
}
35
if
(write_mode) {
36
return
O_WRONLY | O_CREAT;
37
}
38
return
0;
39
}
// flags
40
41
OksSystem::Descriptor::Descriptor
(
const
File
* file,
int
i_flags, mode_t perm)
42
{
43
ERS_ASSERT
(file)
44
open
(file, i_flags, perm);
45
}
// Descriptor
46
47
OksSystem::Descriptor::~Descriptor
()
48
{
49
if
(
m_fd
>= 0) {
50
close_safe
();
51
}
52
}
// ~Descriptor
53
54
OksSystem::Descriptor::operator int()
const
throw()
55
{
56
return
m_fd
;
57
}
// operator int()
58
64
65
void
66
OksSystem::Descriptor::open
(
const
File
* file,
int
i_flags, mode_t perm)
67
{
68
ERS_ASSERT
(file)
69
bool
alreadyExists = file->exists();
70
m_fd
=
::open
(*file, i_flags, perm);
71
m_name
= file->full_name();
72
if
(
m_fd
< 0)
73
throw
OksSystem::OpenFileIssue(
ERS_HERE
, errno,
m_name
.c_str());
74
if
((alreadyExists ==
false
) && ((i_flags & O_CREAT) != 0)) {
75
::fchmod(
m_fd
, perm);
76
}
77
}
// open
78
83
84
void
85
OksSystem::Descriptor::close
()
86
{
87
const
int
status =
::close
(
m_fd
);
88
if
(status < 0) {
89
throw
OksSystem::CloseFileIssue(
ERS_HERE
, errno,
m_name
.c_str());
90
}
//
91
m_fd
= -1;
92
}
// close
93
97
98
void
99
OksSystem::Descriptor::close_safe
() throw()
100
{
101
const
int
status =
::close
(
m_fd
);
102
if
(status < 0) {
103
ers::warning
(OksSystem::CloseFileIssue(
ERS_HERE
, errno,
m_name
.c_str()));
104
}
// if
105
}
// close_safe
106
107
int
108
OksSystem::Descriptor::read
(
void
* buffer,
size_t
number)
const
109
{
110
ssize_t status =
::read
(
m_fd
, buffer, number);
111
if
(status < 0)
112
throw
OksSystem::ReadIssue(
ERS_HERE
, errno,
m_name
.c_str());
113
return
status;
114
}
// read
115
116
int
117
OksSystem::Descriptor::write
(
const
void
* buffer,
size_t
number)
const
118
{
119
ssize_t status =
::write
(
m_fd
, buffer, number);
120
if
(status < 0)
121
throw
OksSystem::WriteIssue(
ERS_HERE
, errno,
m_name
.c_str());
122
return
status;
123
}
// write
124
125
int
126
OksSystem::Descriptor::fd
()
const
throw()
127
{
128
return
m_fd
;
129
}
130
134
135
void
136
OksSystem::Descriptor::closeOnExec
()
137
{
138
139
bool
success =
false
;
140
int
storeErrno = 0;
141
142
int
oldFlags = ::fcntl(
m_fd
, F_GETFD);
143
if
(oldFlags != -1) {
144
int
newFlags = ::fcntl(
m_fd
, F_SETFD, oldFlags | FD_CLOEXEC);
145
if
(newFlags == -1) {
146
storeErrno = errno;
147
}
else
{
148
success =
true
;
149
}
150
}
else
{
151
storeErrno = errno;
152
}
153
154
if
(!success) {
155
std::string eMsg =
"File descriptor for file "
+
m_name
+
156
" will not be closed after exec. Reason: "
+ std::string(::strerror(storeErrno));
157
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, storeErrno,
"fcntl"
, eMsg.c_str());
158
}
159
}
ERS_ASSERT
#define ERS_ASSERT(expression)
Descriptor.hpp
File.hpp
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:127
OksSystem::Descriptor::Descriptor
Descriptor(const File *file, int flags, mode_t perm)
Definition
Descriptor.cpp:41
OksSystem::Descriptor::read
int read(void *buffer, size_t number) const
Definition
Descriptor.cpp:108
OksSystem::Descriptor::fd
int fd() const
file descritptor
Definition
Descriptor.cpp:126
OksSystem::Descriptor::m_fd
int m_fd
Definition
Descriptor.hpp:59
OksSystem::Descriptor::flags
static int flags(bool read_mode, bool write_mode)
Definition
Descriptor.cpp:27
OksSystem::Descriptor::~Descriptor
~Descriptor()
Definition
Descriptor.cpp:47
OksSystem::Descriptor::open
void open(const File *file, int flags, mode_t perm)
internal open method
Definition
Descriptor.cpp:66
OksSystem::Descriptor::closeOnExec
void closeOnExec()
It flags the file descriptor to be closed after any call to the exec okssystem function.
Definition
Descriptor.cpp:136
OksSystem::Descriptor::close
void close()
close the descriptor
Definition
Descriptor.cpp:85
OksSystem::Descriptor::close_safe
void close_safe()
close the descriptor no exception
Definition
Descriptor.cpp:99
OksSystem::Descriptor::write
int write(const void *buffer, size_t number) const
Definition
Descriptor.cpp:117
OksSystem::Descriptor::m_name
std::string m_name
internal file descriptor
Definition
Descriptor.hpp:60
OksSystem::File
Wrapper for file operations.
Definition
File.hpp:38
ers.hpp
ers::warning
void warning(const Issue &issue)
Definition
ers.hpp:150
exceptions.hpp
Generated on
for DUNE-DAQ by
1.18.0