Tomato 1.28
[tomato.git] / release / src / router / rc / cifs.c
blob3deb163ee809bda539fd18b61018ba1d70b7bf5f
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;
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) != 6) || (*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);
67 sprintf(mpath, "/cifs%d", i);
68 umount(mpath);
69 if (mount("-", mpath, "cifs", MS_NOATIME|MS_NODIRATIME, opt) != 0) continue;
70 done[i] = 1;
71 if (try > 12) try = 12; // 1 min
73 if (*exec) {
74 chdir(mpath);
75 exargv[0] = exec;
76 exargv[1] = NULL;
77 _eval(exargv, NULL, 0, &pid);
80 if ((done[1]) && (done[2])) {
81 notice_set("cifs", "");
82 return 0;
85 sleep(5 * ++try);
86 if (try == 24) { // 2 min
87 sprintf(s, "Error mounting CIFS #%s. Still trying... ", (done[1] == done[2]) ? "1 and #2" : ((done[1] == 0) ? "1" : "2"));
88 notice_set("cifs", s);
90 else if (try > 180) { // 15 mins
91 try = 180;
95 return 1;
97 if (strcmp(argv[1], "-u") == 0) {
98 for (i = 1; i <= 2; ++i) {
99 sprintf(mpath, "/cifs%d", i);
100 umount(mpath);
102 modprobe_r("cifs");
103 notice_set("cifs", "");
104 return 0;
108 usage_exit(argv[0], "-m|-u");
109 return 1;