add busybox printf patch from #2021
[openwrt.git] / package / busybox / patches / 999-insmod2.6_search.patch
1 --- busybox/modutils/insmod.c   2007-05-11 12:10:43.000000000 +0200
2 +++ busybox/modutils/insmod.c   2007-05-11 12:12:15.000000000 +0200
3 @@ -75,6 +75,30 @@
4  extern int insmod_ng_main( int argc, char **argv);
5  #endif
6  
7 +static char *m_filename;
8 +static char *m_fullName;
9 +#define _PATH_MODULES  "/lib/modules"
10 +
11 +static int check_module_name_match(const char *filename, struct stat *statbuf,
12 +                                  void *userdata, int depth)
13 +{
14 +       char *fullname = (char *) userdata;
15 +
16 +       if (fullname[0] == '\0')
17 +               return FALSE;
18 +       else {
19 +               char *tmp, *tmp1 = xstrdup(filename);
20 +               tmp = bb_get_last_path_component(tmp1);
21 +               if (strcmp(tmp, fullname) == 0) {
22 +                       free(tmp1);
23 +                       /* Stop searching if we find a match */
24 +                       m_filename = xstrdup(filename);
25 +                       return FALSE;
26 +               }
27 +               free(tmp1);
28 +       }
29 +       return TRUE;
30 +}
31  
32  #if ENABLE_FEATURE_2_4_MODULES
33  
34 @@ -680,7 +704,6 @@
35  #endif
36  
37  
38 -#define _PATH_MODULES  "/lib/modules"
39  enum { STRVERSIONLEN = 64 };
40  
41  /*======================================================================*/
42 @@ -793,37 +816,6 @@
43  static int n_ext_modules_used;
44  extern int delete_module(const char *);
45  
46 -static char *m_filename;
47 -static char *m_fullName;
48 -
49 -
50 -/*======================================================================*/
51 -
52 -
53 -static int check_module_name_match(const char *filename, struct stat *statbuf,
54 -                               void *userdata, int depth)
55 -{
56 -       char *fullname = (char *) userdata;
57 -
58 -       if (fullname[0] == '\0')
59 -               return FALSE;
60 -       else {
61 -               char *tmp, *tmp1 = xstrdup(filename);
62 -               tmp = bb_get_last_path_component(tmp1);
63 -               if (strcmp(tmp, fullname) == 0) {
64 -                       free(tmp1);
65 -                       /* Stop searching if we find a match */
66 -                       m_filename = xstrdup(filename);
67 -                       return FALSE;
68 -               }
69 -               free(tmp1);
70 -       }
71 -       return TRUE;
72 -}
73 -
74 -
75 -/*======================================================================*/
76 -
77  static struct obj_file *arch_new_file(void)
78  {
79         struct arch_file *f;
80 @@ -4265,14 +4257,97 @@
81         long ret;
82         size_t len;
83         void *map;
84 -       char *filename, *options;
85 +       char *options, *tmp;
86 +       struct stat st;
87 +#if ENABLE_FEATURE_CLEAN_UP
88 +       FILE *fp = 0;
89 +#else
90 +       FILE *fp;
91 +#endif
92 +       int k_version = 0;
93 +       struct utsname myuname;
94  
95 -       filename = *++argv;
96 -       if (!filename)
97 +       if (argc < 2)
98                 bb_show_usage();
99  
100 +#if !ENABLE_FEATURE_2_4_MODULES
101 +       /* Grab the module name */
102 +       tmp = basename(xstrdup(argv[1]));
103 +       len = strlen(tmp);
104 +
105 +       if (uname(&myuname) == 0) {
106 +               if (myuname.release[0] == '2') {
107 +                       k_version = myuname.release[2] - '0';
108 +               }
109 +       }
110 +
111 +       if (len > 3 && tmp[len - 3] == '.' && tmp[len - 2] == 'k' && tmp[len - 1] == 'o') {
112 +               len -= 3;
113 +               tmp[len] = '\0';
114 +          }
115 +
116 +
117 +                  m_fullName = xasprintf("%s.ko", tmp);
118 +
119 +                  /* Get a filedesc for the module.  Check we we have a complete path */
120 +                  if (stat(argv[1], &st) < 0 || !S_ISREG(st.st_mode)
121 +                                    || (fp = fopen(argv[1], "r")) == NULL
122 +                     ) {
123 +               /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
124 +               * but do not error out yet if we fail to find it... */
125 +                          if (k_version) {     /* uname succeedd */
126 +                                  char *module_dir;
127 +                                  char *tmdn;
128 +                                  char real_module_dir[FILENAME_MAX];
129 +
130 +                                  tmdn = concat_path_file(_PATH_MODULES, myuname.release);
131 +                       /* Jump through hoops in case /lib/modules/`uname -r`
132 +                                  * is a symlink.  We do not want recursive_action to
133 +                                  * follow symlinks, but we do want to follow the
134 +                                  * /lib/modules/`uname -r` dir, So resolve it ourselves
135 +                       * if it is a link... */
136 +                                  if (realpath(tmdn, real_module_dir) == NULL)
137 +                                          module_dir = tmdn;
138 +                                  else
139 +                                          module_dir = real_module_dir;
140 +                                  recursive_action(module_dir, TRUE, FALSE, FALSE,
141 +                                                  check_module_name_match, 0, m_fullName, 0);
142 +                                  free(tmdn);
143 +                          }
144 +
145 +                          /* Check if we have found anything yet */
146 +                          if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL)) {
147 +                                  char module_dir[FILENAME_MAX];
148 +
149 +                                  free(m_filename);
150 +                                  m_filename = 0;
151 +                                  if (realpath (_PATH_MODULES, module_dir) == NULL)
152 +                                          strcpy(module_dir, _PATH_MODULES);
153 +                       /* No module found under /lib/modules/`uname -r`, this
154 +                       * time cast the net a bit wider.  Search /lib/modules/ */
155 +                                  if (!recursive_action(module_dir, TRUE, FALSE, FALSE,
156 +                                       check_module_name_match, 0, m_fullName, 0)
157 +                                     ) {
158 +                                          if (m_filename == 0
159 +                                                                               || ((fp = fopen(m_filename, "r")) == NULL)
160 +                                             ) {
161 +                                                  bb_error_msg("%s: no module by that name found", m_fullName);
162 +#if ENABLE_FEATURE_CLEAN_UP
163 +                                                       if(fp)
164 +                                                               fclose(fp);
165 +                                                       free(m_filename);
166 +#endif
167 +                                             }
168 +                                     } else
169 +                                             bb_error_msg_and_die("%s: no module by that name found", m_fullName);
170 +                          }
171 +                     } else
172 +                             m_filename = xstrdup(argv[1]);
173 +#endif
174 +
175         /* Rest is options */
176         options = xstrdup("");
177 +       argv++;
178         while (*++argv) {
179                 int optlen = strlen(options);
180                 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
181 @@ -4300,13 +4375,13 @@
182         }
183  #else
184         len = MAXINT(ssize_t);
185 -       map = xmalloc_open_read_close(filename, &len);
186 +       map = xmalloc_open_read_close(m_filename, &len);
187  #endif
188  
189         ret = syscall(__NR_init_module, map, len, options);
190         if (ret != 0) {
191                 bb_perror_msg_and_die("cannot insert '%s': %s (%li)",
192 -                               filename, moderror(errno), ret);
193 +                               m_filename, moderror(errno), ret);
194         }
195  
196         return 0;