15#define WDEBUG(...) Ase::debug ("webui", __VA_ARGS__)
28 bool snap_workaround =
true;
31 snap_workaround =
false;
32 if (snap_workaround) {
33 basedir = Path::xdg_dir (
"DOWNLOAD") +
"/." + executable;
35 "This directory only exists to enable temporary file exchange with\n"
36 "snap packages which are unable to read from ~/.cache/.\n"
37 "Feel free to remove this directory in its entirety.\n";
39 !Path::stringwrite (basedir +
"/README", readme,
true)) {
46 const String query = token.
empty() ?
"" :
"?token=" + token;
50 "<html><!--@@TEMPFILE_PID=%d@@-->\n"
51 "<head><title>%s Authentication Redirect</title>\n"
52 "<meta http-equiv=\"refresh\" content=\"0; url=%s\">\n"
54 "<h1>%s Authentication Redirect</h1>\n\n"
55 "<p>Redirecting to %s: <a href=\"%s\">%s</a></p>\n"
56 "<hr><address>%s</address>\n"
57 "<hr></body></html>\n",
58 getpid(), ua, link + query, ua, ua, link + query, link, ua);
60 if (!Path::stringwrite (html_file, html_text,
true, 0600))
77 if (!! (flags & WebuiFlags::STDIO_REDIRECT)) {
81 return {
errno,
"mkdirs " + logdir };
82 stdio_fd =
open (logfile.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_NOFOLLOW, 0640);
84 return {
errno,
"open " + logfile };
88 int console_stdout_fd = -1, console_stderr_fd = -1;
89 if (!! (flags & WebuiFlags::CONSOLE_LOGS)) {
90 console_stdout_fd =
dup (STDOUT_FILENO);
91 if (console_stdout_fd < 0)
92 return {
errno,
"dup stdout" };
93 console_stderr_fd =
dup (STDERR_FILENO);
94 if (console_stderr_fd < 0) {
95 close (console_stdout_fd);
96 return {
errno,
"dup stderr" };
98 keep_fds.push_back (console_stdout_fd);
99 keep_fds.push_back (console_stderr_fd);
102 if (mode ==
"chromium" || mode ==
"google-chrome")
105 argv.push_back (browser_name);
107 if (temp_dir.
empty())
108 return {
errno,
"mkdtemp" };
109 const std::string user_data_dir_arg =
"--user-data-dir=" + temp_dir;
112 for (
const auto arg : {
113 "--incognito",
"--no-first-run",
"--no-experiments",
114 "--no-default-browser-check",
"--disable-extensions",
"--disable-sync",
116 "--bwsi",
"--new-window" })
117 argv.push_back (arg);
118 if (!! (flags & WebuiFlags::HEADLESS))
119 argv.push_back (
"--headless");
120 argv.push_back (user_data_dir_arg);
121 argv.push_back (app);
123 else if (mode ==
"htmlgui")
127 argv.push_back (
"--no-sandbox");
128 if (!! (flags & WebuiFlags::HEADLESS))
129 argv.push_back (
"--headless");
130 if (console_stdout_fd >= 0 && console_stderr_fd >= 0)
131 argv.push_back (
string_format (
"--console-logs=%d,%d", console_stdout_fd, console_stderr_fd));
132 argv.push_back (url);
134 else if (mode ==
"none" or mode ==
"" or mode ==
"wait")
140 ErrorReason ereason =
spawn_process (argv, &child_pid, SIGTERM, stdio_fd, keep_fds);
142 for (
int fd : keep_fds)
148 loop->exec_sigchld (child_pid,
149 [onclose] (pid_t pid,
int status)
152 if (WIFEXITED (status))
154 else if (WIFSIGNALED (status))
157 info (
"WebUI: child process pid=%d exited: %s", pid, state);
158 atquit_del_killl_pid (pid);
163 info (
"WebUI: started %s pid=%d: %s", browser_name, child_pid, url);
bool mkdirs(const String &dirpath, uint mode)
Create the directories in dirpath with mode, check errno on false returns.
bool check(const String &file, const String &mode)
String cache_home()
Get the $XDG_CACHE_HOME directory, see: https://specifications.freedesktop.org/basedir-spec/latest.
The Anklang C++ API namespace.
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
void atquit_add_removal(const std::string &filename)
Remove filename (or directory) when the program terminates.
String string_capitalize(const String &str, size_t maxn, bool rest_tolower)
Capitalize words, so the first letter is upper case, the rest lower case.
std::string anklang_runpath(RPath rpath, const String &segment)
Retrieve various resource paths at runtime.
std::string String
Convenience alias for std::string.
ErrorReason spawn_process(const std::vector< std::string > &argv, pid_t *child_pid, int pdeathsig, int stdio_fd, const std::vector< int > &keep_fds)
Span a child process after cleaning up the environment.
void cleanup_orphaned_tempfiles(const std::string &directory)
Delete all files that contain @TEMPFILE_PID=d@ without a running pid_t d.
void atquit_add_killl_pid(int pid)
Kill pid when the program terminates.
std::string create_tempfile_dir(const std::string &basename)
Create temporary directory under /tmp, scheduled for removal atquit.