Skip to content
API Documentation
BastionGuard™
TeeJee.System.vala
Go to the documentation of this file.
1
2/*
3 * TeeJee.System.vala
4 *
5 * Copyright 2012-2018 Tony George <teejeetech@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 *
22 *
23 */
24
25namespace TeeJee.System{
26
28 using TeeJee.Logging;
29 using TeeJee.Misc;
31
32 // user ---------------------------------------------------
33
34 public bool user_is_admin(){
35
36 return (get_user_id_effective() == 0);
37 }
38
39 public int get_user_id(){
40
41 // returns actual user id of current user (even for applications executed with sudo and pkexec)
42
43 string pkexec_uid = GLib.Environment.get_variable("PKEXEC_UID");
44
45 if (pkexec_uid != null){
46 return int.parse(pkexec_uid);
47 }
48
49 string sudo_user = GLib.Environment.get_variable("SUDO_UID");
50
51 if (sudo_user != null){
52 return int.parse(sudo_user);
53 }
54
55 return get_user_id_effective(); // normal user
56 }
57
58 private int euid = -1; // cache for get_user_id_effective (its never going to change)
60 // returns effective user id (0 for applications executed with sudo and pkexec)
61 if (euid < 0) {
62 euid = (int) Posix.geteuid();
63 }
64
65 return euid;
66 }
67
68 public string? get_username_from_uid(int user_id){
69 unowned Posix.Passwd? pw = Posix.getpwuid(user_id);
70 string? outvalue = pw?.pw_name;
71 if(null == outvalue) {
72 log_error("Could not get username for uid %d".printf(user_id));
73 }
74 return outvalue;
75 }
76
77 // system ------------------------------------
78
80
81 /* Returns the system up-time in seconds */
82
83 string uptime = file_read("/proc/uptime").split(" ")[0];
84 double secs = double.parse(uptime);
85 return secs;
86 }
87
88 // open -----------------------------
89
90 public static bool xdg_open (string file){
91 if (!TeeJee.ProcessHelper.cmd_exists("xdg-open")) {
92 return false;
93 }
94
95 string cmd = "xdg-open '%s'".printf(escape_single_quote(file));
96
97 return exec_user_async(cmd) == 0;
98 }
99
100 public bool exo_open_folder (string dir_path, bool xdg_open_try_first = true){
101
102 /* Tries to open the given directory in a file manager */
103
104 /*
105 xdg-open is a desktop-independent tool for configuring the default applications of a user.
106 Inside a desktop environment (e.g. GNOME, KDE, Xfce), xdg-open simply passes the arguments
107 to that desktop environment's file-opener application (gvfs-open, kde-open, exo-open, respectively).
108 We will first try using xdg-open and then check for specific file managers if it fails.
109 */
110
111 bool xdgAvailable = cmd_exists("xdg-open");
112 string escaped_dir_path = escape_single_quote(dir_path);
113 int status = -1;
114
115 if (xdg_open_try_first && xdgAvailable){
116 //try using xdg-open
117 string cmd = "xdg-open '%s'".printf(escaped_dir_path);
118 status = exec_script_async (cmd);
119 return (status == 0);
120 }
121
122 foreach(string app_name in
123 new string[]{ "nemo", "nautilus", "thunar", "io.elementary.files", "pantheon-files", "marlin", "dolphin" }){
124 if(!cmd_exists(app_name)) {
125 continue;
126 }
127
128 string cmd = "%s '%s'".printf(app_name, escaped_dir_path);
129 status = exec_script_async (cmd);
130
131 if(status == 0) {
132 return true;
133 }
134 }
135
136 if (!xdg_open_try_first && xdgAvailable){
137 //try using xdg-open
138 string cmd = "xdg-open '%s'".printf(escaped_dir_path);
139 status = exec_script_async (cmd);
140 return (status == 0);
141 }
142
143 return false;
144 }
145
146 public bool using_efi_boot(){
147
148 /* Returns true if the system was booted in EFI mode
149 * and false for BIOS mode */
150
151 return dir_exists("/sys/firmware/efi");
152 }
153
154 // timers --------------------------------------------------
155
156 public GLib.Timer timer_start(){
157 var timer = new GLib.Timer();
158 timer.start();
159 return timer;
160 }
161
162 public ulong timer_elapsed(GLib.Timer timer, bool stop = true){
163 ulong microseconds;
164 double seconds;
165 seconds = timer.elapsed (out microseconds);
166 if (stop){
167 timer.stop();
168 }
169 return (ulong)((seconds * 1000 ) + (microseconds / 1000));
170 }
171
172 public void sleep(int milliseconds){
173 Thread.usleep ((ulong) milliseconds * 1000);
174 }
175
176 public string timer_elapsed_string(GLib.Timer timer, bool stop = true){
177 ulong microseconds;
178 double seconds;
179 seconds = timer.elapsed (out microseconds);
180 if (stop){
181 timer.stop();
182 }
183 return "%.0f ms".printf((seconds * 1000 ) + microseconds/1000);
184 }
185
186 public void set_numeric_locale(string type){
187 Intl.setlocale(GLib.LocaleCategory.NUMERIC, type);
188 Intl.setlocale(GLib.LocaleCategory.COLLATE, type);
189 Intl.setlocale(GLib.LocaleCategory.TIME, type);
190 }
191}
string escape_single_quote(string file_path)
string? file_read(string file_path)
bool dir_exists(string dir_path)
void log_error(string message, bool highlight=false, bool is_warning=false)
void set_numeric_locale(string type)
int exec_script_async(string script)
static int exec_user_async(string command)
static bool cmd_exists(string cmd_tool)
double get_system_uptime_seconds()
GLib.Timer timer_start()
int get_user_id_effective()
bool exo_open_folder(string dir_path, bool xdg_open_try_first=true)
ulong timer_elapsed(GLib.Timer timer, bool stop=true)
string? get_username_from_uid(int user_id)
string timer_elapsed_string(GLib.Timer timer, bool stop=true)
static bool xdg_open(string file)
void sleep(int milliseconds)