Compare commits

...

5 Commits

Author SHA1 Message Date
chn
e3625c78d1 add some modification 2024-07-26 23:41:12 +08:00
chn
9e20fd1d42 some fix 2024-07-26 19:34:08 +08:00
chn
9531f60b05 archive 2024-07-26 17:30:51 +08:00
chn
e0e9ae9ad3 try some 2024-07-26 15:35:36 +08:00
chn
599776b33d 暂存 2024-07-25 21:43:14 +08:00
17 changed files with 290 additions and 147 deletions

View File

@@ -77,26 +77,16 @@
(builtins.attrNames (builtins.readDir ./devices));
in
{
packages = rec
packages =
{
x86_64-linux = rec
{
pkgs = (import inputs.nixpkgs
pkgs = import inputs.nixpkgs
{
system = "x86_64-linux";
config.allowUnfree = true;
overlays = [ inputs.self.overlays.default ];
crossOverlays = [(final: prev:
{
boost = prev.boost.override { zstd = null; };
magic-enum = prev.magic-enum.overrideAttrs (prev: { cmakeFlags = prev.cmakeFlags ++
[ "-DMAGIC_ENUM_OPT_BUILD_EXAMPLES=OFF" "-DMAGIC_ENUM_OPT_BUILD_TESTS=OFF" ]; });
range-v3 = prev.range-v3.overrideAttrs (prev: { cmakeFlags = prev.cmakeFlags ++
[ "-DRANGE_V3_DOCS=OFF" "-DRANGE_V3_TESTS=OFF" "-DRANGE_V3_EXAMPLES=OFF" ]; });
abseil-cpp = prev.abseil-cpp.overrideAttrs (prev: { buildInputs = prev.buildInputs ++
[ final.windows.pthreads ]; });
})];
});
};
default = inputs.nixpkgs.legacyPackages.x86_64-linux.writeText "systems"
(builtins.concatStringsSep "\n" (builtins.map
(system: builtins.toString inputs.self.outputs.nixosConfigurations.${system}.config.system.build.toplevel)
@@ -110,6 +100,7 @@
{ inherit openssh duc; standalone = true; version = inputs.self.rev or "dirty"; };
ufo = pkgs.pkgsStatic.localPackages.ufo.override { version = inputs.self.rev or "dirty"; };
chn-bsub = pkgs.pkgsStatic.localPackages.chn-bsub;
winjob = pkgs.pkgsCross.mingwW64Static.localPackages.winjob;
}
// (
builtins.listToAttrs (builtins.map
@@ -119,12 +110,30 @@
value = inputs.self.outputs.nixosConfigurations.${system}.config.system.build.toplevel;
})
devices)
);
x86_64-w64-mingw32 = rec
{
pkgs = x86_64-linux.pkgs.pkgsCross.mingwW64Static;
winjob = pkgs.localPackages.winjob;
};
);
x86_64-mingwW64 =
let originalPkgs = import inputs.nixpkgs
{
system = "x86_64-linux";
config.allowUnfree = true;
overlays = [ inputs.self.overlays.default ];
crossOverlays = [(final: prev:
{
boost = (prev.boost.override { zstd = null; }).overrideAttrs (prev:
{ patches = prev.patches or [] ++ [ ./local/pkgs/winjob/boost.patch ]; });
magic-enum = prev.magic-enum.overrideAttrs (prev: { cmakeFlags = prev.cmakeFlags ++
[ "-DMAGIC_ENUM_OPT_BUILD_EXAMPLES=OFF" "-DMAGIC_ENUM_OPT_BUILD_TESTS=OFF" ]; });
range-v3 = prev.range-v3.overrideAttrs (prev: { cmakeFlags = prev.cmakeFlags ++
[ "-DRANGE_V3_DOCS=OFF" "-DRANGE_V3_TESTS=OFF" "-DRANGE_V3_EXAMPLES=OFF" ]; });
abseil-cpp = prev.abseil-cpp.overrideAttrs (prev: { buildInputs = prev.buildInputs ++
[ final.windows.pthreads ]; });
})];
};
in rec
{
pkgs = originalPkgs.pkgsCross.mingwW64Static;
winjob = pkgs.localPackages.winjob;
};
};
nixosConfigurations =
(
@@ -198,14 +207,12 @@
packages = [ pkgs.clang-tools_18 ];
CMAKE_EXPORT_COMPILE_COMMANDS = "1";
};
winjob =
let inherit (pkgs) clang-tools_18; in let inherit (inputs.self.packages.x86_64-w64-mingw32) pkgs winjob;
in pkgs.mkShell.override { stdenv = pkgs.gcc14Stdenv; }
{
inputsFrom = [ winjob ];
packages = [ clang-tools_18 ];
CMAKE_EXPORT_COMPILE_COMMANDS = "1";
};
winjob = pkgs.mkShell
{
inputsFrom = [ pkgs.localPackages.winjob ];
packages = with pkgs; [ clang-tools_18 qt6.full qtcreator ];
CMAKE_EXPORT_COMPILE_COMMANDS = "1";
};
};
};
}

View File

@@ -70,7 +70,7 @@ inputs: rec
ufo = inputs.pkgs.callPackage ./ufo
{ inherit concurrencpp biu glad matplotplusplus zpp-bits; tbb = inputs.pkgs.tbb_2021_11; };
chn-bsub = inputs.pkgs.callPackage ./chn-bsub { inherit biu; };
winjob = inputs.pkgs.callPackage ./winjob { stdenv = inputs.pkgs.gcc14Stdenv; };
winjob = inputs.pkgs.callPackage ./winjob {};
sockpp = inputs.pkgs.callPackage ./sockpp.nix { src = inputs.topInputs.sockpp; };
git-lfs-transfer = inputs.pkgs.callPackage ./git-lfs-transfer.nix { src = inputs.topInputs.git-lfs-transfer; };

View File

@@ -1,3 +1,3 @@
CompileFlags:
Add: [ -Wall, -Wextra, -std=c++26 ]
Add: [ -Wall, -Wextra, -std=c++23 ]
Compiler: g++

View File

@@ -13,19 +13,25 @@ endif()
set(WINJOB_VERSION "unknown" CACHE STRING "Version of the winjob")
find_package(Boost REQUIRED COMPONENTS headers filesystem)
find_package(nlohmann_json REQUIRED)
add_executable(winjob src/winjob.cpp src/windows.cpp)
# target_compile_features(winjob PRIVATE cxx_std_26)
target_compile_options(winjob PRIVATE "-std=c++26")
add_executable(winjob src/winjob.cpp src/os.cpp)
target_compile_features(winjob PRIVATE cxx_std_23)
target_include_directories(winjob PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(winjob PRIVATE Boost::headers Boost::filesystem ws2_32 wsock32)
target_link_libraries(winjob PRIVATE Boost::headers Boost::filesystem)
if(WIN32)
target_link_libraries(winjob PRIVATE ws2_32 wsock32)
endif()
target_compile_definitions(winjob PRIVATE winjob_VERSION="${winjob_VERSION}")
add_executable(winjobd src/winjobd.cpp)
# target_compile_features(winjob PRIVATE cxx_std_26)
target_compile_options(winjobd PRIVATE "-std=c++26")
add_executable(winjobd src/winjobd.cpp src/os.cpp src/scheduler.cpp)
target_compile_features(winjob PRIVATE cxx_std_23)
target_compile_options(winjobd PRIVATE -fpermissive)
target_include_directories(winjobd PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(winjobd PRIVATE Boost::headers Boost::filesystem ws2_32 wsock32)
target_link_libraries(winjobd PRIVATE Boost::headers Boost::filesystem)
if(WIN32)
target_link_libraries(winjobd PRIVATE ws2_32 wsock32)
endif()
target_compile_definitions(winjobd PRIVATE winjob_VERSION="${winjob_VERSION}")
install(TARGETS winjob RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@@ -0,0 +1,12 @@
diff --color -ur a/boost/process/v2/detail/impl/utf8.ipp b/boost/process/v2/detail/impl/utf8.ipp
--- a/boost/process/v2/detail/impl/utf8.ipp 2024-07-26 15:50:15.844632780 +0800
+++ b/boost/process/v2/detail/impl/utf8.ipp 2024-07-26 15:50:09.036567016 +0800
@@ -11,7 +11,7 @@
#include <boost/process/v2/error.hpp>
#if defined(BOOST_PROCESS_V2_WINDOWS)
-#include <Windows.h>
+#include <windows.h>
#endif
BOOST_PROCESS_V2_BEGIN_NAMESPACE

View File

@@ -0,0 +1,3 @@
{
"cpu": 8
}

View File

@@ -1,11 +1,11 @@
{
stdenv, cmake, pkg-config, version ? null, lib,
boost
boost, nlohmann_json
}: stdenv.mkDerivation
{
name = "winjob";
src = ./.;
buildInputs = [ boost ];
buildInputs = [ boost nlohmann_json ];
nativeBuildInputs = [ cmake pkg-config ];
cmakeFlags = lib.optionals (version != null) [ "-DWINJOB_VERSION=${version}" ];
}

View File

@@ -0,0 +1,17 @@
# pragma once
# include <string>
# include <vector>
# include <chrono>
namespace winjob
{
enum class JobStatus { Pending, Starting, Running, Done, Exit, Canceled, Lost };
struct Job
{
unsigned Id, Cpu;
std::string User, Program;
std::vector<std::string> Args;
JobStatus Status;
std::chrono::steady_clock::time_point LastReported, Started, Finished;
};
}

View File

@@ -0,0 +1,27 @@
# pragma once
# include <winjob/job.hpp>
# include <boost/asio.hpp>
# include <map>
# include <thread>
namespace winjob
{
class Scheduler
{
public: Scheduler(unsigned cpu);
public: ~Scheduler();
public: std::vector<unsigned> submit(std::vector<Job> jobs);
public: void cancel(std::vector<unsigned> jobs);
public: std::vector<bool> run(std::vector<unsigned> jobs);
public: std::vector<bool> refresh(std::vector<unsigned> jobs);
public: std::vector<Job> status();
protected: unsigned MaxCpu_, UsedCpu_ = 0;
protected: std::multimap<JobStatus, Job> Jobs_;
protected: boost::asio::io_context Context_;
protected: boost::asio::steady_timer Timer_;
protected: bool Destructing_ = false;
protected: std::jthread Executor_;
protected: unsigned NextId_ = 0;
};
}

View File

@@ -0,0 +1,13 @@
# pragma once
# include <string>
# include <functional>
# include <optional>
namespace winjob
{
class Server
{
public: Server(unsigned port, std::function<std::optional<std::string>(std::string)>);
public: ~Server();
};
}

View File

@@ -1,8 +0,0 @@
# include <optional>
# include <string>
# include <utility>
namespace winjob
{
std::optional<std::pair<std::string, std::string>> get_owner(const std::string& file_name);
}

View File

@@ -0,0 +1,13 @@
REM run as admin
set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/c cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )
REM copy files
if not exist "C:\Program Files\winjob" mkdir "C:\Program Files\winjob"
copy winjob.exe "C:\Program Files\winjob\winjob.exe"
copy winjobd.exe "C:\Program Files\winjob\winjobd.exe"
REM create task scheduler
schtasks /create /tn "winjob" /tr "C:\Program Files\winjob\winjobd.exe" /sc onstart /ru system /f
pause

View File

@@ -0,0 +1,58 @@
# include <winjob/scheduler.hpp>
# include <set>
namespace winjob
{
using namespace std::literals;
Scheduler::Scheduler(unsigned cpu)
: MaxCpu_(cpu), Timer_(Context_, 1s), Executor_([&] { Context_.run(); })
{
Timer_.async_wait([&, this](boost::system::error_code)
{
// do not run again if destructing
if (Destructing_) return;
// check if any job is outdated
std::vector<Job> outdated;
auto now = std::chrono::steady_clock::now();
for (auto it = Jobs_.begin(); it != Jobs_.end();)
if
(
std::set{JobStatus::Pending, JobStatus::Starting, JobStatus::Running}.contains(it->first)
&& now - it->second.LastReported > std::chrono::seconds(30)
)
{
if (std::set{JobStatus::Starting, JobStatus::Running}.contains(it->first))
UsedCpu_ -= it->second.Cpu;
outdated.push_back(it->second);
outdated.back().Status = JobStatus::Lost;
it = Jobs_.erase(it);
}
else ++it;
for (auto& job : outdated) Jobs_.insert({JobStatus::Lost, job});
// schedule next check
Timer_.expires_at(Timer_.expiry() + 1s);
});
}
Scheduler::~Scheduler() { Destructing_ = true; Executor_.join(); }
std::vector<unsigned> Scheduler::submit(std::vector<Job> jobs)
{
std::vector<unsigned> ids;
boost::asio::dispatch(Context_, [&, this]
{
for (auto& job : jobs)
{
job.Id = NextId_++;
job.Status = JobStatus::Pending;
job.LastReported = std::chrono::steady_clock::now();
Jobs_.insert({JobStatus::Pending, job});
ids.push_back(job.Id);
}
});
return ids;
}
void Scheduler::cancel(unsigned id) {}
bool Scheduler::run(unsigned id) {}
std::vector<Job> Scheduler::status() {}
}

View File

View File

@@ -1,58 +0,0 @@
# include <winjob/windows.hpp>
# include <windows.h>
# include <tchar.h>
# include <accctrl.h>
# include <aclapi.h>
namespace winjob
{
std::optional<std::pair<std::string, std::string>> get_owner(const std::string& file_name)
{
DWORD dwRtnCode = 0;
PSID pSidOwner = NULL;
BOOL bRtnBool = TRUE;
LPTSTR AcctName = NULL;
LPTSTR DomainName = NULL;
DWORD dwAcctName = 1, dwDomainName = 1;
SID_NAME_USE eUse = SidTypeUnknown;
HANDLE hFile;
PSECURITY_DESCRIPTOR pSD = NULL;
// Get the handle of the file object.
hFile = CreateFile
(file_name.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Check GetLastError for CreateFile error code.
if (hFile == INVALID_HANDLE_VALUE) return {};
// Get the owner SID of the file.
dwRtnCode = GetSecurityInfo(hFile, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pSidOwner, NULL, NULL, NULL, &pSD);
// Check GetLastError for GetSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) return {};
// First call to LookupAccountSid to get the buffer sizes.
bRtnBool = LookupAccountSid
(NULL, pSidOwner, AcctName, (LPDWORD)&dwAcctName, DomainName, (LPDWORD)&dwDomainName, &eUse);
// Reallocate memory for the buffers.
AcctName = (LPTSTR)GlobalAlloc(GMEM_FIXED, dwAcctName * sizeof(wchar_t));
// Check GetLastError for GlobalAlloc error condition.
if (AcctName == NULL) return {};
DomainName = (LPTSTR)GlobalAlloc(GMEM_FIXED, dwDomainName * sizeof(wchar_t));
// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) return {};
// Second call to LookupAccountSid to get the account name.
bRtnBool = LookupAccountSid
(NULL, pSidOwner, AcctName, (LPDWORD)&dwAcctName, DomainName, (LPDWORD)&dwDomainName, &eUse);
// Check GetLastError for LookupAccountSid error condition.
if (bRtnBool == FALSE) return {};
return std::make_pair(std::string(DomainName), std::string(AcctName));
}
}

View File

@@ -1,22 +1,24 @@
# include <boost/asio.hpp>
# include <iostream>
# include <fstream>
int main()
{
boost::asio::io_context io_context;
boost::asio::local::stream_protocol::endpoint ep("winjobd.sock");
// send a message to the server
boost::asio::local::stream_protocol::socket socket(io_context);
socket.connect(ep);
std::string message;
std::getline(std::cin, message);
boost::asio::write(socket, boost::asio::buffer(message));
// receive a message from the server
boost::asio::streambuf buf;
boost::asio::read_until(socket, buf, '\n');
std::istream is(&buf);
std::string line;
std::getline(is, line);
std::cout << "Received: " << line << '\n';
std::ofstream test(R"(C:\Users\chn\Desktop\test.txt)");
// boost::asio::io_context io_context;
// boost::asio::local::stream_protocol::endpoint ep(R"(C:\ProgramData\winjob\winjobd.sock)");
// // send a message to the server
// boost::asio::local::stream_protocol::socket socket(io_context);
// socket.connect(ep);
// std::string message;
// std::getline(std::cin, message);
// message += '\n';
// boost::asio::write(socket, boost::asio::buffer(message));
// // receive a message from the server
// boost::asio::streambuf buf;
// boost::asio::read_until(socket, buf, '\n');
// std::istream is(&buf);
// std::string line;
// std::getline(is, line);
// std::cout << "Received: " << line << '\n';
return 0;
}

View File

@@ -1,31 +1,82 @@
# include <boost/asio.hpp>
# include <winjob/os.hpp>
# include <winjob/scheduler.hpp>
# include <nlohmann/json.hpp>
# include <iostream>
# include <filesystem>
# include <fstream>
# include <sstream>
using namespace std::literals;
int main()
{
boost::asio::io_context io_context;
boost::asio::local::stream_protocol::endpoint ep("winjobd.sock");
boost::asio::local::stream_protocol::acceptor acceptor(io_context, ep, false);
std::function<void(const boost::system::error_code&, boost::asio::local::stream_protocol::socket)> func =
[&](const boost::system::error_code& ec, boost::asio::local::stream_protocol::socket socket)
{
if (ec)
{
std::cerr << "Failed to accept connection\n";
return;
}
std::cout << "Accepted connection\n";
boost::asio::streambuf buf;
boost::asio::read_until(socket, buf, '\n');
std::istream is(&buf);
std::string line;
std::getline(is, line);
std::cout << "Received: " << line << '\n';
// write a message to the client
std::string message = "thanks for the message\n";
boost::asio::write(socket, boost::asio::buffer(message));
acceptor.async_accept(func);
};
acceptor.async_accept(func);
io_context.run();
# ifdef _WIN32
std::filesystem::path datadir = R"(C:\ProgramData\winjob)";
# else
std::filesystem::path datadir = ".";
# endif
// ensure files and directories exist
std::filesystem::create_directories(datadir);
if (std::filesystem::exists(datadir / "winjobd.sock")) std::filesystem::remove(datadir / "winjobd.sock");
std::ofstream log(datadir / "log.txt", std::ios::app);
// setup scheduler
winjob::Scheduler scheduler(nlohmann::json::parse(std::ifstream(datadir / "config.json"))["cpu"]);
// setup connection
// boost::asio::io_context io_context;
// std::wstring endpoint = LR"(C:\ProgramData\winjob\winjobd.sock)";
// boost::asio::local::stream_protocol::acceptor acceptor(io_context, endpoint, false);
// winjob::set_permission(endpoint);
//
// std::function<void(const boost::system::error_code&, boost::asio::local::stream_protocol::socket)> func =
// [&](const boost::system::error_code& ec, boost::asio::local::stream_protocol::socket socket)
// {
// if (ec) log << "Failed to accept connection\n" << std::flush;
// else
// {
// log << "Accepted connection\n" << std::flush;
// boost::asio::streambuf buf;
// boost::asio::read_until(socket, buf, '\n');
// std::istream is(&buf);
// std::string line;
// std::getline(is, line);
// log << "Received: " << line << '\n' << std::flush;
// // write a message to the client
//
//
//
//
//
// boost::asio::write(socket, boost::asio::buffer(message));
// acceptor.async_accept(func);
// }
//
//
// };
// acceptor.async_accept(func);
// io_context.run();
//
//
//
//
// auto user = winjob::get_owner(LR"(C:\Users\chn\Desktop\winjob.exe)");
// if (!user)
// {
// log << "Failed to get owner\n" << std::flush;
// return 1;
// }
// if (user)
// {
// log << "Owner: " << user->first << "\\" << user->second << '\n' << std::flush;
// auto launcher = boost::process::v2::windows::with_logon_launcher(user->second, L"", user->first,
// LOGON_WITH_PROFILE);
// boost::asio::io_context ctx;
// boost::process::v2::error_code ec;
// std::wstring program = LR"(C:\Users\chn\Desktop\winjob.exe)";
// auto process = launcher(ctx, ec, program, std::vector<std::wstring>{});
// }
}