forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libosal.c
150 lines (140 loc) · 4.11 KB
/
libosal.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
/******************************************************************************
* Copyright (C) 2014-2015
* file: libosal.c
* author: gozfree <[email protected]>
* created: 2015-11-28 17:59:24
* updated: 2015-11-28 17:59:24
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include "libosal.h"
int system_noblock(char **argv)
{
pid_t pid;
struct sigaction ignore, saveintr, savequit;
sigset_t chldmask, savemask;
if (argv == NULL)
return -1;
ignore.sa_handler = SIG_IGN;
sigemptyset(&ignore.sa_mask);
ignore.sa_flags = 0;
if (sigaction(SIGINT, &ignore, &saveintr) < 0) return -1;
if (sigaction(SIGQUIT, &ignore, &savequit) < 0) return -1;
if (sigaction(SIGCHLD, &ignore, &savequit) < 0) return -1;
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0) return -1;
if ((pid = fork()) < 0) {
perror("fork");
return -1;
} else if (pid == 0) {
sigaction(SIGINT, &saveintr, NULL);
sigaction(SIGQUIT, &savequit, NULL);
sigaction(SIGCHLD, &savequit, NULL);
sigprocmask(SIG_SETMASK, &savemask, NULL);
if (-1 == execvp(argv[0], argv)) {
perror("exec");
}
_exit(127);
} else {
}
if (sigaction(SIGINT, &saveintr, NULL) < 0) return -1;
if (sigaction(SIGQUIT, &savequit, NULL) < 0) return -1;
if (sigaction(SIGCHLD, &ignore, &savequit) < 0) return -1;
if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0) return -1;
return pid;
}
ssize_t system_noblock_with_result(char **argv, void *buf, size_t count)
{
int len = -1;
int old_fd, new_fd;
int fd[2];
if (pipe(fd)) {
printf("pipe failed\n");
return -1;
}
int rfd = fd[0];
int wfd = fd[1];
if (EOF == fflush(stdout)) {
printf("fflush failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (old_fd = dup(STDOUT_FILENO))) {
printf("dup STDOUT_FILENO failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (new_fd = dup2(wfd, STDOUT_FILENO))) {
//no need to check failed??
//printf("dup2 STDOUT_FILENO failed: %s\n", strerror(errno));
//return -1;
}
if (-1 == system_noblock(argv)) {
printf("system call failed!\n");
return -1;
}
if (-1 == read(rfd, buf, count-1)) {
printf("read buffer failed!\n");
return -1;
}
len = strlen(buf);
*((char *)buf + len - 1) = '\0';
if (-1 == dup2(old_fd, new_fd)) {
printf("dup2 failed: %s\n", strerror(errno));
return -1;
}
return len;
}
ssize_t system_with_result(const char *cmd, void *buf, size_t count)
{
int len = -1;
int old_fd, new_fd;
int fd[2];
if (pipe(fd)) {
printf("pipe failed\n");
return -1;
}
int rfd = fd[0];
int wfd = fd[1];
if (EOF == fflush(stdout)) {
printf("fflush failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (old_fd = dup(STDOUT_FILENO))) {
printf("dup STDOUT_FILENO failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (new_fd = dup2(wfd, STDOUT_FILENO))) {
//no need to check failed??
//printf("dup2 STDOUT_FILENO failed: %s\n", strerror(errno));
//return -1;
}
if (-1 == system(cmd)) {
printf("system call failed!\n");
return -1;
}
if (-1 == read(rfd, buf, count-1)) {
printf("read buffer failed!\n");
return -1;
}
len = strlen(buf);
*((char *)buf + len - 1) = '\0';
if (-1 == dup2(old_fd, new_fd)) {
printf("dup2 failed: %s\n", strerror(errno));
return -1;
}
return len;
}
int is_little_endian(void)
{
unsigned int probe = 0xff;
size_t sz = sizeof(unsigned int);
unsigned char * probe_byte = (unsigned char *)&probe;
if (!(probe_byte[0] == 0xff || probe_byte[sz - 1] == 0xff)) {
printf("%s: something wrong!\n", __func__);
}
return probe_byte[0] == 0xff;
}