LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ApplicationInstaller.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef APPLICATIONINSTALLER_H_
23 #define APPLICATIONINSTALLER_H_
24 
25 #include "Common.h"
26 
27 #include <list>
28 #include <string>
29 #include <map>
30 #include <vector>
31 #include <glib.h>
32 #include <cjson/json.h>
33 #include <cjson/json_util.h>
34 
35 #include <lunaservice.h>
36 
37 #include "MutexLocker.h"
38 
39 #include <QObject>
40 
41 #include <sys/statfs.h>
42 #include <sys/statvfs.h>
43 
44 class PackageDescription;
45 
46 // for debug only
47 typedef int (*statfsfn)(const char *, struct statfs *);
48 typedef int (*statvfsfn)(const char *,struct statvfs *);
49 
50 // ipkg install and ipkg remove processes will run with this priority instead of inheriting sysmgr's
51 #define IPKG_PROCESS_PRIORITY 1
52 
54 public:
55  enum Type {
56  Install = 0,
58  };
59 
62  }
63 
64  virtual ~CommandParams() {
65  if (_childStdOutChannel) {
66  g_io_channel_unref(_childStdOutChannel);
67  }
68  if (_childStdOutSource) {
69  g_source_destroy(_childStdOutSource);
70  g_source_unref(_childStdOutSource);
71  }
72  }
73 
75  GIOChannel* _childStdOutChannel;
77 };
78 
79 class InstallParams : public CommandParams {
80 public:
81  InstallParams(const std::string& target, const std::string& id, const unsigned long ticket, LSHandle * lshandle,const LSMessage * msg,const unsigned int uncompressedSizeInKB, bool verify = true, bool systemMode = false)
82  : CommandParams(CommandParams::Install), _target(target) , _id(id), ticketId(ticket) , _lshandle(lshandle) , _msg(msg) , _verify(verify), _sysMode(systemMode), _uncompressedSizeInKB(uncompressedSizeInKB)
83  { }
84  const std::string _target;
85  const std::string _id;
86  const unsigned long ticketId;
87  LSHandle * _lshandle;
88  const LSMessage * _msg;
89  bool _verify;
90  bool _sysMode;
91  const unsigned int _uncompressedSizeInKB;
92  std::string _packageId;
93 };
94 
95 class RemoveParams : public CommandParams {
96 public:
97  RemoveParams(const std::string& packageName,const unsigned long ticket,LSHandle * lshandle,const LSMessage * msg,int cause)
98  : CommandParams(CommandParams::Remove), _packageName(packageName) , ticketId(ticket) , _lshandle(lshandle) , _msg(msg) , _cause(cause)
99  {}
100  const std::string _packageName;
101  const unsigned long ticketId;
102  const LSHandle * _lshandle;
103  const LSMessage * _msg;
104  const int _cause;
105 };
106 
108 {
109  Q_OBJECT
110 
111 public:
112 
113  static ApplicationInstaller* instance();
114  static std::string s_installer_version;
115 
116  //Luna bus functions
117  static bool cbInstallProgressQuery(LSHandle* lshandle, LSMessage *msg,void *user_data);
118  static bool cbInstall(LSHandle* lshandle, LSMessage *msg,void *user_data);
119  static bool cbInstallNoVerify(LSHandle* lshandle, LSMessage *msg,void *user_data);
120  static bool cbRemove(LSHandle* lshandle, LSMessage *msg,void *user_data);
121  static gboolean cbShallowRemove(gpointer param);
122  static bool cbIsInstalled(LSHandle* lshandle, LSMessage *msg,void *user_data);
123  static bool cbNotifyOnChange(LSHandle* lshandle, LSMessage *msg,void *user_data);
124  static bool cbGetSizes(LSHandle* lshandle,LSMessage *msg,void *user_data);
125  static bool cbQueryInstallCapacity(LSHandle* lshandle,LSMessage *msg,void *user_data);
126  static bool cbDetermineInstallSpaceNeeded(LSHandle* lshandle,LSMessage *msg,void *user_data);
127  static bool cbRevoke(LSHandle* lshandle,LSMessage *msg,void *user_data);
128  static bool cbPubSubRegister(LSHandle* handle, LSMessage* message, void* ctxt);
129  static bool cbPubSubStatus(LSHandle* handle, LSMessage* msg, void* ctxt);
130 
131  static bool cbDbgGetPkgInfoFromStatusFile(LSHandle* lshandle,LSMessage *msg,void *user_data);
132  static bool cbDbgCopyDir(LSHandle* lshandle,LSMessage *msg,void *user_data);
133  static bool cbDbgFakeFsSize(LSHandle* lshandle,LSMessage *msg,void *user_data);
134  static bool cbDbgUnFakeFsSizes(LSHandle* lshandle,LSMessage *msg,void *user_data);
135  static bool cbGetFsSize(LSHandle* lshandle,LSMessage *msg,void *user_data);
136  static bool cbDbgFillSize(LSHandle* lshandle,LSMessage *msg,void *user_data);
137  static bool cbDbgGetAppSizeOnFs(LSHandle* lshandle,LSMessage *msg,void *user_data);
138 
139  //Native interface (for direct calls w/in lunasysmgr)
140  bool install(const std::string& targetPackageName, unsigned int uncompressedAppSizeInKB, const unsigned long ticket);
141  void notifyAppInstalled(const std::string& appId,const std::string& appVersion);
142 
143  bool downloadAndInstall (LSHandle* handle, const std::string& targetPackageFile, struct json_object* authToken, struct json_object* deviceId,
144  unsigned long ticket, bool subscribe);
145  void oneCommandProcessed();
146 
147  static uint64_t getSizeOfAppDir(const std::string& dirName);
148 
149  static Mutex s_sizeFnMutex;
150  static uint64_t s_sizeFnAccumulator;
151  static uint64_t s_auxSizeFnAccumulator;
152  static uint64_t s_targetFsBlockSize;
153  static std::string s_sizeFnBaseDir;
154  static json_object * s_manifestJobj;
155 
156  static int _getSizeCbFn(const char *fpath, const struct stat *sb,int typeflag, struct FTW *ftwbuf);
157  static int _getSizeOfAppCbFn(const char *fpath, const struct stat *sb,int typeflag, struct FTW *ftwbuf);
158 
159 // static uint64_t getSizeOfAppDir_opt(const std::string& dirName);
160  static uint64_t getSizeOfAppOnFs(const std::string& destFsPath,const std::string& dirName,uint32_t * r_pBsize=NULL);
161  static uint64_t getSizeOfPackageOnFsGenerateManifest(const std::string& destFsPath, PackageDescription* packageDesc, uint32_t * r_pBsize);
162  static uint64_t getSizeOfPackageById(const std::string& packageId);
163 
164  static uint64_t getFsFreeSpaceInMB(const std::string& pathOnFs);
165  static uint64_t getFsFreeSpaceInBlocks(const std::string& pathOnFs,uint64_t * pBlockSize = 0);
166 
167  static bool extractVersionFromAppInfo(const std::string& appBaseDir,std::string& r_versionString);
168 
169 #define APPREMOVED_CAUSE_UNKNOWN -1
170 #define APPREMOVED_CAUSE_USERDELETED 0
171 #define APPREMOVED_CAUSE_APPREVOKED 1
172  void notifyAppRemoved(const std::string& appId,const std::string& appVersion,int cause=APPREMOVED_CAUSE_UNKNOWN);
173  static bool getDownloadPathBasedOnSpaceRemaining(std::string& packageDownloadPath);
174  static bool isValidInstallURI(const std::string& url);
175 
176  bool allowSuspend();
177 
178  json_object * packageInfoFileToJson(const std::string& packageId);
179 
180 private Q_SLOTS:
181 
182  void slotMediaPartitionAvailable(bool val);
183 
184 private:
185 
188 
189  LSHandle* m_service;
190 
191  std::list<std::string> m_appInstallBaseDirs;
192 
193  bool init();
194  void startService();
195  void stopService();
196 
197  void enterBrickMode();
198  void exitBrickMode();
199 
200  bool lunasvcInstallProgressQuery(LSHandle* lshandle, LSMessage *msg,void *user_data);
201  int lunasvcRemove(RemoveParams* params);
202  bool lunasvcIsInstalled(LSHandle* lshandle, LSMessage *msg,void *user_data);
203  bool lunasvcNotifyOnChange(const std::string& packageName,LSHandle * lshandle,LSMessage *msg);
204  bool lunasvcGetInstalledSizes(LSHandle * lshandle,LSMessage *msg);
205  int lunasvcQueryInstallCapacity(const std::string& packageId,uint64_t packageSizeInKB,uint64_t uncompressedPackageSizeInKB,uint64_t& r_spaceNeeded);
206 
207  void processOrQueueCommand(CommandParams* cmd);
208  bool processInstallCommand(InstallParams* params);
209  bool processRemoveCommand(RemoveParams* params);
210  bool processNextCommand();
211 
212  void closeApp(const std::string& appId);
213 
214  static bool packageNameFromControl(const std::string& controlTarGzPathAndFile,const std::string& tempDir,std::string& return_PackageName);
215  static int getAllUserInstalledAppNames(std::vector<std::string>& appList,std::string basePkgDirName);
216  static bool findUserInstalledAppName(const std::string& packageName,const std::string& basePkgDirName);
217  static int getAllUserInstalledAppSizes(std::vector<std::pair<std::string,uint64_t> >& appList,std::string basePkgDirName);
218 
219  static bool arePathsOnSameFilesystem(const std::string& path1,const std::string& path2);
220 
221  static int doSignatureVerifyOnFile(const std::string& file,const std::string& signatureFile,const std::string& pubkeyFile);
222  static int doSignatureVerifyOnFiles(std::vector<std::string>& files,const std::string& signatureFile,const std::string& pubkeyFile);
223  static int extractPublicKeyFromCert(const std::string& certFile,const std::string& pubkeyFile);
224  static int runOpenSSL(std::vector<std::string>& params,const std::string& command);
225  static int runIpkgRemove(const std::string& ipkgRoot,const std::string& packageName);
226 
227  static std::list<CommandParams*> s_commandParams;
228  bool m_inBrickMode;
229 
230  struct CommandState {
231  CommandState() {
232  reset();
233  }
234 
235  void reset() {
236  processing = false;
237  sourceId = 0;
238  pid = -1;
239  }
240 
241  bool processing;
242  guint sourceId;
243  GPid pid;
244  };
245 
246  CommandState m_cmdState;
247 
248  //------------------------------------------------ DEBUG -----------------------------------------------------------
249 
250  static statfsfn s_statfsFn;
251  static statvfsfn s_statvfsFn;
252  static int dbg_restoreFakeFsEntriesAtStartup();
253  static int dbg_statfs(const char *, struct statfs *);
254  static int dbg_statvfs(const char *,struct statvfs *);
255  static int dbg_fill(std::string& path,uint32_t& bsize,uint32_t& nblocks);
256  static int dbg_getAppSizeOnFs(const std::string& fspath,const std::string& appbasepath,uint64_t& r_size);
257  static std::map<uint32_t,std::pair<uint32_t,uint32_t> > dbg_statfs_map;
258  static std::map<uint32_t,std::pair<uint32_t,uint32_t> > dbg_statvfs_map;
259  static json_object * dbg_statxfs_persistent;
260 
261 }; //end class ApplicationInstaller
262 
263 #endif /*APPLICATIONINSTALLER_H_*/