0a12e5dfca78c68e3d15025c622c2f49d8142a05
[framework/web/wrt-installer.git] / src / wrt-installer / wrt-installer.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /* @file    wrt-installer.cpp
17  * @version 1.0
18  * @brief   Implementation file for installer
19  */
20
21 #include "wrt-installer.h"
22 #include "plugin_utils.h"
23
24 #include <string>
25 #include <cstring>
26 #include <cstdlib>
27 #include <dirent.h>
28 #include <sys/resource.h>
29
30 #include <dpl/log/log.h>
31 #include <dpl/optional.h>
32 #include <dpl/optional_typedefs.h>
33 #include <dpl/exception.h>
34 #include <dpl/wrt-dao-ro/global_config.h>
35 #include <dpl/wrt-dao-ro/config_parser_data.h>
36 #include <dpl/string.h>
37 #include <dpl/abstract_waitable_input_adapter.h>
38 #include <dpl/abstract_waitable_output_adapter.h>
39 #include <dpl/zip_input.h>
40 #include <dpl/binary_queue.h>
41 #include <dpl/copy.h>
42 #include <dpl/errno_string.h>
43 #include <dpl/utils/wrt_global_settings.h>
44 #include <parser_runner.h>
45 #include <widget_parser.h>
46 #include <root_parser.h>
47
48 #include <Elementary.h>
49
50 #include <pkg-manager/pkgmgr_signal_interface.h>
51 #include <pkg-manager/pkgmgr_signal_dummy.h>
52 #include <pkg-manager/pkgmgr_signal.h>
53
54 using namespace WrtDB;
55
56 namespace { // anonymous
57 const char * const PKGMGR_INSTALL_MSG = "Install widget";
58 const char * const PKGMGR_UNINSTALL_MSG = "Uninstall widget";
59
60 const char * const CONFIG_XML = "config.xml";
61 const char * const HYBRID_CONFIG_XML = "res/wgt/config.xml";
62
63 const unsigned int NOFILE_CNT_FOR_INSTALLER = 9999;
64
65 struct free_deleter
66 {
67     void operator()(void* x)
68     {
69         free(x);
70     }
71 };
72
73 struct PluginInstallerData
74 {
75     void* wrtInstaller;
76     std::string pluginPath;
77 };
78 } // namespace anonymous
79
80 WrtInstaller::WrtInstaller(int argc, char **argv) :
81     Application(argc, argv, "backend", false),
82     DPL::TaskDecl<WrtInstaller>(this),
83     m_installMode(WRT_INSTALL_MODE_UNKNOWN),
84     m_packagePath(),
85     m_initialized(false),
86     m_numPluginsToInstall(0),
87     m_totalPlugins(0),
88     m_returnStatus(-1),
89     m_installByPkgmgr(false),
90     m_quiet(true),
91     m_startupPluginInstallation(false)
92 {
93     Touch();
94     LogDebug("App Created");
95 }
96
97 WrtInstaller::~WrtInstaller()
98 {
99     LogDebug("App Finished");
100 }
101
102 int WrtInstaller::getReturnStatus() const
103 {
104     if (!m_returnStatus) {
105         return RE_SUCCESS;
106     } else {
107         return RE_FAIL;
108     }
109 }
110
111 void WrtInstaller::OnStop()
112 {
113     LogInfo("Stopping Dummy Client");
114 }
115
116 void WrtInstaller::OnCreate()
117 {
118     LogInfo("Creating DummyClient");
119     fprintf(stderr,
120             "===========================================================\n");
121     fprintf(stderr, "# wrt-installer #\n");
122     fprintf(stderr, "# argc [%d]\n", m_argc);
123     fprintf(stderr, "# argv[0] = [%s]\n", m_argv[0]);
124     fprintf(stderr, "# argv[1] = [%s]\n", m_argv[1]);
125     fprintf(stderr, "# argv[2] = [%s]\n", m_argv[2]);
126     fprintf(stderr,
127             "===========================================================\n");
128
129     AddStep(&WrtInstaller::initStep);
130
131     std::string arg = m_argv[0];
132
133     pkgmgrSignalInterface =
134         std::static_pointer_cast<PackageManager::IPkgmgrSignal>(
135             std::shared_ptr<PackageManager::PkgmgrSignalDummy>(
136                 new PackageManager::PkgmgrSignalDummy()
137                 )
138             );
139
140     if (arg.empty()) {
141         return showHelpAndQuit();
142     }
143
144     installNewPlugins();
145
146     if (arg.find("wrt-installer") != std::string::npos) {
147         if (m_argc <= 1) {
148             return showHelpAndQuit();
149         }
150
151         arg = m_argv[1];
152         if (arg == "-h" || arg == "--help") {
153             if (m_argc != 2) {
154                 return showHelpAndQuit();
155             }
156
157             // Just show help
158             return showHelpAndQuit();
159         } else if (arg == "-p" || arg == "--install-plugins") {
160             if (m_argc != 2) {
161                 return showHelpAndQuit();
162             }
163             if (!m_startupPluginInstallation) {
164                 AddStep(&WrtInstaller::installPluginsStep);
165             } else {
166                 LogInfo("Plugin installation alredy started");
167             }
168         } else if (arg == "-i" || arg == "--install") {
169             if (m_argc != 3) {
170                 return showHelpAndQuit();
171             }
172
173             struct stat info;
174             if (-1 != stat(m_argv[2], &info) && S_ISDIR(info.st_mode)) {
175                 LogInfo("Installing package directly from directory");
176                 m_installMode = WRT_INSTALL_MODE_INSTALL_DIRECTORY;
177             } else {
178                 LogInfo("Installing from regular location");
179                 m_installMode = WRT_INSTALL_MODE_INSTALL_WGT;
180             }
181             m_packagePath = m_argv[2];
182             AddStep(&WrtInstaller::installStep);
183         } else if (arg == "-il" || arg == "--install-preload") {
184             if (m_argc != 3) {
185                 return showHelpAndQuit();
186             }
187             m_packagePath = m_argv[2];
188             m_installMode = WRT_INSTALL_MODE_INSTALL_PRELOAD;
189             AddStep(&WrtInstaller::installStep);
190         } else if (arg == "-un" || arg == "--uninstall-name") {
191             if (m_argc != 3) {
192                 return showHelpAndQuit();
193             }
194             m_name = m_argv[2];
195             AddStep(&WrtInstaller::uninstallPkgNameStep);
196         } else if (arg == "-up" || arg == "--uninstall-packagepath") {
197             if (m_argc != 3) {
198                 return showHelpAndQuit();
199             }
200             m_packagePath = m_argv[2];
201             AddStep(&WrtInstaller::unistallWgtFileStep);
202         } else if (arg == "-r" || arg == "--reinstall") {
203             if (m_argc != 3) {
204                 return showHelpAndQuit();
205             }
206             LogInfo("Installing package directly from directory");
207             m_installMode = WRT_INSTALL_MODE_REINSTALL;
208             m_packagePath = m_argv[2];
209             AddStep(&WrtInstaller::installStep);
210         } else {
211             return showHelpAndQuit();
212         }
213     } else if (arg.find("backend") != std::string::npos) {
214         using namespace PackageManager;
215         m_installByPkgmgr = true;
216
217         auto pkgmgrSignal = std::shared_ptr<PackageManager::PkgmgrSignal>(
218                 new PackageManager::PkgmgrSignal()
219                 );
220
221         pkgmgrSignal->initialize(m_argc, m_argv);
222         m_quiet = pkgmgrSignal->isNoPopupRequired();
223         LogDebug("backend m_quiet" << m_quiet);
224
225         int reqType = pkgmgrSignal->getRequestedType();
226
227         pkgmgrSignalInterface =
228             std::static_pointer_cast<PackageManager::IPkgmgrSignal>(
229                 pkgmgrSignal);
230         switch (reqType) {
231         case PKGMGR_REQ_INSTALL:
232             m_packagePath = m_argv[4];
233             struct stat info;
234             if (-1 != stat(m_argv[4], &info) && S_ISDIR(info.st_mode)) {
235                 LogInfo("Installing package directly from directory");
236                 m_installMode = WRT_INSTALL_MODE_INSTALL_DIRECTORY;
237             } else {
238                 LogInfo("Installing from regular location");
239                 m_installMode = WRT_INSTALL_MODE_INSTALL_WGT;
240             }
241             AddStep(&WrtInstaller::installStep);
242             break;
243         case PKGMGR_REQ_UNINSTALL:
244             m_name = m_argv[4];
245             AddStep(&WrtInstaller::uninstallPkgNameStep);
246             break;
247         case PKGMGR_REQ_REINSTALL:
248             m_packagePath = m_argv[4];
249             m_installMode = WRT_INSTALL_MODE_REINSTALL;
250             AddStep(&WrtInstaller::installStep);
251             break;
252         default:
253             LogDebug("Not available type");
254             break;
255         }
256     }
257
258     AddStep(&WrtInstaller::shutdownStep);
259     DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>::
260         PostEvent(
261         WRTInstallerNS::NextStepEvent());
262 }
263
264 void WrtInstaller::OnReset(bundle* /*b*/)
265 {
266     LogDebug("OnReset");
267 }
268
269 void WrtInstaller::OnTerminate()
270 {
271     LogDebug("Wrt Shutdown now");
272     PluginUtils::unlockPluginInstallation();
273     if (m_initialized) {
274         wrt_installer_shutdown();
275     }
276 }
277
278 void WrtInstaller::showHelpAndQuit()
279 {
280     printf("Usage: wrt-installer [OPTION]... [WIDGET: ID/NAME/PATH]...\n"
281            "Operate with WebRuntime daemon: install, uninstall"
282            " and launch widgets.\n"
283            "Query list of installed widgets and setup up debugging support.\n"
284            "\n"
285            "Exactly one option must be selected.\n"
286            "Mandatory arguments to long options are mandatory for short "
287            "options too.\n"
288            "  -h,    --help                                 show this help\n"
289            "  -p,    --install-plugins                      install plugins\n"
290            "  -i,    --install                              "
291            "install or update widget package for given path\n"
292            "  -un,   --uninstall-name                       "
293            "uninstall widget for given package name\n"
294            "  -up,   --uninstall-packagepath                "
295            "uninstall widget for given package file path\n"
296            "  -r,    --reinstall                            "
297            "reinstall web application\n"
298            "\n");
299
300     Quit();
301 }
302
303 void WrtInstaller::OnEventReceived(const WRTInstallerNS::QuitEvent& /*event*/)
304 {
305     LogDebug("Quiting");
306
307     if (m_initialized) {
308         LogDebug("Wrt Shutdown now");
309         SwitchToStep(&WrtInstaller::shutdownStep);
310         DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>::
311             PostEvent(
312             WRTInstallerNS::NextStepEvent());
313     } else {
314         LogDebug("Quiting application");
315         return Quit();
316     }
317 }
318
319 void WrtInstaller::OnEventReceived(
320     const WRTInstallerNS::NextStepEvent& /*event*/)
321 {
322     LogDebug("Executing next step");
323     NextStep();
324 }
325
326 void WrtInstaller::OnEventReceived(
327     const WRTInstallerNS::InstallPluginEvent& /*event*/)
328 {
329     PluginInstallerData* privateData = new PluginInstallerData;
330     privateData->wrtInstaller = this;
331
332     if (!(*m_pluginsPaths).empty()) {
333         privateData->pluginPath = (*m_pluginsPaths).front();
334         (*m_pluginsPaths).pop_front();
335
336         wrt_install_plugin(privateData->pluginPath.c_str(),
337                            static_cast<void*>(privateData),
338                            &staticWrtPluginInstallationCallback,
339                            &staticWrtPluginInstallProgressCb);
340     } else {
341         delete privateData;
342     }
343 }
344
345 void WrtInstaller::initStep()
346 {
347     wrt_installer_init(this, staticWrtInitCallback);
348 }
349
350 void WrtInstaller::installStep()
351 {
352     LogDebug("Installing widget ...");
353     std::unique_ptr<char, free_deleter> packagePath(canonicalize_file_name(
354                                                         m_packagePath.c_str()));
355
356     wrt_install_widget(packagePath ? packagePath.get() : m_packagePath.c_str(),
357                        this, &staticWrtStatusCallback,
358                        (!m_quiet || m_installByPkgmgr)
359                        ? &staticWrtInstallProgressCallback : NULL,
360                        m_installMode,
361                        m_quiet,
362                        pkgmgrSignalInterface);
363 }
364
365 void WrtInstaller::installPluginsStep()
366 {
367     LogDebug("Installing plugins ...");
368     fprintf(stderr, "Installing plugins ...\n");
369
370     if (m_startupPluginInstallation) {
371         LogInfo("Plugin installation started because new plugin package found");
372     } else if (!PluginUtils::lockPluginInstallation()) {
373         LogError("Failed to open plugin installation lock file"
374                  " Plugins are currently installed by other process");
375         staticWrtPluginInstallationCallback(WRT_INSTALLER_ERROR_PLUGIN_INSTALLATION_FAILED,
376                                             this);
377         return;
378     }
379
380     std::string PLUGIN_PATH = std::string(GlobalConfig::GetDevicePluginPath());
381
382     DIR *dir;
383     dir = opendir(PLUGIN_PATH.c_str());
384
385     if (!dir) {
386         return;
387     }
388
389     LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
390
391     std::list<std::string> pluginsPaths;
392     struct dirent libdir;
393     struct dirent *result;
394     int return_code;
395     errno = 0;
396     for (return_code = readdir_r(dir, &libdir, &result);
397             result != NULL && return_code == 0;
398             return_code = readdir_r(dir, &libdir, &result))
399     {
400         if (strcmp(libdir.d_name, ".") == 0 ||
401             strcmp(libdir.d_name, "..") == 0)
402         {
403             continue;
404         }
405
406         std::string path = PLUGIN_PATH;
407         path += "/";
408         path += libdir.d_name;
409
410         struct stat tmp;
411
412         if (stat(path.c_str(), &tmp) == -1) {
413             LogError("Failed to open file" << path);
414             continue;
415         }
416
417         if (!S_ISDIR(tmp.st_mode)) {
418             LogError("Not a directory" << path);
419             continue;
420         }
421
422         pluginsPaths.push_back(path);
423     }
424
425     if (return_code != 0 || errno != 0) {
426         LogError("readdir_r() failed with " << DPL::GetErrnoString());
427     }
428
429     //set nb of plugins to install
430     //this value indicate how many callbacks are expected
431     m_numPluginsToInstall = pluginsPaths.size();
432     LogInfo("Plugins to install: " << m_numPluginsToInstall);
433     m_pluginsPaths = pluginsPaths;
434
435     m_totalPlugins = m_numPluginsToInstall;
436     DPL::Event::ControllerEventHandler<WRTInstallerNS::InstallPluginEvent>
437         ::PostEvent(WRTInstallerNS::InstallPluginEvent());
438
439     if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
440         LogError("Failed to close dir: " << PLUGIN_PATH << " with error: "
441                                          << DPL::GetErrnoString());
442     }
443 }
444
445 void WrtInstaller::uninstallPkgNameStep()
446 {
447     LogDebug("Uninstalling widget ...");
448     LogDebug("Package name : " << m_name);
449     wrt_uninstall_widget(m_name.c_str(), this, &staticWrtStatusCallback,
450                          (!m_quiet || m_installByPkgmgr)
451                          ? &staticWrtUninstallProgressCallback : NULL,
452                          pkgmgrSignalInterface);
453 }
454
455 void WrtInstaller::unistallWgtFileStep()
456 {
457     LogDebug("Uninstalling widget ...");
458
459     Try {
460         // Parse config
461         ParserRunner parser;
462         ConfigParserData configInfo;
463
464         // Open zip file
465         std::unique_ptr<DPL::ZipInput> zipFile(
466             new DPL::ZipInput(m_packagePath));
467         std::unique_ptr<DPL::ZipInput::File> configFile;
468
469         Try {
470             // Open config.xml file
471             configFile.reset(zipFile->OpenFile(CONFIG_XML));
472         }
473         Catch(DPL::ZipInput::Exception::OpenFileFailed)
474         {
475             // Open config.xml file for hybrid
476             configFile.reset(zipFile->OpenFile(HYBRID_CONFIG_XML));
477         }
478
479         // Extract config
480         DPL::BinaryQueue buffer;
481         DPL::AbstractWaitableInputAdapter inputAdapter(configFile.get());
482         DPL::AbstractWaitableOutputAdapter outputAdapter(&buffer);
483         DPL::Copy(&inputAdapter, &outputAdapter);
484         parser.Parse(&buffer,
485                      ElementParserPtr(
486                          new RootParser<WidgetParser>(configInfo,
487                                                       DPL::FromUTF32String(
488                                                           L"widget"))));
489
490         DPL::OptionalString pkgId = configInfo.tizenPkgId;
491         if (!pkgId.IsNull()) {
492             LogDebug("Pkgid from packagePath : " << pkgId);
493             wrt_uninstall_widget(
494                 DPL::ToUTF8String(*pkgId).c_str(), this, &staticWrtStatusCallback,
495                 !m_quiet ? &staticWrtUninstallProgressCallback
496                 : NULL,
497                 pkgmgrSignalInterface);
498         } else {
499             LogError("Fail to uninstalling widget... ");
500             m_returnStatus = -1;
501             DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::
502                 PostEvent(
503                 WRTInstallerNS::QuitEvent());
504         }
505     }
506     Catch(DPL::ZipInput::Exception::OpenFailed)
507     {
508         LogError("Failed to open widget package");
509         printf("failed: widget package does not exist\n");
510         m_returnStatus = -1;
511         DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::
512             PostEvent(
513             WRTInstallerNS::QuitEvent());
514     }
515     Catch(DPL::ZipInput::Exception::OpenFileFailed)
516     {
517         printf("failed: widget config file does not exist\n");
518         LogError("Failed to open config.xml file");
519         m_returnStatus = -1;
520         DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::
521             PostEvent(
522             WRTInstallerNS::QuitEvent());
523     }
524     Catch(ElementParser::Exception::ParseError)
525     {
526         printf("failed: can not parse config file\n");
527         LogError("Failed to parse config.xml file");
528         m_returnStatus = -1;
529         DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::
530             PostEvent(
531             WRTInstallerNS::QuitEvent());
532     }
533 }
534
535 void WrtInstaller::shutdownStep()
536 {
537     LogDebug("Closing Wrt connection ...");
538     if (m_initialized) {
539         wrt_installer_shutdown();
540         m_initialized = false;
541         DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::
542             PostEvent(
543             WRTInstallerNS::QuitEvent());
544     }
545 }
546
547 void WrtInstaller::staticWrtInitCallback(WrtErrStatus status,
548                                          void* userdata)
549 {
550     WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
551     Assert(This);
552
553     if (status == WRT_SUCCESS) {
554         LogDebug("Init succesfull");
555         This->m_initialized = true;
556         This->m_returnStatus = 0;
557
558         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>
559             ::PostEvent(WRTInstallerNS::NextStepEvent());
560     } else {
561         LogError("Init unsuccesfull");
562         This->m_returnStatus = -1;
563         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>::
564             PostEvent(
565             WRTInstallerNS::QuitEvent());
566     }
567 }
568
569 void WrtInstaller::staticWrtStatusCallback(std::string tizenId,
570                                            WrtErrStatus status,
571                                            void* userdata)
572 {
573     WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
574     Assert(This);
575
576     Step current = This->GetCurrentStep();
577     DPL::String resultMsg;
578     std::string printMsg;
579
580     if (current == &WrtInstaller::installStep) {
581         resultMsg = DPL::FromUTF8String(PKGMGR_INSTALL_MSG);
582         printMsg = "installation";
583     } else if (current == &WrtInstaller::uninstallPkgNameStep ||
584                current == &WrtInstaller::unistallWgtFileStep)
585     {
586         resultMsg = DPL::FromUTF8String(PKGMGR_UNINSTALL_MSG);
587         printMsg = "uninstallation";
588     }
589
590     if (WRT_SUCCESS != status) {
591         // Failure
592         LogError("Step failed");
593         This->m_returnStatus = -1;
594
595         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>
596             ::PostEvent(WRTInstallerNS::QuitEvent());
597
598         switch (status) {
599             case WRT_INSTALLER_ERROR_PACKAGE_NOT_FOUND:
600                 This->m_returnStatus = 1; //this status is specific
601                 fprintf(stderr, "## wrt-installer : %s %s has failed - widget package does not exist\n",
602                         tizenId.c_str(), printMsg.c_str());
603                 break;
604
605             case WRT_INSTALLER_ERROR_PACKAGE_INVALID:
606                 This->m_returnStatus = 1; //this status is specific
607                 fprintf(stderr, "## wrt-installer : %s %s has failed - invalid widget package\n",
608                         tizenId.c_str(), printMsg.c_str());
609                 break;
610
611             case WRT_INSTALLER_ERROR_PACKAGE_LOWER_VERSION:
612                 This->m_returnStatus = 1; //this status is specific
613                 fprintf(stderr, "## wrt-installer : %s %s has failed - given"
614                         " version is lower than existing version\n",
615                         tizenId.c_str(), printMsg.c_str());
616                 break;
617
618             case WRT_INSTALLER_ERROR_MANIFEST_NOT_FOUND:
619                 This->m_returnStatus = 1; //this status is specific
620                 fprintf(stderr, "## wrt-installer : %s %s has failed - manifest"
621                         " file doesn't find in package.\n",
622                         tizenId.c_str(), printMsg.c_str());
623                 break;
624
625             case WRT_INSTALLER_ERROR_MANIFEST_INVALID:
626                 This->m_returnStatus = 1; //this status is specific
627                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
628                         "invalid manifestx.xml\n",
629                         tizenId.c_str(), printMsg.c_str());
630                 break;
631
632             case WRT_INSTALLER_CONFIG_NOT_FOUND:
633                 This->m_returnStatus = 1; //this status is specific
634                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
635                         "config.xml does not exist\n",
636                         tizenId.c_str(), printMsg.c_str());
637                 break;
638
639             case WRT_INSTALLER_ERROR_CONFIG_INVALID:
640                 This->m_returnStatus = 1; //this status is specific
641                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
642                         "invalid config.xml\n",
643                         tizenId.c_str(), printMsg.c_str());
644                 break;
645
646             case WRT_INSTALLER_ERROR_SIGNATURE_NOT_FOUND:
647                 This->m_returnStatus = 1; //this status is specific
648                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
649                         "signature doesn't exist in package.\n",
650                         tizenId.c_str(), printMsg.c_str());
651                 break;
652
653             case WRT_INSTALLER_ERROR_SIGNATURE_INVALID:
654                 This->m_returnStatus = 1; //this status is specific
655                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
656                         "invalid signature.\n",
657                         tizenId.c_str(), printMsg.c_str());
658                 break;
659
660             case WRT_INSTALLER_ERROR_SIGNATURE_VERIFICATION_FAILED:
661                 This->m_returnStatus = 1; //this status is specific
662                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
663                         "signature verification failed.\n",
664                         tizenId.c_str(), printMsg.c_str());
665                 break;
666
667             case WRT_INSTALLER_ERROR_ROOT_CERTIFICATE_NOT_FOUND:
668                 This->m_returnStatus = 1; //this status is specific
669                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
670                         "root certificate could not find.\n",
671                         tizenId.c_str(), printMsg.c_str());
672                 break;
673
674             case WRT_INSTALLER_ERROR_CERTIFICATION_INVAID:
675                 This->m_returnStatus = 1; //this status is specific
676                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
677                         "invalid certification.\n",
678                         tizenId.c_str(), printMsg.c_str());
679                 break;
680
681             case WRT_INSTALLER_ERROR_CERTIFICATE_CHAIN_VERIFICATION_FAILED:
682                 This->m_returnStatus = 1; //this status is specific
683                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
684                         "certificate chain verification failed.\n",
685                         tizenId.c_str(), printMsg.c_str());
686                 break;
687
688             case WRT_INSTALLER_ERROR_CERTIFICATE_EXPIRED:
689                 This->m_returnStatus = 1; //this status is specific
690                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
691                         "certificate expired.\n",
692                         tizenId.c_str(), printMsg.c_str());
693                 break;
694
695             case WRT_INSTALLER_ERROR_INVALID_PRIVILEGE:
696                 This->m_returnStatus = 1; //this status is specific
697                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
698                         "invalid privilege\n",
699                         tizenId.c_str(), printMsg.c_str());
700                 break;
701
702             case WRT_INSTALLER_ERROR_MENU_ICON_NOT_FOUND:
703                 This->m_returnStatus = 1; //this status is specific
704                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
705                         "menu icon could not find\n",
706                         tizenId.c_str(), printMsg.c_str());
707                 break;
708
709             case WRT_INSTALLER_ERROR_FATAL_ERROR:
710                 This->m_returnStatus = 1; //this status is specific
711                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
712                         "fatal error\n",
713                         tizenId.c_str(), printMsg.c_str());
714                 break;
715
716             case WRT_INSTALLER_ERROR_OUT_OF_STORAGE:
717                 This->m_returnStatus = 1; //this status is specific
718                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
719                         "out of storage\n",
720                         tizenId.c_str(), printMsg.c_str());
721                 break;
722
723             case WRT_INSTALLER_ERROR_OUT_OF_MEMORY:
724                 This->m_returnStatus = 1; //this status is specific
725                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
726                         "out of memory\n",
727                         tizenId.c_str(), printMsg.c_str());
728                 break;
729
730             case WRT_INSTALLER_ERROR_PACKAGE_ALREADY_INSTALLED:
731                 This->m_returnStatus = 1; //this status is specific
732                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
733                         "package already installed\n",
734                         tizenId.c_str(), printMsg.c_str());
735                 break;
736
737             case WRT_INSTALLER_ERROR_ACE_CHECK_FAILED:
738                 This->m_returnStatus = 1; //this status is specific
739                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
740                         "ace check failure\n",
741                         tizenId.c_str(), printMsg.c_str());
742                 break;
743
744             case WRT_INSTALLER_ERROR_MANIFEST_CREATE_FAILED:
745                 This->m_returnStatus = 1; //this status is specific
746                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
747                         "to create manifest failed\n",
748                         tizenId.c_str(), printMsg.c_str());
749                 break;
750
751             case WRT_INSTALLER_ERROR_ENCRYPTION_FAILED:
752                 This->m_returnStatus = 1; //this status is specific
753                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
754                         "encryption of resource failed\n",
755                         tizenId.c_str(), printMsg.c_str());
756                 break;
757
758             case WRT_INSTALLER_ERROR_INSTALL_OSP_SERVCIE:
759                 This->m_returnStatus = 1; //this status is specific
760                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
761                         "installation of osp service failed\n",
762                         tizenId.c_str(), printMsg.c_str());
763                 break;
764
765             case WRT_INSTALLER_ERROR_UNINSTALLATION_FAILED:
766                 This->m_returnStatus = 1; //this status is specific
767                 fprintf(stderr, "## wrt-installer : %s %s has failed - "
768                         "widget uninstallation failed\n",
769                         tizenId.c_str(), printMsg.c_str());
770                 break;
771
772
773             case WRT_INSTALLER_ERROR_UNKNOWN:
774                 fprintf(stderr,"## wrt-installer : %s %s has failed - unknown error\n",
775                         tizenId.c_str(), printMsg.c_str());
776                 break;
777
778             default:
779                 break;
780         }
781     } else {
782         fprintf(stderr,
783                 "## wrt-installer : %s %s was successful.\n",
784                 tizenId.c_str(),
785                 printMsg.c_str());
786         LogDebug("Status succesfull");
787         This->m_returnStatus = 0;
788         resultMsg += L" : " + DPL::FromUTF8String(PKGMGR_END_SUCCESS);
789
790         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::
791                                                      NextStepEvent>
792             ::PostEvent(WRTInstallerNS::NextStepEvent());
793     }
794 }
795
796 void WrtInstaller::staticWrtPluginInstallationCallback(WrtErrStatus status,
797                                                        void* userdata)
798 {
799     Assert(userdata);
800
801     PluginInstallerData* data = static_cast<PluginInstallerData*>(userdata);
802
803     WrtInstaller *This = static_cast<WrtInstaller*>(data->wrtInstaller);
804
805     std::string path = std::string(data->pluginPath);
806     delete data;
807
808     This->m_numPluginsToInstall--;
809     LogDebug("Plugins to install: " << This->m_numPluginsToInstall);
810
811     if (This->m_numPluginsToInstall < 1) {
812         LogDebug("All plugins installation completed");
813         fprintf(stderr, "All plugins installation completed.\n");
814
815         //remove installation request
816         if (!PluginUtils::removeInstallationRequiredFlag()) {
817             LogInfo("Failed to remove file initializing plugin installation");
818         }
819
820         //remove lock file
821         if (!PluginUtils::unlockPluginInstallation()) {
822             LogInfo("Failed to remove installation lock");
823         }
824
825         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>
826             ::PostEvent(WRTInstallerNS::NextStepEvent());
827     } else {
828         This->DPL::Event::ControllerEventHandler<WRTInstallerNS::
829                                                      InstallPluginEvent>::
830             PostEvent(
831             WRTInstallerNS::InstallPluginEvent());
832     }
833
834     if (WRT_SUCCESS == status) {
835         This->m_returnStatus = 0;
836         fprintf(stderr,
837                 "## wrt-installer : plugin installation successfull [%s]\n",
838                 path.c_str());
839         LogDebug("One plugin Installation succesfull: " << path);
840         return;
841     }
842
843     // Failure
844     LogWarning("One of the plugins installation failed!: " << path);
845
846     switch (status) {
847     case WRT_INSTALLER_ERROR_PLUGIN_INSTALLATION_FAILED:
848         LogError("failed: plugin installation failed\n");
849         break;
850
851     case WRT_INSTALLER_ERROR_UNKNOWN:
852         LogError("failed: unknown error\n");
853         break;
854
855     default:
856         break;
857     }
858 }
859
860 void WrtInstaller::staticWrtPluginInstallProgressCb(float percent,
861                                                     const char* description,
862                                                     void* userdata)
863 {
864     PluginInstallerData* data = static_cast<PluginInstallerData*>(userdata);
865
866     std::string path = std::string(data->pluginPath);
867
868     LogInfo("Plugin Installation: " << path <<
869             " progress: " << percent <<
870             "description " << description);
871 }
872
873 void WrtInstaller::staticWrtInstallProgressCallback(float percent,
874                                                     const char* description,
875                                                     void* /*userdata*/)
876 {
877     //WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
878     LogInfo(" progress: " << percent <<
879             " description: " << description);
880 }
881 void WrtInstaller::staticWrtUninstallProgressCallback(float percent,
882                                                       const char* description,
883                                                       void* /*userdata*/)
884 {
885     //WrtInstaller *This = static_cast<WrtInstaller*>(userdata);
886     LogInfo(" progress: " << percent <<
887             " description: " << description);
888 }
889
890 void WrtInstaller::showResultCallback(void *data, Evas_Object* /*obj*/,
891                                       void* /*event_info*/)
892 {
893     WrtInstaller *This = static_cast<WrtInstaller*>(data);
894     Assert(This);
895
896     This->DPL::Event::ControllerEventHandler<WRTInstallerNS::NextStepEvent>
897         ::PostEvent(WRTInstallerNS::NextStepEvent());
898 }
899
900 void WrtInstaller::failResultCallback(void *data, Evas_Object* /*obj*/,
901                                       void* /*event_info*/)
902 {
903     WrtInstaller *This = static_cast<WrtInstaller*>(data);
904     Assert(This);
905
906     This->DPL::Event::ControllerEventHandler<WRTInstallerNS::QuitEvent>
907         ::PostEvent(WRTInstallerNS::QuitEvent());
908 }
909
910 void WrtInstaller::installNewPlugins()
911 {
912     LogDebug("Install new plugins");
913
914     if (!PluginUtils::lockPluginInstallation()) {
915         LogInfo("Lock NOT created");
916         return;
917     }
918
919     if (!PluginUtils::checkPluginInstallationRequired()) {
920         LogDebug("Plugin installation not required");
921         PluginUtils::unlockPluginInstallation();
922         return;
923     }
924
925     m_startupPluginInstallation = true;
926     AddStep(&WrtInstaller::installPluginsStep);
927 }
928
929 int main(int argc, char *argv[])
930 {
931     UNHANDLED_EXCEPTION_HANDLER_BEGIN
932     {
933         // Output on stdout will be flushed after every newline character,
934         // even if it is redirected to a pipe. This is useful for running
935         // from a script and parsing output.
936         // (Standard behavior of stdlib is to use full buffering when
937         // redirected to a pipe, which means even after an end of line
938         // the output may not be flushed).
939         setlinebuf(stdout);
940
941         // Check and re-set the file open limitation
942         struct rlimit rlim;
943         if (getrlimit(RLIMIT_NOFILE, &rlim) != -1) {
944             LogDebug("RLIMIT_NOFILE sft(" << rlim.rlim_cur << ")");
945             LogDebug("RLIMIT_NOFILE hrd(" << rlim.rlim_max << ")");
946
947             if (rlim.rlim_cur < NOFILE_CNT_FOR_INSTALLER) {
948                 rlim.rlim_cur = NOFILE_CNT_FOR_INSTALLER;
949                 rlim.rlim_max = NOFILE_CNT_FOR_INSTALLER;
950                 if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) {
951                     LogError("setrlimit is fail!!");
952                 }
953             }
954         } else {
955             LogError("getrlimit is fail!!");
956         }
957
958         // set evas backend type for emulator
959         // popup isn't showed in the emulator,
960         // if backend isn't set to SW backend
961         if (GlobalSettings::IsEmulator()) {
962             if (setenv("ELM_ENGINE", "x11", 1)) {
963                 LogDebug("Enable backend");
964             }
965         }
966
967         WrtInstaller app(argc, argv);
968         int ret = app.Exec();
969         LogDebug("App returned: " << ret);
970         ret = app.getReturnStatus();
971         LogDebug("WrtInstaller returned: " << ret);
972         return ret;
973     }
974     UNHANDLED_EXCEPTION_HANDLER_END
975 }