Skip to content

Commit

Permalink
scoped_handle: get_ref && get_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
mkdym committed Oct 22, 2015
1 parent 20a4f3f commit 00189f7
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 52 deletions.
4 changes: 2 additions & 2 deletions DaemonSvc/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool CDaemon::start()
void CDaemon::keep_running()
{
InfoLog("keep_running begin");
const DWORD r = WaitForSingleObject(m_exit_event.get(), INFINITE);
const DWORD r = WaitForSingleObject(m_exit_event.get_ref(), INFINITE);
switch (r)
{
case WAIT_OBJECT_0:
Expand All @@ -68,7 +68,7 @@ void CDaemon::stop()
//ºÍstart±£³ÖÒ»Ö£¬¶¼ÊÇremove_all
CTasksController::get_instance_ref().stop_all();
CTasksController::get_instance_ref().delete_all();
SetEvent(m_exit_event.get());
SetEvent(m_exit_event.get_ref());
InfoLog("stop end");
}

Expand Down
2 changes: 1 addition & 1 deletion DaemonSvc/logger_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ bool CLoggerImpl::write(const LOG_LEVEL level, const std::string& s)
}

DWORD written_bytes = 0;
if (!WriteFile(m_log_file_handle.get(), s.c_str(), s.size(), &written_bytes, NULL))
if (!WriteFile(m_log_file_handle.get_ref(), s.c_str(), s.size(), &written_bytes, NULL))
{
print_last_err("WriteFile fail");
break;
Expand Down
10 changes: 5 additions & 5 deletions DaemonSvc/proc_non_exist_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void CProcNonExistTask::stop()
{
assert(m_exit_event.valid());

SetEvent(m_exit_event.get());
SetEvent(m_exit_event.get_ref());
if (m_worker_thread.joinable())
{
m_worker_thread.join();
Expand Down Expand Up @@ -107,7 +107,7 @@ void CProcNonExistTask::worker_func()
}

//sleep some while if function has done something which will effect later on
const DWORD wait_result = WaitForSingleObject(m_exit_event.get(), 1000);
const DWORD wait_result = WaitForSingleObject(m_exit_event.get_ref(), 1000);
if (WAIT_OBJECT_0 == wait_result)
{
InfoLog("got exit notify");
Expand All @@ -122,7 +122,7 @@ void CProcNonExistTask::worker_func()
{
ErrorLogLastErr("OpenProcess[%lu] fail", pid);

const DWORD wait_result = WaitForSingleObject(m_exit_event.get(), m_interval_seconds * 1000);
const DWORD wait_result = WaitForSingleObject(m_exit_event.get_ref(), m_interval_seconds * 1000);
if (WAIT_OBJECT_0 == wait_result)
{
InfoLog("got exit notify");
Expand All @@ -133,7 +133,7 @@ void CProcNonExistTask::worker_func()
{
bool should_break = false;

HANDLE pHandles[2] = {m_exit_event.get(), hProcess.get()};
HANDLE pHandles[2] = {m_exit_event.get_ref(), hProcess.get_ref()};
const DWORD wait_result = WaitForMultipleObjects(sizeof(pHandles) / sizeof(pHandles[0]),
pHandles, FALSE, INFINITE);
switch (wait_result)
Expand Down Expand Up @@ -161,7 +161,7 @@ void CProcNonExistTask::worker_func()
default:
ErrorLogLastErr("WaitForMultipleObjects fail, return code: %lu", wait_result);
//sleep some while for recover from error state
if (WAIT_OBJECT_0 == WaitForSingleObject(m_exit_event.get(), 1000))
if (WAIT_OBJECT_0 == WaitForSingleObject(m_exit_event.get_ref(), 1000))
{
InfoLog("got exit notify");
should_break = true;
Expand Down
34 changes: 13 additions & 21 deletions DaemonSvc/process_creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,23 @@ HANDLE ProcessCreator::create_process_as_same_token(HANDLE hSourceProcess,
do
{
scoped_handle<> hSourceToken;
//if (!OpenProcessToken(hSourceProcess, TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY, hSourceToken.get_ptr()))
if (!OpenProcessToken(hSourceProcess, TOKEN_DUPLICATE, hSourceToken.get_ptr()))
{
HANDLE hSourceToken_ = NULL;
//if (!OpenProcessToken(hSourceProcess, TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY, &hToken))
if (!OpenProcessToken(hSourceProcess, TOKEN_DUPLICATE, &hSourceToken_))
{
ErrorLogLastErr("OpenProcessToken fail");
break;
}
hSourceToken.reset(hSourceToken_);
ErrorLogLastErr("OpenProcessToken fail");
break;
}

scoped_handle<> hTargetToken;
if (!DuplicateTokenEx(hSourceToken.get_ref(),
MAXIMUM_ALLOWED,
NULL,
SecurityDelegation,
TokenPrimary,
hTargetToken.get_ptr()))
{
HANDLE hTargetToken_ = NULL;
if (!DuplicateTokenEx(hSourceToken.get(),
MAXIMUM_ALLOWED,
NULL,
SecurityDelegation,
TokenPrimary,
&hTargetToken_))
{
ErrorLogLastErr("DuplicateTokenEx fail");
break;
}
hTargetToken.reset(hTargetToken_);
ErrorLogLastErr("DuplicateTokenEx fail");
break;
}

STARTUPINFO si = {0};
Expand All @@ -56,7 +48,7 @@ HANDLE ProcessCreator::create_process_as_same_token(HANDLE hSourceProcess,
memcpy_s(cmd_array.get(), command.size() * sizeof(tchar),
command.c_str(), command.size() * sizeof(tchar));

if (!CreateProcessAsUser(hTargetToken.get(),
if (!CreateProcessAsUser(hTargetToken.get_ref(),
NULL,
cmd_array.get(),
NULL,
Expand Down
2 changes: 1 addition & 1 deletion DaemonSvc/process_path_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ tstring CProcessPathQuery::query(const DWORD pid, bool& native_name)
}
else
{
s = query(hProcess.get(), native_name);
s = query(hProcess.get_ref(), native_name);
}

return s;
Expand Down
4 changes: 2 additions & 2 deletions DaemonSvc/process_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool CProcessScanner::next(ProcessInfo& info)
pe32.dwSize = sizeof(pe32);
if (m_first_enum)
{
if (!Process32First(m_hSnapshot.get(), &pe32))
if (!Process32First(m_hSnapshot.get_ref(), &pe32))
{
ErrorLogLastErr("Process32First fail");
break;
Expand All @@ -47,7 +47,7 @@ bool CProcessScanner::next(ProcessInfo& info)
}
else
{
if (!Process32Next(m_hSnapshot.get(), &pe32))
if (!Process32Next(m_hSnapshot.get_ref(), &pe32))
{
CLastErrorFormat e;
if (e.code() != ERROR_NO_MORE_FILES)
Expand Down
8 changes: 4 additions & 4 deletions DaemonSvc/scoped_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class scoped_handle : public boost::noncopyable

//HANDLE is a ptr, so do not need HANDLE* or HANDLE&
//just assign by value
scoped_handle(HANDLE h)
scoped_handle(const HANDLE &h)
{
h_ = h;
}
Expand All @@ -34,7 +34,7 @@ class scoped_handle : public boost::noncopyable
}

//you should ensure not self-assignment
void reset(HANDLE h)
void reset(const HANDLE &h)
{
destory();
h_ = h;
Expand All @@ -59,12 +59,12 @@ class scoped_handle : public boost::noncopyable
return h_ != invalid_value;
}

HANDLE get() const
HANDLE& get_ref()
{
return h_;
}

HANDLE *get_ptr() const
HANDLE* get_ptr()
{
return &h_;
}
Expand Down
6 changes: 3 additions & 3 deletions DaemonSvc/time_interval_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void CTimeIntervalTask::stop()
{
assert(m_exit_event.valid());

SetEvent(m_exit_event.get());
SetEvent(m_exit_event.get_ref());
if (m_worker_thread.joinable())
{
m_worker_thread.join();
Expand All @@ -77,7 +77,7 @@ void CTimeIntervalTask::worker_func()

while (true)
{
const DWORD wait_result = WaitForSingleObject(m_exit_event.get(), m_interval_seconds * 1000);
const DWORD wait_result = WaitForSingleObject(m_exit_event.get_ref(), m_interval_seconds * 1000);
if (WAIT_OBJECT_0 == wait_result)
{
InfoLog("got exit notify");
Expand All @@ -102,7 +102,7 @@ void CTimeIntervalTask::worker_func()
{
ErrorLogLastErr("WaitForSingleObject fail, return code: %lu", wait_result);
//sleep some while for recover from error state
if (WAIT_OBJECT_0 == WaitForSingleObject(m_exit_event.get(), 1000))
if (WAIT_OBJECT_0 == WaitForSingleObject(m_exit_event.get_ref(), 1000))
{
InfoLog("got exit notify");
break;
Expand Down
4 changes: 2 additions & 2 deletions DaemonSvc/time_point_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void CTimePointTask::stop()
{
assert(m_exit_event.valid());

SetEvent(m_exit_event.get());
SetEvent(m_exit_event.get_ref());
if (m_worker_thread.joinable())
{
m_worker_thread.join();
Expand Down Expand Up @@ -92,7 +92,7 @@ void CTimePointTask::worker_func()
//等待时间:等待误差时间的一半,若这一半大于10分钟,则以10分钟计

//todo
const DWORD wait_result = WaitForSingleObject(m_exit_event.get(), INFINITE);
const DWORD wait_result = WaitForSingleObject(m_exit_event.get_ref(), INFINITE);
if (WAIT_OBJECT_0 == wait_result)
{
InfoLog("got exit notify");
Expand Down
12 changes: 4 additions & 8 deletions DaemonSvc/windows_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ bool WindowsUtil::set_privilege(const tstring& privilege_name, const bool enable
do
{
scoped_handle<> hToken;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken.get_ptr()))
{
HANDLE hToken_ = NULL;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken_))
{
ErrorLogLastErr("OpenProcessToken fail");
break;
}
hToken.reset(hToken_);
ErrorLogLastErr("OpenProcessToken fail");
break;
}

LUID luid = {0};
Expand All @@ -75,7 +71,7 @@ bool WindowsUtil::set_privilege(const tstring& privilege_name, const bool enable
}

// Enable the privilege or disable all privileges.
BOOL adjust_success = AdjustTokenPrivileges(hToken.get(), FALSE, &tp,
BOOL adjust_success = AdjustTokenPrivileges(hToken.get_ref(), FALSE, &tp,
sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL);
CLastErrorFormat e;
if (!adjust_success)
Expand Down
6 changes: 3 additions & 3 deletions DaemonSvc/xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ xml_doc_ptr xml::load_xml_file(const std::string& file_path)
}

LARGE_INTEGER li = {0};
if (!GetFileSizeEx(h.get(), &li))
if (!GetFileSizeEx(h.get_ref(), &li))
{
ErrorLogLastErr("GetFileSizeEx fail, file path: %s", file_path.c_str());
break;
Expand All @@ -119,7 +119,7 @@ xml_doc_ptr xml::load_xml_file(const std::string& file_path)
//do not need zero memory, because read file return count
boost::scoped_array<char> data(new char[li.LowPart]);
DWORD bytes_read = 0;
if (!ReadFile(h.get(), data.get(), li.LowPart, &bytes_read, NULL))
if (!ReadFile(h.get_ref(), data.get(), li.LowPart, &bytes_read, NULL))
{
ErrorLogLastErr("ReadFile fail, file path: %s", file_path.c_str());
break;
Expand Down Expand Up @@ -159,7 +159,7 @@ bool xml::save_xml_to_file(const xml_doc_ptr pdoc, const std::string& file_path)

std::string s = get_xml_string(pdoc);
DWORD written_bytes = 0;
if (!WriteFile(h.get(), s.c_str(), s.size(), &written_bytes, NULL))
if (!WriteFile(h.get_ref(), s.c_str(), s.size(), &written_bytes, NULL))
{
ErrorLogLastErr("WriteFile fail, file path: %s", file_path.c_str());
break;
Expand Down

0 comments on commit 00189f7

Please sign in to comment.