Netgear WNR3500L: correct leds behavior
[tomato.git] / release / src / router / rc / cifs.c
blob1375f3e55faf1a33684ad6ea5f76025bdd45b001
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>
14 void start_cifs(void)
16 xstart("mount-cifs", "-m");
19 void stop_cifs(void)
21 killall("mount-cifs", SIGTERM);
22 eval("mount-cifs", "-u");
25 int mount_cifs_main(int argc, char *argv[])
27 char s[512];
28 char opt[512];
29 char mpath[32];
30 int i, j;
31 int try;
32 int first;
33 char *on, *unc, *user, *pass, *dom, *exec, *servern, *sec;
34 int done[3];
35 char *exargv[3];
36 int pid;
38 if (argc == 2) {
39 if (strcmp(argv[1], "-m") == 0) {
40 done[1] = 0;
41 done[2] = 0;
42 first = 1;
43 try = 0;
44 while (1) {
45 for (i = 1; i <= 2; ++i) {
46 if (done[i]) continue;
48 done[i] = 2;
50 sprintf(s, "cifs%d", i);
51 strlcpy(s, nvram_safe_get(s), sizeof(s));
52 if ((vstrsep(s, "<", &on, &unc, &user, &pass, &dom, &exec, &servern, &sec) != 8) || (*on != '1')) continue;
54 done[i] = 0;
56 if (first) {
57 notice_set("cifs", "Mounting...");
58 modprobe("cifs");
59 first = 0;
62 j = sprintf(opt, "sep=<unc=%s", unc);
63 if (*user) j += sprintf(opt + j, "<user=%s", user);
64 if (*pass) j += sprintf(opt + j, "<pass=%s", pass);
65 if (*dom) j += sprintf(opt + j, "<dom=%s", dom);
66 if (*servern) j += sprintf(opt + j, "<servern=%s", servern);
67 if (*sec) j += sprintf(opt + j, "<sec=%s", sec);
69 sprintf(mpath, "/cifs%d", i);
70 umount(mpath);
71 if (mount("-", mpath, "cifs", MS_NOATIME|MS_NODIRATIME, opt) != 0) continue;
72 done[i] = 1;
73 if (try > 12) try = 12; // 1 min
75 if (*exec) {
76 chdir(mpath);
77 exargv[0] = exec;
78 exargv[1] = NULL;
79 _eval(exargv, NULL, 0, &pid);
82 if ((done[1]) && (done[2])) {
83 notice_set("cifs", "");
84 return 0;
87 sleep(5 * ++try);
88 if (try == 24) { // 2 min
89 sprintf(s, "Error mounting CIFS #%s. Still trying... ", (done[1] == done[2]) ? "1 and #2" : ((done[1] == 0) ? "1" : "2"));
90 notice_set("cifs", s);
92 else if (try > 180) { // 15 mins
93 try = 180;
97 return 1;
99 if (strcmp(argv[1], "-u") == 0) {
100 for (i = 1; i <= 2; ++i) {
101 sprintf(mpath, "/cifs%d", i);
102 umount(mpath);
104 modprobe_r("cifs");
105 notice_set("cifs", "");
106 return 0;
110 usage_exit(argv[0], "-m|-u");
111 return 1;