usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / rc / cifs.c
blob1140a384db0127d88704add7e1cee7e0564a17a9
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "rc.h"
10 #include <sys/mount.h>
11 #include <sys/stat.h>
12 #include <sys/statfs.h>
13 #ifndef MNT_DETACH
14 #define MNT_DETACH 0x00000002
15 #endif
18 void start_cifs(void)
20 xstart("mount-cifs", "-m");
23 void stop_cifs(void)
25 killall("mount-cifs", SIGTERM);
26 eval("mount-cifs", "-u");
29 int mount_cifs_main(int argc, char *argv[])
31 char s[512];
32 char opt[512];
33 char mpath[32];
34 int i, j;
35 int try;
36 int first;
37 char *on, *unc, *user, *pass, *dom, *exec, *servern, *sec, *custom;
38 int done[3];
39 struct statfs sf;
41 if (argc == 2) {
42 if (strcmp(argv[1], "-m") == 0) {
43 done[1] = 0;
44 done[2] = 0;
45 first = 1;
46 try = 0;
47 while (1) {
48 for (i = 1; i <= 2; ++i) {
49 if (done[i]) continue;
51 done[i] = 2;
53 sprintf(s, "cifs%d", i);
54 strlcpy(s, nvram_safe_get(s), sizeof(s));
55 if ((vstrsep(s, "<", &on, &unc, &user, &pass, &dom, &exec, &servern, &sec) != 8) || (*on != '1')) continue;
56 custom = nvram_safe_get("cifs_opts");
58 done[i] = 0;
60 if (first) {
61 notice_set("cifs", "Mounting...");
62 modprobe("cifs");
63 first = 0;
64 if (nvram_get_int("cifs_dbg") > 0) {
65 f_write_string("/proc/fs/cifs/cifsFYI", nvram_safe_get("cifs_dbg"), 0, 0);
69 j = sprintf(opt, "sep=<unc=%s", unc);
70 if (*user) j += sprintf(opt + j, "<user=%s", user);
71 if (*pass) j += sprintf(opt + j, "<pass=%s", pass);
72 if (*dom) j += sprintf(opt + j, "<dom=%s", dom);
73 if (*servern) j += sprintf(opt + j, "<servern=%s", servern);
74 if (*sec) j += sprintf(opt + j, "<sec=%s", sec);
75 if (*custom) j += sprintf(opt + j, "<%s", custom);
77 sprintf(mpath, "/cifs%d", i);
78 umount(mpath);
79 if (mount("-", mpath, "cifs", MS_NOATIME|MS_NODIRATIME, opt) != 0) continue;
80 done[i] = 1;
81 if (try > 12) try = 12; // 1 min
83 if (*exec) {
84 chdir(mpath);
85 system(exec);
87 run_userfile(mpath, ".autorun", mpath, 3);
89 if ((done[1]) && (done[2])) {
90 notice_set("cifs", "");
91 return 0;
94 sleep(5 * ++try);
95 if (try == 24) { // 2 min
96 sprintf(s, "Error mounting CIFS #%s. Still trying... ", (done[1] == done[2]) ? "1 and #2" : ((done[1] == 0) ? "1" : "2"));
97 notice_set("cifs", s);
99 else if (try > 180) { // 15 mins
100 try = 180;
104 return 1;
106 if (strcmp(argv[1], "-u") == 0) {
107 for (i = 1; i <= 2; ++i) {
108 sprintf(mpath, "/cifs%d", i);
109 if ((statfs(mpath, &sf) == 0) && (sf.f_type != 0x73717368)) {
110 // is mounted
111 run_userfile(mpath, ".autostop", mpath, 5);
112 run_nvscript("script_autostop", mpath, 5);
114 umount2(mpath, MNT_DETACH);
116 modprobe_r("cifs");
117 notice_set("cifs", "");
118 return 0;
122 usage_exit(argv[0], "-m|-u");
123 return 1;