Skip to content

Commit

Permalink
Fixed an issue where cage directory could not be created in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaozhuai committed Dec 17, 2019
1 parent b22f094 commit d77b1fe
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 31 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This project was standing on the shoulders of [binaryage/asepsis](https://github

For discussion, go [https://github.com/JK3Y/asepsis/issues/25](https://github.com/JK3Y/asepsis/issues/25)

# How to use it
# How to use

## Install

Expand Down Expand Up @@ -60,6 +60,7 @@ Type `brew install cmake` to install `cmake`.
git clone https://github.com/xiaozhuai/odourless
cd odourless
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make install -j8
open dist
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Odourless是一个禁止MacOS系统生成.DS_Store的工具
git clone https://github.com/xiaozhuai/odourless
cd odourless
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make install -j8
open dist
Expand Down
4 changes: 4 additions & 0 deletions src/common/FileSystemHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ bool FileSystemHelper::exists(const std::string &path) {
bool FileSystemHelper::rename(const std::string &src, const std::string &dst) {
return ::rename(src.c_str(), dst.c_str()) == 0;
}

bool FileSystemHelper::chmod(const std::string &path, int mode) {
return ::chmod(path.c_str(), mode) == 0;
}
2 changes: 2 additions & 0 deletions src/common/FileSystemHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class FileSystemHelper {

static bool rename(const std::string &src, const std::string &dst);

static bool chmod(const std::string &path, int mode);

};


Expand Down
2 changes: 2 additions & 0 deletions src/common/OdourlessUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//#define ODOURLESS_INSTALL_PATH "/Users/xiaozhuai/work/odourless/cmake-build-debug/Odourless.app"
//#define ODOURLESS_INSTALL_PATH "/Applications/Odourless.app"

#define CAGE_DIRECTORY_PATH "/usr/local/var/.odourless_cage"

typedef struct {
bool suc;
int ret;
Expand Down
59 changes: 35 additions & 24 deletions src/daemon/odourless-daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Injector.h"
#include "FileSystemHelper.h"
#include "ProcessHelper.h"
#include "OdourlessUtils.h"
#include "Log.h"

#include <csignal>
Expand All @@ -22,43 +23,53 @@ void sigHandler(int sig) {
int main(int argc, char **argv) {
Log::init();

signal(SIGINT, sigHandler);
if (getuid() > 0) {
LOGE("please run me as root");
return 1;
}

if (!FileSystemHelper::ensureDir(CAGE_DIRECTORY_PATH, 0777, true)) {
LOGE("ensure cage directory \"%s\" failed!", CAGE_DIRECTORY_PATH);
return 2;
}

if (!FileSystemHelper::chmod(CAGE_DIRECTORY_PATH, 0777)) {
LOGE("chmod cage directory \"%s\" 0777 failed!", CAGE_DIRECTORY_PATH);
return 3;
}

const std::string finderProcessPath = "/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder";
const std::string executableDirectory = ProcessHelper::getCurrentExecutableDirectory();
const std::string bootstrapLib = executableDirectory + "/../lib/libbootstrap.dylib";
const std::string injectLib = executableDirectory + "/../lib/libodourless-inject.dylib";

if (getuid() > 0) {
LOGE("please run me as root");
return -1;
}


Injector inj(bootstrapLib);

finderPid = ProcessHelper::getPidByProcessPath(finderProcessPath);
if (!finderPid) {
LOGE("process %s not found", finderProcessPath.c_str());
return 0;
}
LOG("finder pid: %u", finderPid);

// sleep for a while
sleep(3);
inj.inject(finderPid, injectLib);
signal(SIGINT, sigHandler);

while (monitoring) {
sleep(3);
pid_t pid = ProcessHelper::getPidByProcessPath(finderProcessPath);
if (pid != finderPid) {
LOG("finder has restarted, pid: %u", finderPid);
finderPid = pid;
if (finderPid == 0) {
if (pid != 0) {
finderPid = pid;
LOG("finder pid: %u", finderPid);

sleep(3);
inj.inject(finderPid, injectLib);
} else {
LOG("finder not running");
}
} else {
if (pid != finderPid) {
finderPid = pid;
LOG("finder restarted, pid: %u", finderPid);

// sleep for a while
sleep(3);
inj.inject(finderPid, injectLib);
sleep(3);
inj.inject(finderPid, injectLib);
}
}

sleep(3);
}

Log::destroy();
Expand Down
1 change: 1 addition & 0 deletions src/inject-lib/DSStoreHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "DSStoreHelper.h"
#include "FileSystemHelper.h"
#include "OdourlessUtils.h"

bool DSStoreHelper::isDSStore(const std::string &path) {
return FileSystemHelper::getName(path) == ".DS_Store"
Expand Down
1 change: 0 additions & 1 deletion src/inject-lib/DSStoreHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <string>

#define CAGE_DIRECTORY_PATH "/usr/local/var/.odourless_cage"

class DSStoreHelper {
public:
Expand Down
5 changes: 0 additions & 5 deletions src/inject-lib/strategy/asepsis_strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ static int replacement_setattrlist(const char *path, void *list, void *buf, size
}

bool apply_asepsis_strategy() {
if (!FileSystemHelper::ensureDir(CAGE_DIRECTORY_PATH, 0755, true)) {
LOGE("ensure cage directory \"%s\" failed!", CAGE_DIRECTORY_PATH);
return false;
}

real_open = Hook::getRealFunc<open_t>("open");
real_openx_np = Hook::getRealFunc<openx_np_t>("openx_np");
real_getattrlist = Hook::getRealFunc<getattrlist_t>("getattrlist");
Expand Down

0 comments on commit d77b1fe

Please sign in to comment.