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 <sys/types.h>
16
#include <sys/stat.h>
17
#include <fcntl.h>
18
#include <unistd.h>
19
20
#include "
okssystem/File.hpp
"
21
#include "
okssystem/Descriptor.hpp
"
22
#include "
okssystem/exceptions.hpp
"
23
24
#include "
ers/ers.hpp
"
25
26
int
OksSystem::Descriptor::flags
(
bool
read_mode,
bool
write_mode) {
27
if
(read_mode && write_mode) {
28
return
O_RDWR | O_CREAT;
29
}
30
if
(read_mode) {
31
return
O_RDONLY;
32
}
33
if
(write_mode) {
34
return
O_WRONLY | O_CREAT;
35
}
36
return
0;
37
}
// flags
38
39
40
OksSystem::Descriptor::Descriptor
(
const
File
* file,
int
i_flags, mode_t perm) {
41
ERS_ASSERT
( file )
42
open
(file,i_flags,perm);
43
}
// Descriptor
44
45
OksSystem::Descriptor::~Descriptor
() {
46
if
(
m_fd
>=0) {
47
close_safe
();
48
}
49
}
// ~Descriptor
50
51
OksSystem::Descriptor::operator int()
const
throw() {
52
return
m_fd
;
53
}
// operator int()
54
55
61
62
void
OksSystem::Descriptor::open
(
const
File
* file,
int
i_flags, mode_t perm) {
63
ERS_ASSERT
( file )
64
bool
alreadyExists = file->exists();
65
m_fd
=
::open
(*file,i_flags,perm);
66
m_name
= file->full_name();
67
if
(
m_fd
<0)
throw
OksSystem::OpenFileIssue(
ERS_HERE
, errno,
m_name
.c_str() );
68
if
((alreadyExists ==
false
) && ((i_flags & O_CREAT) != 0)) {
69
::fchmod(
m_fd
, perm);
70
}
71
}
// open
72
77
78
void
OksSystem::Descriptor::close
() {
79
const
int
status =
::close
(
m_fd
);
80
if
(status<0) {
81
throw
OksSystem::CloseFileIssue(
ERS_HERE
, errno,
m_name
.c_str() );
82
}
//
83
m_fd
= -1 ;
84
}
// close
85
86
90
91
void
OksSystem::Descriptor::close_safe
() throw() {
92
const
int
status =
::close
(
m_fd
);
93
if
(status<0) {
94
ers::warning
( OksSystem::CloseFileIssue(
ERS_HERE
, errno,
m_name
.c_str() ) );
95
}
// if
96
}
// close_safe
97
98
int
OksSystem::Descriptor::read
(
void
* buffer,
size_t
number)
const
{
99
ssize_t status =
::read
(
m_fd
,buffer,number);
100
if
(status<0)
throw
OksSystem::ReadIssue(
ERS_HERE
, errno,
m_name
.c_str() );
101
return
status;
102
}
// read
103
104
105
int
OksSystem::Descriptor::write
(
const
void
* buffer,
size_t
number)
const
{
106
ssize_t status =
::write
(
m_fd
,buffer,number);
107
if
(status<0)
throw
OksSystem::WriteIssue(
ERS_HERE
, errno,
m_name
.c_str() );
108
return
status;
109
}
// write
110
111
int
OksSystem::Descriptor::fd
()
const
throw() {
112
return
m_fd
;
113
}
114
118
119
void
OksSystem::Descriptor::closeOnExec
() {
120
121
bool
success =
false
;
122
int
storeErrno = 0;
123
124
int
oldFlags = ::fcntl(
m_fd
, F_GETFD);
125
if
(oldFlags != -1) {
126
int
newFlags = ::fcntl(
m_fd
, F_SETFD, oldFlags|FD_CLOEXEC);
127
if
(newFlags == -1) {
128
storeErrno = errno;
129
}
else
{
130
success =
true
;
131
}
132
}
else
{
133
storeErrno = errno;
134
}
135
136
if
(!success) {
137
std::string eMsg =
"File descriptor for file "
+
m_name
+
138
" will not be closed after exec. Reason: "
+ std::string(::strerror(storeErrno));
139
throw
OksSystem::OksSystemCallIssue(
ERS_HERE
, storeErrno,
"fcntl"
, eMsg.c_str());
140
}
141
142
}
ERS_ASSERT
#define ERS_ASSERT(expression)
Descriptor.hpp
File.hpp
ERS_HERE
#define ERS_HERE
Definition
LocalContext.hpp:141
OksSystem::Descriptor::Descriptor
Descriptor(const File *file, int flags, mode_t perm)
Definition
Descriptor.cpp:40
OksSystem::Descriptor::read
int read(void *buffer, size_t number) const
Definition
Descriptor.cpp:98
OksSystem::Descriptor::fd
int fd() const
file descritptor
Definition
Descriptor.cpp:111
OksSystem::Descriptor::m_fd
int m_fd
Definition
Descriptor.hpp:61
OksSystem::Descriptor::flags
static int flags(bool read_mode, bool write_mode)
Definition
Descriptor.cpp:26
OksSystem::Descriptor::~Descriptor
~Descriptor()
Definition
Descriptor.cpp:45
OksSystem::Descriptor::open
void open(const File *file, int flags, mode_t perm)
internal open method
Definition
Descriptor.cpp:62
OksSystem::Descriptor::closeOnExec
void closeOnExec()
It flags the file descriptor to be closed after any call to the exec okssystem function.
Definition
Descriptor.cpp:119
OksSystem::Descriptor::close
void close()
close the descriptor
Definition
Descriptor.cpp:78
OksSystem::Descriptor::close_safe
void close_safe()
close the descriptor no exception
Definition
Descriptor.cpp:91
OksSystem::Descriptor::write
int write(const void *buffer, size_t number) const
Definition
Descriptor.cpp:105
OksSystem::Descriptor::m_name
std::string m_name
internal file descriptor
Definition
Descriptor.hpp:62
OksSystem::File
Wrapper for file operations.
Definition
File.hpp:37
ers.hpp
ers::warning
void warning(const Issue &issue)
Definition
ers.hpp:126
exceptions.hpp
Generated on
for DUNE-DAQ by
1.17.0