Back to index

lightning-sunbird  0.9+nobinonly
ipcModuleReg.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla IPC.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <string.h>
00039 #include <stdlib.h>
00040 
00041 #include "prlink.h"
00042 #include "prio.h"
00043 #include "prlog.h"
00044 #include "plstr.h"
00045 
00046 #include "ipcConfig.h"
00047 #include "ipcLog.h"
00048 #include "ipcModuleReg.h"
00049 #include "ipcModule.h"
00050 #include "ipcCommandModule.h"
00051 #include "ipcd.h"
00052 
00053 //-----------------------------------------------------------------------------
00054 
00055 struct ipcModuleRegEntry
00056 {
00057     nsID              target;
00058     ipcModuleMethods *methods;
00059     PRLibrary        *lib;
00060 };
00061 
00062 #define IPC_MAX_MODULE_COUNT 64
00063 
00064 static ipcModuleRegEntry ipcModules[IPC_MAX_MODULE_COUNT];
00065 static int ipcModuleCount = 0;
00066 
00067 //-----------------------------------------------------------------------------
00068 
00069 static PRStatus
00070 AddModule(const nsID &target, ipcModuleMethods *methods, const char *libPath)
00071 {
00072     if (ipcModuleCount == IPC_MAX_MODULE_COUNT) {
00073         LOG(("too many modules!\n"));
00074         return PR_FAILURE;
00075     }
00076 
00077     if (!methods) {
00078         PR_NOT_REACHED("null module methods");
00079         return PR_FAILURE;
00080     }
00081 
00082     //
00083     // each ipcModuleRegEntry holds a reference to a PRLibrary, and on
00084     // shutdown, each PRLibrary reference will be released.  this ensures
00085     // that the library will not be unloaded until all of the modules in
00086     // that library are shutdown.
00087     //
00088     ipcModules[ipcModuleCount].target = target;
00089     ipcModules[ipcModuleCount].methods = methods;
00090     ipcModules[ipcModuleCount].lib = PR_LoadLibrary(libPath);
00091 
00092     ++ipcModuleCount;
00093     return PR_SUCCESS;
00094 }
00095 
00096 static void
00097 InitModuleFromLib(const char *modulesDir, const char *fileName)
00098 {
00099     LOG(("InitModuleFromLib [%s]\n", fileName));
00100 
00101     static const ipcDaemonMethods gDaemonMethods =
00102     {
00103         IPC_DAEMON_METHODS_VERSION,
00104         IPC_DispatchMsg,
00105         IPC_SendMsg,
00106         IPC_GetClientByID,
00107         IPC_GetClientByName,
00108         IPC_EnumClients,
00109         IPC_GetClientID,
00110         IPC_ClientHasName,
00111         IPC_ClientHasTarget,
00112         IPC_EnumClientNames,
00113         IPC_EnumClientTargets
00114     };
00115 
00116     int dLen = strlen(modulesDir);
00117     int fLen = strlen(fileName);
00118 
00119     char *buf = (char *) malloc(dLen + 1 + fLen + 1);
00120     memcpy(buf, modulesDir, dLen);
00121     buf[dLen] = IPC_PATH_SEP_CHAR;
00122     memcpy(buf + dLen + 1, fileName, fLen);
00123     buf[dLen + 1 + fLen] = '\0';
00124 
00125     PRLibrary *lib = PR_LoadLibrary(buf);
00126     if (lib) {
00127         ipcGetModulesFunc func =
00128             (ipcGetModulesFunc) PR_FindFunctionSymbol(lib, "IPC_GetModules");
00129 
00130         LOG(("  func=%p\n", (void*) func));
00131 
00132         if (func) {
00133             const ipcModuleEntry *entries = NULL;
00134             int count = func(&gDaemonMethods, &entries);
00135             for (int i=0; i<count; ++i) {
00136                 if (AddModule(entries[i].target, entries[i].methods, buf) == PR_SUCCESS) {
00137                     if (entries[i].methods->init)
00138                         entries[i].methods->init();
00139                 }
00140             }
00141         }
00142         PR_UnloadLibrary(lib);
00143     }
00144 
00145     free(buf);
00146 }
00147 
00148 //-----------------------------------------------------------------------------
00149 // ipcModuleReg API
00150 //-----------------------------------------------------------------------------
00151 
00152 void
00153 IPC_InitModuleReg(const char *exePath)
00154 {
00155     if (!(exePath && *exePath))
00156         return;
00157 
00158     //
00159     // register plug-in modules
00160     //
00161     char *p = PL_strrchr(exePath, IPC_PATH_SEP_CHAR);
00162     if (p == NULL) {
00163         LOG(("unexpected exe path\n"));
00164         return;
00165     }
00166 
00167     int baseLen = p - exePath;
00168     int finalLen = baseLen + 1 + sizeof(IPC_MODULES_DIR);
00169 
00170     // build full path to ipc modules
00171     char *modulesDir = (char*) malloc(finalLen);
00172     memcpy(modulesDir, exePath, baseLen);
00173     modulesDir[baseLen] = IPC_PATH_SEP_CHAR;
00174     memcpy(modulesDir + baseLen + 1, IPC_MODULES_DIR, sizeof(IPC_MODULES_DIR));
00175 
00176     LOG(("loading libraries in %s\n", modulesDir));
00177     // 
00178     // scan directory for IPC modules
00179     //
00180     PRDir *dir = PR_OpenDir(modulesDir);
00181     if (dir) {
00182         PRDirEntry *ent;
00183         while ((ent = PR_ReadDir(dir, PR_SKIP_BOTH)) != NULL) {
00184             // 
00185             // locate extension, and check if dynamic library
00186             //
00187 
00188             const char *p = strrchr(ent->name, '.');
00189             if (p && PL_strcasecmp(p, MOZ_DLL_SUFFIX) == 0)
00190                 InitModuleFromLib(modulesDir, ent->name);
00191         }
00192         PR_CloseDir(dir);
00193     }
00194 
00195     free(modulesDir);
00196 }
00197 
00198 void
00199 IPC_ShutdownModuleReg()
00200 {
00201     //
00202     // shutdown modules in reverse order    
00203     //
00204     while (ipcModuleCount) {
00205         ipcModuleRegEntry &entry = ipcModules[--ipcModuleCount];
00206         if (entry.methods->shutdown)
00207             entry.methods->shutdown();
00208         if (entry.lib)
00209             PR_UnloadLibrary(entry.lib);
00210     }
00211 }
00212 
00213 void
00214 IPC_NotifyModulesClientUp(ipcClient *client)
00215 {
00216     for (int i = 0; i < ipcModuleCount; ++i) {
00217         ipcModuleRegEntry &entry = ipcModules[i];
00218         if (entry.methods->clientUp)
00219             entry.methods->clientUp(client);
00220     }
00221 }
00222 
00223 void
00224 IPC_NotifyModulesClientDown(ipcClient *client)
00225 {
00226     for (int i = 0; i < ipcModuleCount; ++i) {
00227         ipcModuleRegEntry &entry = ipcModules[i];
00228         if (entry.methods->clientDown)
00229             entry.methods->clientDown(client);
00230     }
00231 }
00232 
00233 PRStatus
00234 IPC_DispatchMsg(ipcClient *client, const nsID &target, const void *data, PRUint32 dataLen)
00235 {
00236     // dispatch message to every module registered under the given target.
00237     for (int i=0; i<ipcModuleCount; ++i) {
00238         ipcModuleRegEntry &entry = ipcModules[i];
00239         if (entry.target.Equals(target)) {
00240             if (entry.methods->handleMsg)
00241                 entry.methods->handleMsg(client, target, data, dataLen);
00242         }
00243     }
00244     return PR_SUCCESS;
00245 }