Skip to content

Commit

Permalink
AStyle source code reformat
Browse files Browse the repository at this point in the history
 - defined coding style via AStyle configuration in src/astylerc
   (requires astyle version 3+)
 - added convenience scripts for reformatting the sources
   (scripts/reformat-sources.sh)
 - reformated sources
 - added code-style.sh check to the test suite

Resolves: #185
  • Loading branch information
Daniel Kopeček committed Oct 27, 2017
1 parent f440f8e commit 6b7863a
Show file tree
Hide file tree
Showing 160 changed files with 4,686 additions and 4,399 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ before_install:
- sudo apt-get install -y aspell aspell-en
- sudo apt-get install -y libprotobuf-dev protobuf-compiler
- sudo apt-get install -y valgrind
- sudo apt-get install -y realpath
- gem install coveralls-lcov
- curl -JLo /tmp/astyle.tgz --remote-header-name 'https://netix.dl.sourceforge.net/project/astyle/astyle/astyle%203.0.1/astyle_3.0.1_linux.tar.gz'
install:
- pushd .
- cd /tmp
- tar zxvf astyle.tgz
- cd astyle/build/gcc
- make && sudo make install
- popd
before_script:
- ./autogen.sh
- mkdir build && cd build
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Added AuditBackend usbguard-daemon configuration option for selecting audit log backend.
- Linux Audit support via new LinuxAudit backend.
- Added missing RuleCondition.hpp header file to the public API headers.
- Code Style specification via AStyle configuration file.

### Removed
- Removed Utility.hpp header file from public API headers
Expand All @@ -27,6 +28,7 @@
- usbguard-daemon now opens it's configuration in read-only mode
- Fixed UEventDeviceManager to work with Linux Kernel >= 4.13
- Refactored audit logging to support different audit log backends
- Reformatted source code to conform to the code style.

## 0.7.0 - 2017-04-12
### Added
Expand Down
3 changes: 2 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ EXTRA_DIST =\
usbguard-daemon.conf.in \
usbguard.service.in \
VERSION \
CHANGELOG.md
CHANGELOG.md \
src/astylerc

BUILT_SOURCES=\
src/build-config.h
Expand Down
23 changes: 23 additions & 0 deletions scripts/astyle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
#
# Copyright (C) 2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Daniel Kopecek <dkopecek@redhat.com>
#
PROJECT_ROOT="$(dirname "$0")/../"
ASTYLERC_PATH="${PROJECT_ROOT}/src/astylerc"

exec astyle $(< "${ASTYLERC_PATH}") $@
28 changes: 28 additions & 0 deletions scripts/reformat-sources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
#
# Copyright (C) 2017 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors: Daniel Kopecek <dkopecek@redhat.com>
#
set -ex

PROJECT_ROOT="$(dirname "$0")/../"
ASTYLE="${PROJECT_ROOT}/scripts/astyle.sh"

find "${PROJECT_ROOT}/src" \
-type f -not -path '*ThirdParty/*' \
\( -name '*.cpp' -or -name '*.hpp' -or -name '*.c' -or -name '*.h' \) \
-exec "${ASTYLE}" --preserve-date --suffix=none --lineend=linux --formatted "{}" \;
60 changes: 28 additions & 32 deletions src/CLI/IPCSignalWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Authors: Daniel Kopecek <dkopecek@redhat.com>
//
#ifdef HAVE_BUILD_CONFIG_H
#include <build-config.h>
#include <build-config.h>
#endif

#include "IPCSignalWatcher.hpp"
Expand Down Expand Up @@ -45,6 +45,7 @@ namespace usbguard
if (hasOpenExecutable()) {
closeExecutable();
}

openExecutable(path);
}

Expand All @@ -53,60 +54,59 @@ namespace usbguard
std::cout << "[IPC] Connected" << std::endl;

if (hasOpenExecutable()) {
const std::map<std::string,std::string> env = {
const std::map<std::string, std::string> env = {
{ "USBGUARD_IPC_SIGNAL", "IPC.Connected" }
};

runExecutable(env);
}
}

void IPCSignalWatcher::IPCDisconnected(bool exception_initiated, const IPCException& exception)
{
std::cout << "[IPC] Disconnected: exception_initiated=" << exception_initiated;

if (exception_initiated) {
std::cout << " message=" << exception.message();
}

std::cout << std::endl;

if (hasOpenExecutable()) {
const std::map<std::string,std::string> env = {
const std::map<std::string, std::string> env = {
{ "USBGUARD_IPC_SIGNAL", "IPC.Disconnected" },
{ "USBGUARD_MESSAGE", exception_initiated ? exception.message() : "" }
};

runExecutable(env);
}
}

void IPCSignalWatcher::DevicePresenceChanged(uint32_t id,
DeviceManager::EventType event,
Rule::Target target,
const std::string& device_rule)
DeviceManager::EventType event,
Rule::Target target,
const std::string& device_rule)
{
std::cout << "[device] PresenceChanged: id=" << id << std::endl;
std::cout << " event=" << DeviceManager::eventTypeToString(event) << std::endl;
std::cout << " target=" << Rule::targetToString(target) << std::endl;
std::cout << " device_rule=" << device_rule << std::endl;

if (hasOpenExecutable()) {
const std::map<std::string,std::string> env = {
const std::map<std::string, std::string> env = {
{ "USBGUARD_IPC_SIGNAL", "Device.PresenceChanged" },
{ "USBGUARD_DEVICE_ID", std::to_string(id) },
{ "USBGUARD_DEVICE_EVENT", DeviceManager::eventTypeToString(event) },
{ "USBGUARD_DEVICE_TARGET", Rule::targetToString(target) },
{ "USBGUARD_DEVICE_RULE", device_rule }
};

runExecutable(env);
}
}

void IPCSignalWatcher::DevicePolicyChanged(uint32_t id,
Rule::Target target_old,
Rule::Target target_new,
const std::string& device_rule,
uint32_t rule_id)
Rule::Target target_old,
Rule::Target target_new,
const std::string& device_rule,
uint32_t rule_id)
{
std::cout << "[device] PolicyChanged: id=" << id << std::endl;
std::cout << " target_old=" << Rule::targetToString(target_old) << std::endl;
Expand All @@ -115,15 +115,14 @@ namespace usbguard
std::cout << " rule_id=" << rule_id << std::endl;

if (hasOpenExecutable()) {
const std::map<std::string,std::string> env = {
const std::map<std::string, std::string> env = {
{ "USBGUARD_IPC_SIGNAL", "Device.PolicyChanged" },
{ "USBGUARD_DEVICE_ID", std::to_string(id) },
{ "USBGUARD_DEVICE_TARGET_OLD", Rule::targetToString(target_old) },
{ "USBGUARD_DEVICE_TARGET_NEW", Rule::targetToString(target_new) },
{ "USBGUARD_DEVICE_RULE", device_rule },
{ "USBGUARD_DEVICE_RULE_ID", std::to_string(rule_id) }
};

runExecutable(env);
}
}
Expand Down Expand Up @@ -163,7 +162,7 @@ namespace usbguard
return (_exec_path_fd >= 0);
}

void IPCSignalWatcher::runExecutable(const std::map<std::string,std::string>& environment)
void IPCSignalWatcher::runExecutable(const std::map<std::string, std::string>& environment)
{
/*
* Fork child process, that will fork again and exec.
Expand Down Expand Up @@ -216,21 +215,20 @@ namespace usbguard
if (pid_fork2 == (::pid_t)-1) {
::_exit(errno);
}

if (pid_fork2 > 0) {
::_exit(0);
}

char * const exec_argv[] = { &_exec_path[0], nullptr };
char ** const exec_envp = createExecutableEnvironment(environment);

char* const exec_argv[] = { &_exec_path[0], nullptr };
char** const exec_envp = createExecutableEnvironment(environment);
(void)::fexecve(_exec_path_fd, exec_argv, exec_envp);
const int saved_errno = errno;

destroyExecutableEnvironment(exec_envp);
_exit(saved_errno);
}

char ** IPCSignalWatcher::createExecutableEnvironment(const std::map<std::string,std::string>& environment)
char** IPCSignalWatcher::createExecutableEnvironment(const std::map<std::string, std::string>& environment)
{
size_t environ_size = 0;

Expand All @@ -240,7 +238,7 @@ namespace usbguard
}
}

char ** envp = new char* [environ_size + environment.size() + 1];
char** envp = new char* [environ_size + environment.size() + 1];
size_t i = 0;

for (; i < environ_size; ++i) {
Expand All @@ -249,46 +247,44 @@ namespace usbguard

for (const auto& map_entry : environment) {
std::string environ_value;

environ_value.append(map_entry.first);
environ_value.append("=");
environ_value.append(map_entry.second);

envp[i++] = cstrCopy(environ_value.c_str());
}

envp[i] = nullptr;

return envp;
}

void IPCSignalWatcher::destroyExecutableEnvironment(char ** const envp)
void IPCSignalWatcher::destroyExecutableEnvironment(char** const envp)
{
if (envp != nullptr) {
char ** envp_current = envp;
char** envp_current = envp;

while (envp_current[0] != nullptr) {
delete [] envp_current[0];
++envp_current;
}

delete [] envp;
}
}

char *IPCSignalWatcher::cstrCopy(const char *c_str)
char* IPCSignalWatcher::cstrCopy(const char* c_str)
{
if (c_str == nullptr) {
return nullptr;
}

const auto c_str_len = ::strlen(c_str);
char *c_str_copy = new char [c_str_len + 1];
char* c_str_copy = new char [c_str_len + 1];
/*
* new throws std::bad_alloc on failure, no need
* to check the pointer.
*/
::memcpy(c_str_copy, c_str, c_str_len);
c_str_copy[c_str_len] = '\0';

return c_str_copy;
}
} /* namespace usbguard */
Expand Down
24 changes: 12 additions & 12 deletions src/CLI/IPCSignalWatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//
#pragma once
#ifdef HAVE_BUILD_CONFIG_H
#include <build-config.h>
#include <build-config.h>
#endif

#include "usbguard/IPCClient.hpp"
Expand All @@ -36,24 +36,24 @@ namespace usbguard
void IPCDisconnected(bool exception_initiated, const IPCException& exception) override;

void DevicePresenceChanged(uint32_t id,
DeviceManager::EventType event,
Rule::Target target,
const std::string& device_rule) override;
DeviceManager::EventType event,
Rule::Target target,
const std::string& device_rule) override;

void DevicePolicyChanged(uint32_t id,
Rule::Target target_old,
Rule::Target target_new,
const std::string& device_rule,
uint32_t rule_id) override;
Rule::Target target_old,
Rule::Target target_new,
const std::string& device_rule,
uint32_t rule_id) override;
private:
void openExecutable(const std::string& path);
void closeExecutable();
bool hasOpenExecutable() const;

void runExecutable(const std::map<std::string,std::string>& environment);
static char ** createExecutableEnvironment(const std::map<std::string,std::string>& environment);
static void destroyExecutableEnvironment(char ** const envp);
static char *cstrCopy(const char *c_str);
void runExecutable(const std::map<std::string, std::string>& environment);
static char** createExecutableEnvironment(const std::map<std::string, std::string>& environment);
static void destroyExecutableEnvironment(char** const envp);
static char* cstrCopy(const char* c_str);

std::string _exec_path;
int _exec_path_fd {-1};
Expand Down
5 changes: 3 additions & 2 deletions src/CLI/PolicyGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Authors: Daniel Kopecek <dkopecek@redhat.com>
//
#ifdef HAVE_BUILD_CONFIG_H
#include <build-config.h>
#include <build-config.h>
#endif

#include "PolicyGenerator.hpp"
Expand All @@ -26,7 +26,7 @@ namespace usbguard
{

PolicyGenerator::PolicyGenerator()
: _ruleset(nullptr)
: _ruleset(nullptr)
{
_with_hash = true;
_hash_only = false;
Expand Down Expand Up @@ -91,6 +91,7 @@ namespace usbguard
}

bool port_specific = _port_specific;

/*
* If the the global "port specific" flag isn't
* set, check the "no iSerial port specific" flag
Expand Down
2 changes: 1 addition & 1 deletion src/CLI/PolicyGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// Authors: Daniel Kopecek <dkopecek@redhat.com>
//
#ifdef HAVE_BUILD_CONFIG_H
#include <build-config.h>
#include <build-config.h>
#endif

#include "usbguard/Rule.hpp"
Expand Down

0 comments on commit 6b7863a

Please sign in to comment.