-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusbeject.c
More file actions
233 lines (202 loc) · 5.58 KB
/
usbeject.c
File metadata and controls
233 lines (202 loc) · 5.58 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <linux/limits.h>
#include "config.h"
#ifdef DEBUG
# define D(...) printf(__VA_ARGS__)
#else
# define D(...) ((void)0)
#endif
struct cookie {
char *name;
char *fstype;
char *devname;
char *mountpoint;
char mounted;
};
static struct cookie cookies[COOKIE_MAX];
static int cookie_cnt;
static char *strnstr(char const *str, int len, char const *pat)
{
int i, j;
for (i = 0; i < len; i++) {
for (j = 0; i+j < len && '\0' != pat[j] && str[i+j] == pat[j]; j++)
;
if ('\0' == pat[j])
return (char*)str+i;
}
return NULL;
}
static int isinteger(char const *num)
{
for (; isdigit(*num) && '\0' != *num; num++)
;
return ('\0' == *num);
}
static int parse_cookie(char const *path, char *fstype, char *devname, char *mountpoint)
{
int ret = 0;
int len;
FILE *content = fopen(path, "r");
struct string {
char *str;
int len;
} strs[] = {
{fstype, FSTYPE_LEN},
{devname, PATH_MAX},
{mountpoint, PATH_MAX},
};
for (int i = 0; i < 3; i++) {
if (!fgets(strs[i].str, strs[i].len, content)) {
ret = -1;
goto close;
}
len = strlen(strs[i].str);
if ('\n' != strs[i].str[len-1]) {
ret = -2;
goto close;
}
strs[i].str[len-1] = '\0'; /* remove '\n' */
}
close:
fclose(content);
return ret;
}
static int init(void)
{
int ret = 0;
DIR *cookie_root = opendir(COOKIE);
struct dirent *ent;
cookie_cnt = 0;
if (!cookie_root) {
printf("[Warn] %s: %s\n", COOKIE, strerror(errno));
return 0;
}
while ((ent = readdir(cookie_root)) != NULL) {
if (ent->d_type != DT_REG)
continue;
/* fstype devname mountpoint */
char fstype[FSTYPE_LEN], devname[PATH_MAX], mountpoint[PATH_MAX];
char path[PATH_MAX] = COOKIE"/";
strncat(path, ent->d_name, PATH_MAX-strlen(path)-1);
if (0 != parse_cookie(path, fstype, devname, mountpoint)) {
ret = -1;
goto close;
}
cookies[cookie_cnt].name = strdup(ent->d_name);
cookies[cookie_cnt].fstype = strdup(fstype);
cookies[cookie_cnt].devname = strdup(devname);
cookies[cookie_cnt].mountpoint = strdup(mountpoint);
cookies[cookie_cnt].mounted = 1;
D("%s %s %s %s\n", ent->d_name, fstype, devname, mountpoint);
cookie_cnt++;
}
close:
closedir(cookie_root);
return ret;
}
static void quit(void)
{
for (int i = 0; i < cookie_cnt; i++) {
free(cookies[i].name);
free(cookies[i].devname);
free(cookies[i].mountpoint);
free(cookies[i].fstype);
}
}
static void eject(int idx)
{
char name[PATH_MAX] = COOKIE"/";
strncat(name, cookies[idx].name, PATH_MAX);
if (-1 == umount(cookies[idx].mountpoint)) {
printf("umount %s: %s\n", cookies[idx].mountpoint, strerror(errno));
return;
}
rmdir(cookies[idx].mountpoint);
remove(name);
cookies[idx].mounted = 0;
D("name: %s\n", name);
}
static void eject_all(void)
{
for (int i = 0; i < cookie_cnt; i++)
eject(i);
}
static void list(void)
{
if (0 == cookie_cnt) {
printf("There is no device mounted at %s.\n", COOKIE);
return;
}
printf("no.\t%-20s %-20s %-20s\n", "devname", "mountpoint", "fstype");
for (int i = 0; i < cookie_cnt; i++) {
printf("%2d.\t%-20s %-20s %-20s\n", i+1,
cookies[i].devname, cookies[i].mountpoint, cookies[i].fstype);
}
}
static void help(void)
{
printf("Usage: %s [-hl] [<mountpoint> or <no.>]\n", APP);
printf("usbeject ejects usb device mounted via `usbmount`.\n");
printf(" -h: show this page\n"
" -l: list all devices and their mountpoints\n"
" <mountpoint>: a mountpoint\n"
" <no.>: number of order listed\n"
"If no argment given, eject all usb devices.\n");
printf("%s version %s BSD-2\n", APP, APP_VER);
}
int main(int argc, char **argv)
{
char opt;
if (0 != init()) {
printf("[Error] Init Error, Quit.\n");
return 1;
}
while (-1 != (opt = getopt(argc, argv, "hl"))) {
switch (opt) {
case 'h':
help();
exit(0);
break;
case 'l':
list();
exit(0);
break;
default:
help();
break;
}
}
if (optind < argc) {
for (int i = optind; i < argc; i++) {
if (isinteger(argv[i])) {
int no = atoi(argv[i]);
D("<no.>: %d\n", no);
no--; /* begin from 0 */
if (0 <= no && no < cookie_cnt && cookies[no].mounted)
eject(no);
} else {
for (int c = 0; c < cookie_cnt; c++) {
if ((strnstr(cookies[c].devname, strlen(cookies[c].devname), argv[i])
|| strnstr(cookies[c].mountpoint, strlen(cookies[c].mountpoint), argv[i]))
&& cookies[c].mounted) {
D("<mountpoint>: %s %s\n", argv[i], cookies[c].mountpoint);
eject(c);
}
}
}
}
exit(0);
}
/* no argument given */
eject_all();
quit();
return 0;
}