-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnosuspend.c
176 lines (156 loc) · 4.6 KB
/
nosuspend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <spawn.h>
#include "config.h"
/* gcc -o nosuspend nosuspend.c */
/* chown -v root:root ./nosuspend */
/* chmod -v 4755 ./nosuspend */
extern char **environ;
void show_help(char* arg) {
fprintf(stderr,
"nosuspend blocks computer suspend while another "
"command-line operation is running\n"
"Usage: %s executable arg arg2 . . \n", arg);
}
void show_version(char* arg) {
fprintf(stderr, "nosuspend Version \033[1;32m %s \n \033[0m"
" GNU GENERAL PUBLIC LICENSE V.3 @ 2017 by Hermman Meyer\n", VERSION );
}
void run_cmd(char *cmd) {
pid_t pid;
char *arg[] = {"sh", "-c", cmd, NULL};
int status;
status = posix_spawn(&pid, "/bin/sh", NULL, NULL, arg, environ);
if (status == 0) {
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid");
}
} else {
fprintf(stderr,"posix_spawn: %s\n", strerror(status));
}
}
void create_cmd_line(char* cmd, int argc,char* argv[], int sc) {
char uname[100] = {"\0",};
if (getlogin_r(uname,100)) {
fprintf(stderr, "Fail to get user name, exit here\n");
exit(1);
}
const char* cmd1 = "/bin/systemd-inhibit --why='User application run' su ";
const char* cmd2 = " -c '";
strcat(cmd, cmd1);
strcat(cmd,uname);
strcat(cmd, cmd2);
if (sc) strcat(cmd, "screen ");
for (int i = 1; i < argc; ++i) {
if ((strcmp(argv[i],"--try-again") !=0)) {
strcat(cmd, argv[i]);
strcat(cmd, " ");
}
}
strcat(cmd, " '");
//fprintf(stderr," cmd = %s", cmd);
}
char* check_for_gui_libs(char *p, char* argv[]) {
char cmdp[100] = {"\0",};
strcat(cmdp,"which ");
strcat(cmdp,argv[1]);
strcat(cmdp,"|xargs ldd | grep -E 'libgtk*|libqt*|*fltk*'");
FILE *fp = popen(cmdp, "r");
if (!fp) {
return 0;
}
char fpf[1024] = {"\0",};
if( fgets (fpf, sizeof(fpf), fp)!=NULL ) {
p = fgets(fpf, sizeof(fpf), fp);
}
pclose(fp);
return p;
}
int check_user_input(int argc,char* argv[]) {
//fprintf(stderr," argc = %i", argc);
if (strlen(argv[1]) > 45) {
fprintf(stderr, "first arg is to long my friend \n");
return 1;
}
int bz = 0;
int bzgo = 0;
const char nogo[] = "&;|$><`\\!";
char *ret = NULL;
for (int i = 0; i < argc; ++i) {
//fprintf(stderr," argv = %s", argv[i]);
bz += strlen(argv[i]);
bzgo +=strspn(argv[i],". ABCDEFGHIJKLMNOPQRSTUVWXYZÄÜÖabcdefghijklmnopqrstuvwxyzäüöß0123456789_/'-?*~:%");
ret = strpbrk(argv[i],nogo);
if (bz>4000) {
fprintf(stderr, "arg list is to long my friend \n");
return 1;
}
if (ret) {
fprintf(stderr, "arg (& ; | $ > < ` \\ ! ) is not allowed \n");
return 1;
}
if (bz != bzgo) {
fprintf(stderr, "arg contain not allowed chars \n");
return 1;
}
}
return 0;
}
void create_first_cmd_line(char* cmd, int argc,char* argv[]) {
strcat(cmd, "pkexec ");
strcat(cmd, argv[0]);
strcat(cmd, " ");
for (int i = 1; i < argc; ++i) {
strcat(cmd, argv[i]);
strcat(cmd, " ");
}
strcat(cmd, " --try-again ");
//fprintf(stderr," cmd = %s", cmd);
}
int main(int argc,char* argv[]){
if ((strcmp(argv[1], "-h") == 0) ||(strcmp(argv[1], "--help")== 0)) {
show_help(argv[0]);
return 0;
} else if ((strcmp(argv[1], "-v") == 0) ||(strcmp(argv[1], "--version")== 0)) {
show_version(argv[0]);
return 0;
} else if(argc<2) {
show_help(argv[0]);
return 0;
}
char cmd[5000] = {"\0",};
if (geteuid () != 0 ) {
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i],"--try-again") ==0) {
fprintf(stderr," Error executing command: Request dismissed");
exit(1);
}
}
create_first_cmd_line(cmd, argc, argv);
run_cmd(cmd);
exit(0);
}
if (system("which screen 2>&1 >/dev/null")!=0) {
fprintf(stderr, "couldn't find 'screen', please install it to use nosuspend\n");
exit(1);
}
if (check_user_input(argc, argv)) {
exit(1);
}
char *p = "";
p = check_for_gui_libs(p, argv);
if (*p == 0) {
create_cmd_line(cmd, argc, argv, 1);
run_cmd(cmd);
sleep(1);
} else {
create_cmd_line(cmd, argc, argv, 0);
run_cmd(cmd);
exit(0);
}
return 0;
}