-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.c
260 lines (217 loc) · 6.54 KB
/
connect.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (c) 2006 Jeremy Whelchel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "pz.h"
#include "podtopod.h"
//static char *ScsiDir = "/dev/scsi/host0/bus0/target0/lun0/";
/* disc, part1, part2, part3 */
/* /dev/sda -- main partition
/dev/sda1 -- firmware+bootloader
/dev/sda2 -- data partition
/dev/sda3 -- linux filesystem [if exists]
*/
//static char *ScsiDeviceFile = "/proc/bus/scsi";
// Checks whether a kernel module of the specified name is loaded
// TODO: Rewrite checking only /proc/modules
int check_kernel_module(char *module_name) {
char *lsmod_cmd = "/bin/lsmod";
size_t len = 0;
ssize_t read;
char *buf = NULL;
FILE *fp;
if((fp = popen(lsmod_cmd, "r")) == NULL)
return -1;
getline(&buf, &len, fp); // first line is just header info
while ((read = getline(&buf, &len, fp)) != -1)
if (strncmp(buf, module_name, strlen(module_name)) == 0) {
free(buf);
pclose(fp);
return 1;
}
if (buf)
free(buf);
pclose(fp);
return 0;
}
/* poll_connection() - checks to see if there is an iPod
* connected to and recognized by the SCSI subsystem.
* Non-iPod (perhaps only works for 2.6 kernels with sysfs):
* Polls all devices in /sys/bus/scsi/devices/ *
* iPod version (uses SCSI procfs)
* Polls all devices in /proc/scsi/scsi
*/
#define ACCEPTED_MODEL "iPod"
#define ACCEPTED_VENDOR "Apple"
#define SCSI_DIRNAME "/sys/bus/scsi/devices"
#ifdef IPOD
/* Expected contents of /proc/scsi/scsi:
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
Vendor: Apple Model: iPod Rev: 1.62
*/
int poll_connection() {
#ifdef ALWAYS_CONNECTED
return 1;
#endif
char buf[256];
FILE *fp = fopen ("/proc/scsi/scsi", "r");
if (!fp)
return -1;
int ret = 0;
while (ret==0 && fgets (buf, 256, fp)) {
char *vendor, *model;
if (!(vendor = strstr(buf, "Vendor: "))) continue;
if (!(model = strstr(buf, "Model: "))) continue;
vendor += 8;
model += 7;
if (strncmp(vendor, ACCEPTED_VENDOR, strlen(ACCEPTED_VENDOR)) == 0)
if (strncmp(model, ACCEPTED_MODEL, strlen(ACCEPTED_MODEL)) == 0)
ret = 1;
}
fclose (fp);
return ret;
}
#else
int poll_connection() {
struct dirent *subdir;
DIR *dir;
FILE *file;
char longname[512];
dir = opendir(SCSI_DIRNAME);
while ((subdir = readdir(dir))) {
char model[64];
char vendor[64];
if (strncmp(subdir->d_name,".", 1 /*strlen(subdir->d_name)*/) == 0) continue;
/* Get model name */
sprintf(longname, "%s/%s/model", SCSI_DIRNAME, subdir->d_name);
file = fopen(longname, "r");
if (!file) continue;
fgets(model, 64, file);
fclose(file);
if (model[strlen(model)-1]=='\n') model[strlen(model)-1] = '\0';
/* Get vendor name */
sprintf(longname, "%s/%s/vendor", SCSI_DIRNAME, subdir->d_name);
file = fopen(longname, "r");
if (!file) continue;
fgets(vendor, 64, file);
fclose(file);
if (vendor[strlen(vendor)-1]=='\n') vendor[strlen(vendor)-1] = '\0';
if (strncmp(model, ACCEPTED_MODEL, strlen(ACCEPTED_MODEL)) == 0)
if (strncmp(vendor, ACCEPTED_VENDOR, strlen(ACCEPTED_VENDOR)) == 0) {
return 1;
}
}
return 0;
}
#endif /* IPOD */
/* Polls to check if RiP is mounted.
* Returns 0/1 for mounted/unmounted.
* -1 is for error.
* 2 is for mounted at incorrect location
*/
int poll_mount(const char *expected_device, const char *expected_mountpoint) {
#ifdef ALWAYS_MOUNTED
return 1;
#endif
char buf[256];
FILE *fp = fopen ("/proc/mounts", "r");
if (!fp)
return -1;
int ret = 0;
while (ret==0 && fgets (buf, 256, fp)) {
if (!strchr (buf, ' ')) continue;
char *mountpt = strchr (buf, ' ') + 1;
//char *fstype = strchr (mountpt, ' ') + 1;
*strchr (buf, ' ') = 0;
*strchr (mountpt, ' ') = 0;
// *strchr (fstype, ' ') = 0;
if (strcmp (buf, expected_device)==0) {
if (strcmp (mountpt, expected_mountpoint)==0)
ret = 1;
else {
pz_error("Warning: Remote ipod mounted at %s, expecting %s\n", mountpt, expected_mountpoint);
ret = 2;
}
}
}
fclose (fp);
return ret;
}
int do_mount() {
printf("Mounting %s at %s\n", RIP_MOUNTDEVICE, RIP_MOUNTPOINT);
if (vfork() == 0) {
/* This needs to wait for sbp2/scsi subsystem to recognize
all the proper partitions on the iPod
Better yet: do checking to make sure proper partitions exists
this could also deal with FAT vs HFS iPods
*/
#ifdef IPOD
execl("/bin/mount", "/bin/mount", "-t", "vfat", RIP_MOUNTDEVICE, RIP_MOUNTPOINT, NULL);
#else
execl("/bin/mount", "/bin/mount", RIP_MOUNTPOINT, NULL);
#endif
_exit(0);
}
return 1;
}
int do_unmount() {
if (!poll_mount(RIP_MOUNTDEVICE, RIP_MOUNTPOINT))
return 0;
printf("Unmounting %s\n", RIP_MOUNTDEVICE);
if (vfork() == 0) {
execl("/bin/umount", "/bin/umount", RIP_MOUNTDEVICE, NULL);
_exit(0);
}
return 1;
}
extern int rip_status;
int do_eject() {
if (rip_status == STATUS_MOUNTED)
do_unmount();
printf("Ejecting iPod at %s\n", RIP_MOUNTDEVICE);
#ifdef IPOD
/* This is a poor hack to deal with busybox's eject not working for SCSI devices */
if (vfork() == 0) { execl("/bin/rmmod", "/bin/rmmod", "sbp2", NULL); _exit(0); }
pz_dialog("iPod Eject", "Please physically disconnect the iPods before pressing [OK]", 1, 0, "OK");
if (vfork() == 0) { execl("/bin/modprobe", "/bin/modprobe", "sbp2", NULL); _exit(0); }
#else
if (vfork() == 0) { execl("/usr/bin/eject", "/usr/bin/eject", RIP_MOUNTDEVICE, NULL); _exit(0); }
#endif
ipod_status_ejected();
return 1;
}
PzWindow *new_domount_window() {
do_mount();
return TTK_MENU_DONOTHING;
}
PzWindow *new_dounmount_window() {
do_unmount();
return TTK_MENU_DONOTHING;
}
PzWindow *new_eject_window() {
do_eject();
return TTK_MENU_UPALL;
}