Merge branch 'org.openembedded.dev' of git://git.openembedded.net/openembedded into...
[vuplus_openembedded] / packages / packagekit / files / racing_condition.patch
1 diff --git a/backends/opkg/pk-backend-opkg.c b/backends/opkg/pk-backend-opkg.c
2 index a7b5244..9e71b90 100644
3 --- a/backends/opkg/pk-backend-opkg.c
4 +++ b/backends/opkg/pk-backend-opkg.c
5 @@ -20,6 +20,7 @@
6   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7   */
8  
9 +#include <unistd.h>
10  #include <gmodule.h>
11  #include <glib.h>
12  #include <string.h>
13 @@ -47,6 +48,22 @@ typedef struct {
14         PkBackend *backend;
15  } SearchParams;
16  
17 +static int __opkg_lock__ = 0;
18 +static inline void opkg_thread_lock() 
19 +{
20 +       while ( __opkg_lock__ ) 
21 +               usleep(500);
22 +       __opkg_lock__ = 1;
23 +}
24 +
25 +static inline void opkg_thread_unlock() 
26 +{
27 +       if (! __opkg_lock__ ) {
28 +               pk_debug("Warning: opkg_thread_unlock  without lock before!!\n");
29 +       }
30 +       __opkg_lock__ = 0;
31 +}
32 +
33  static void
34  opkg_unknown_error (PkBackend *backend, gint error_code, gchar *failed_cmd)
35  {
36 @@ -236,6 +253,7 @@ backend_refresh_cache_thread (PkBackend *backend)
37                         opkg_unknown_error (backend, ret, "Refreshing cache");
38         }
39         pk_backend_finished (backend);
40 +       opkg_thread_unlock();
41  
42         return (ret == 0);
43  }
44 @@ -246,6 +264,7 @@ backend_refresh_cache_thread (PkBackend *backend)
45  static void
46  backend_refresh_cache (PkBackend *backend, gboolean force)
47  {
48 +       opkg_thread_lock();
49         pk_backend_set_status (backend, PK_STATUS_ENUM_REFRESH_CACHE);
50         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
51  
52 @@ -260,15 +279,26 @@ backend_refresh_cache (PkBackend *backend, gboolean force)
53  static void
54  pk_opkg_package_list_cb (opkg_t *opkg, opkg_package_t *pkg, void *data)
55  {
56 -       SearchParams *params = (SearchParams*) data;
57 +       SearchParams *params;
58         gchar *uid;
59         gchar *haystack;
60         gint status, match;
61 -       PkFilterEnum filters = params->filters;
62 +       PkFilterEnum filters;
63  
64 +       if (!opkg) {
65 +               pk_debug("opkg is NULL!!\n");
66 +               return;
67 +       }
68 +       if (!data) {
69 +               pk_debug("data is NULL!!\n");
70 +               return;
71 +       }
72         if (!pkg->name)
73                 return;
74  
75 +       params = (SearchParams*) data;
76 +       filters = params->filters;
77 +
78         switch (params->search_type)
79         {
80                 case SEARCH_NAME:
81 @@ -336,6 +366,7 @@ backend_search_thread (PkBackend *backend)
82         opkg_list_packages (opkg, pk_opkg_package_list_cb, params);
83  
84         pk_backend_finished (params->backend);
85 +       opkg_thread_unlock();
86  
87         g_free (params->needle);
88         g_free (params);
89 @@ -348,6 +379,7 @@ backend_search_name (PkBackend *backend, PkFilterEnum filters, const gchar *sear
90  {
91         SearchParams *params;
92  
93 +       opkg_thread_lock();
94  
95         pk_backend_set_status (backend, PK_STATUS_ENUM_QUERY);
96         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
97 @@ -370,6 +402,7 @@ backend_search_description (PkBackend *backend, PkFilterEnum filters, const gcha
98  {
99         SearchParams *params;
100  
101 +       opkg_thread_lock();
102  
103         pk_backend_set_status (backend, PK_STATUS_ENUM_QUERY);
104         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
105 @@ -389,6 +422,7 @@ backend_search_group (PkBackend *backend, PkFilterEnum filters, const gchar *sea
106  {
107         SearchParams *params;
108  
109 +       opkg_thread_lock();
110  
111         pk_backend_set_status (backend, PK_STATUS_ENUM_QUERY);
112         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
113 @@ -431,12 +465,15 @@ backend_install_packages_thread (PkBackend *backend)
114         }
115  
116         pk_backend_finished (backend);
117 +       opkg_thread_unlock();
118         return (err == 0);
119  }
120  
121  static void
122  backend_install_packages (PkBackend *backend, gchar **package_ids)
123  {
124 +       opkg_thread_lock();
125 +
126         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
127         pk_backend_set_status (backend, PK_STATUS_ENUM_INSTALL);
128  
129 @@ -493,6 +530,7 @@ backend_remove_packages_thread (PkBackend *backend)
130         }
131  
132         pk_backend_finished (backend);
133 +       opkg_thread_unlock();
134         return (err == 0);
135  }
136  
137 @@ -500,6 +538,8 @@ static void
138  backend_remove_packages (PkBackend *backend, gchar **package_ids, gboolean allow_deps, gboolean autoremove)
139  {
140         gpointer *params;
141 +       
142 +       opkg_thread_lock();
143  
144         pk_backend_set_status (backend, PK_STATUS_ENUM_REMOVE);
145         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
146 @@ -540,12 +580,15 @@ backend_update_system_thread (PkBackend *backend)
147                 opkg_unknown_error (backend, err, "Upgrading system");
148  
149         pk_backend_finished (backend);
150 +       opkg_thread_unlock();
151         return (err != 0);
152  }
153  
154  static void
155  backend_update_system (PkBackend *backend)
156  {
157 +       opkg_thread_lock();
158 +
159         pk_backend_set_status (backend, PK_STATUS_ENUM_UPDATE);
160         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
161  
162 @@ -571,6 +614,7 @@ backend_update_package_thread (PkBackend *backend)
163                                 "Package not found");
164                 pk_package_id_free (pi);
165                 pk_backend_finished (backend);
166 +               opkg_thread_unlock();
167                 return FALSE;
168         }
169  
170 @@ -581,6 +625,7 @@ backend_update_package_thread (PkBackend *backend)
171  
172         pk_package_id_free (pi);
173         pk_backend_finished (backend);
174 +       opkg_thread_unlock();
175         return (err != 0);
176  }
177  
178 @@ -589,10 +634,12 @@ backend_update_packages (PkBackend *backend, gchar **package_ids)
179  {
180         gint i;
181  
182 -       pk_backend_set_status (backend, PK_STATUS_ENUM_UPDATE);
183 -       pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
184 -
185         for (i = 0; package_ids[i]; i++) {
186 +
187 +               opkg_thread_lock();
188 +
189 +               pk_backend_set_status (backend, PK_STATUS_ENUM_UPDATE);
190 +               pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
191                 pk_backend_set_string (backend, "pkgid", package_ids[i]);
192                 pk_backend_thread_create (backend, backend_update_package_thread);
193         }
194 @@ -626,12 +673,14 @@ backend_get_updates_thread (PkBackend *backend)
195  {
196         opkg_list_upgradable_packages (opkg, pk_opkg_list_upgradable_cb, backend);
197         pk_backend_finished (backend);
198 +       opkg_thread_unlock();
199         return TRUE;
200  }
201  
202  static void
203  backend_get_updates (PkBackend *backend, PkFilterEnum filters)
204  {
205 +       opkg_thread_lock();
206         pk_backend_set_status (backend, PK_STATUS_ENUM_UPDATE);
207         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
208  
209 @@ -672,6 +721,7 @@ backend_get_details_thread (PkBackend *backend)
210         {
211                 pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_ID_INVALID, "invalid package id");
212                 pk_backend_finished (backend);
213 +               opkg_thread_unlock();
214                 return FALSE;
215         }
216  
217 @@ -683,6 +733,7 @@ backend_get_details_thread (PkBackend *backend)
218         {
219                 pk_backend_error_code (backend, PK_ERROR_ENUM_PACKAGE_NOT_FOUND, NULL);
220                 pk_backend_finished (backend);
221 +               opkg_thread_unlock();
222                 return FALSE;
223         }
224  
225 @@ -698,15 +749,18 @@ backend_get_details_thread (PkBackend *backend)
226         }
227  
228         pk_backend_details (backend, newid, NULL, group, pkg->description, pkg->url, pkg->size);
229 +       pk_backend_finished (backend);
230 +       opkg_thread_unlock();
231 +
232         g_free (newid);
233         opkg_package_free(pkg);
234 -       pk_backend_finished (backend);
235         return TRUE;
236  }
237  
238  static void
239  backend_get_details (PkBackend *backend, const gchar *package_id)
240  {
241 +       opkg_thread_lock();
242         pk_backend_set_percentage (backend, PK_BACKEND_PERCENTAGE_INVALID);
243         pk_backend_thread_create (backend, backend_get_details_thread);
244  }