Back to index

im-sdk  12.3.91
SunIM.c
Go to the documentation of this file.
00001 /*
00002   Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004   Permission is hereby granted, free of charge, to any person obtaining a
00005   copy of this software and associated documentation files (the
00006   "Software"), to deal in the Software without restriction, including
00007   without limitation the rights to use, copy, modify, merge, publish,
00008   distribute, sublicense, and/or sell copies of the Software, and to
00009   permit persons to whom the Software is furnished to do so, subject to
00010   the following conditions: The above copyright notice and this
00011   permission notice shall be included in all copies or substantial
00012   portions of the Software.
00013 
00014 
00015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018   IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021   THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022   ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025   Except as contained in this notice, the names of The Open Group and/or
00026   Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027   promote the sale, use or other dealings in this Software without prior
00028   written authorization from The Open Group and/or Sun Microsystems,
00029   Inc., as applicable.
00030 
00031 
00032   X Window System is a trademark of The Open Group
00033 
00034   OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035   logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036   Group. All other trademarks and registered trademarks mentioned herein
00037   are the property of their respective owners. No right, title or
00038   interest in or to any trademark, service mark, logo or trade name of
00039   Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <stdarg.h>
00045 #include <string.h>
00046 #include <sys/types.h>
00047 #include <sys/param.h>
00048 #include <errno.h>
00049 #ifdef  sun
00050 #include <unistd.h>
00051 #endif
00052 
00053 #include "SunIM.h"
00054 #include "SunIMLock.h"
00055 
00056 Public iml_inst *iml_execute_iml_wrapper(iml_session_t *, iml_inst **);
00057 int update_supported_langlist_for_le(iml_desktop_t *, IMLEName *, IMLocale *, int);
00058 Public void close_le_module(void *);
00059 Public iml_if_t *if_OpenIF(const char *if_path,
00060                         const char *if_name,
00061                         const char *locale, Bool);
00062 Public void if_CloseIF(iml_if_t *, Bool);
00063 Public Bool if_GetIFValues(iml_if_t *, IMArgList, int);
00064 Public Bool if_SetIFValues(iml_if_t *, IMArgList, int);
00065 Public iml_session_t *if_CreateSC(iml_if_t *, IMArgList, int);
00066 Public Bool if_DestroySC(iml_session_t *);
00067 Public Bool if_GetSCValues(iml_session_t *, IMArgList, int);
00068 Public Bool if_SetSCValues(iml_session_t *, IMArgList, int);
00069 Public void if_SendEvent(iml_session_t *, IMInputEvent *);
00070 Public void if_SendEvent_AuxGet(iml_session_t *, IMInputEvent *);
00071 Public IMText *if_ResetSC(iml_session_t *);
00072 Public void if_SetSCFocus(iml_session_t *);
00073 Public void if_UnsetSCFocus(iml_session_t *);
00074 
00075 Public Bool if_configure(iml_if_t * If,
00076                       const char *if_path,
00077                       const char *if_name,
00078                       const char *if_locale,
00079                       Bool);
00080 Public iml_session_t *iml_construct_session(iml_desktop_t *, IMArgList, int);
00081 
00082 iml_desktop_t *new_user(iml_if_t * If,
00083                      const char *user_name,
00084                      const char *host_name,
00085                      const char *display_id);
00086 iml_desktop_t *find_user(iml_if_t * If, char *user_name, char *host_name, char *display_id);
00087 void del_user(iml_desktop_t * desktop);
00088 Private void *nsc_GetFunction(const char *name);
00089 Private void *hkc_GetFunction(const char *name);
00090 
00091 void add_session_to_desktop(iml_session_t *);
00092 void remove_session_from_desktop(iml_session_t *);
00093 
00094 static listener_id_t add_listener_to_desktop(iml_session_t *, void *, void *);
00095 iml_listener_t *lookup_listener_from_desktop(iml_session_t *, int);
00096 void delete_listener_from_desktop(iml_session_t *, int);
00097 iml_file_status_t *get_fs_by_id(iml_nsc *, int);
00098 
00099 Public char* secure_pathname(char *p); 
00100 
00101 int ns_open(iml_nsc *, const char *, int, ...);
00102 size_t ns_read(iml_nsc *, int, void *, size_t);
00103 int ns_stat(iml_nsc *, const char *, struct stat *);
00104 size_t ns_write(iml_nsc *, int, void *, size_t);
00105 int ns_close(iml_nsc *, int);
00106 int ns_mkdir(iml_nsc *, const char *, mode_t);
00107 int ns_rmdir(iml_nsc *, const char *);
00108 int ns_symlink(iml_nsc *, const char *, const char *);
00109 int ns_lstat(iml_nsc *, const char *, struct stat *);
00110 int ns_fstat(iml_nsc *, int filedes, struct stat *);
00111 int ns_creat(iml_nsc *, const char *, mode_t);
00112 off_t ns_lseek(iml_nsc *, int, off_t, int);
00113 int ns_unlink(iml_nsc *, const char *);
00114 int ns_rename(iml_nsc *, const char *, const char *);
00115 int ns_fcntl(iml_nsc *, int, int, int);
00116 int ns_truncate(iml_nsc *, const char *, off_t);
00117 int ns_ftruncate(iml_nsc *, int, off_t);
00118 DIR *ns_opendir(iml_nsc *, const char *);
00119 struct dirent *ns_readdir(iml_nsc *, DIR *);
00120 int ns_closedir(iml_nsc *, DIR *);
00121 
00122 /*    TODO!! Need to implement the following NS API calls 
00123  *
00124  * int ns_access(iml_nsc *, const char *pathname, int mode);
00125  * int ns_chmod(iml_nsc *, const char *path, mode_t mode);
00126  * int ns_chown(iml_nsc *, const char *path, uid_t owner, gid_t group);
00127  * long ns_fpathconf(iml_nsc *, int filedes, int name);
00128  * int ns_link(iml_nsc *, const char *oldpath, const char *newpath);
00129  * long ns_pathconf(iml_nsc *, char *path, int name);
00130  * ssize_t ns_readv(iml_nsc *, int fd, const struct iovec *vector, int count);
00131  * ssize_t ns_writev(iml_nsc *, int fd, const struct iovec *vector, int count);
00132  * long ns_ulimit(iml_nsc *, int cmd, long newlimit);
00133  * mode_t ns_umask(iml_nsc *, mode_t mask);
00134  * int ns_utime(iml_nsc *, const char *filename, const struct utimbuf *buf);
00135  * int ns_utimes(iml_nsc *, char *filename, struct timeval *tvp);
00136  */
00137 
00138 Private int updateSupportedLocales(iml_desktop_t *, IMLEName *, IMLocale *, int);
00139 Private void *ns_create(const char *, int, void *);
00140 Private void ns_free(iml_nsc_t);
00141 
00142 /* Methods for Hotkey Context */
00143 Private int switchLEProfile(iml_session_t *,int, IMLEName *);
00144 
00145 Private int lookup_config(iml_nsc *, const char *, char **);
00146 
00147 typedef struct _name_fn_map {
00148     char *name;
00149     void *fn_ptr;
00150 } name_fn_map;
00151 
00152 Private name_fn_map iof_map_list[] = {
00153        {"_nsc_create", (void *)ns_create},
00154        {"_nsc_free", (void *)ns_free},
00155        {"open", (void *)ns_open},
00156        {"read", (void *)ns_read},
00157        {"stat", (void *)ns_stat},
00158        {"write", (void *)ns_write},
00159        {"close", (void *)ns_close},
00160        {"mkdir", (void *)ns_mkdir},
00161        {"rmdir", (void *)ns_rmdir},
00162        {"symlink", (void *)ns_symlink},
00163        {"lstat", (void *)ns_lstat},
00164        {"creat", (void *)ns_creat},
00165        {"lseek", (void *)ns_lseek},
00166        {"unlink", (void *)ns_unlink},
00167        {"rename", (void *)ns_rename},
00168        {"fcntl", (void *)ns_fcntl},
00169        {"truncate", (void *)ns_truncate},
00170        {"ftruncate", (void *)ns_ftruncate},
00171        {"opendir", (void *)ns_opendir},
00172        {"readdir", (void *)ns_readdir},
00173        {"closedir", (void *)ns_closedir},
00174        {"fstat", (void *)ns_fstat},
00175        {"access", (void *)0},
00176        {"chmod", (void *)0},
00177        {"chown", (void *)0},
00178        {"pathconf", (void *)0},
00179        {"fpathconf", (void *)0},
00180        {"readv", (void *)0},
00181        {"writev", (void *)0},
00182        {"ulimit", (void *)0},
00183        {"umask", (void *)0},
00184        {"utime", (void *)0},
00185        {"utimes", (void *)0},
00186        {"link", (void *)0},
00187 };
00188 
00189 Private void *hk_create(const char *, int, void *);
00190 Private void hk_free(iml_hkc_t);
00191 
00192 Private name_fn_map hkf_map_list[] = {
00193        {"_hkc_create", (void *)hk_create},
00194        {"_hkc_free", (void *)hk_free},
00195 };
00196 
00197 static int unique_ns_id = 0;
00198 static int dirID;
00199 
00200 Public iml_if_t *
00201 if_OpenIF(
00202     const char *if_path,
00203     const char *if_name,
00204     const char *if_locale,
00205     Bool call_openif
00206 )
00207 {
00208     iml_if_t *If;
00209     If = (iml_if_t *) calloc(1, sizeof(iml_if_t));
00210     If->hkm = (IMHotkeyManagerStruct *) 0;
00211     if (NULL != getenv("IIIMD_NSFIO_OLD")) {
00212        If->nsc_get_function = nsc_GetFunction;
00213     } else {
00214        extern void * fop_get_function(const char *);
00215        If->nsc_get_function = fop_get_function;
00216     }
00217     If->hkc_get_function = hkc_GetFunction;
00218     If->updateSupportedLocales = updateSupportedLocales;
00219     If->switchLEProfile = switchLEProfile;
00220 
00221     if (if_configure(If, if_path, if_name, if_locale, call_openif) == False) {
00222         if (If->locale) {
00223             free(If->locale);
00224         }
00225         if (If->if_name) {
00226             free(If->if_name);
00227         }
00228         if (If->ifpath_name) {
00229             free(If->ifpath_name);
00230         }
00231         free(If);
00232         If = NULL;
00233     }
00234     return If;
00235 }
00236 
00237 Public void
00238 if_CloseIF(
00239     iml_if_t * If,
00240     Bool call_openif
00241 )
00242 {
00243     if(call_openif == True) {
00244         If->ifm->if_CloseIF(If);
00245     }
00246 
00247     if (If->m_wrapper) {
00248         free (If->m_wrapper);
00249     }
00250     if (If->m) {
00251         free(If->m);
00252     }
00253     if (If->locale) {
00254         free(If->locale);
00255     }
00256     if (If->if_name) {
00257         free(If->if_name);
00258     }
00259     if (If->ifpath_name) {
00260         free(If->ifpath_name);
00261     }
00262 
00263     close_le_module(If->dl_module);
00264 
00265     free(If);
00266 }
00267 
00268 Public Bool
00269 if_GetIFValues(
00270     iml_if_t * If,
00271     IMArgList args,
00272     int n_args
00273 )
00274 {
00275     if (If) {
00276         return If->ifm->if_GetIFValues(If, args, n_args);
00277     }
00278     return False;
00279 }
00280 
00281 Public Bool
00282 if_SetIFValues(
00283     iml_if_t * If,
00284     IMArgList args,
00285     int n_args
00286 )
00287 {
00288     if (If) {
00289         return If->ifm->if_SetIFValues(If, args, n_args);
00290     }
00291     return False;
00292 }
00293 
00294 #ifdef sun
00295 mutex_t              desktop_mutex;
00296 #elif  WIN32
00297 HANDLE        desktop_mutex;
00298 #else  /* Linux */
00299 pthread_mutex_t      desktop_mutex;
00300 #endif
00301 
00302 
00303 Public iml_session_t *
00304 if_CreateSC(
00305     iml_if_t * If,
00306     IMArgList args,
00307     int n_args
00308 )
00309 {
00310     iml_session_t *s = NULL;
00311     Bool ret;
00312     IMArg *p = args;
00313     int i;
00314     
00315     iml_desktop_t *desktop = (iml_desktop_t *) 0;
00316     
00317     char *user_name = (char *) 0;
00318     char *host_name = (char *) 0;
00319     char *display_id = (char *) 0;
00320     
00321     for (i = 0; i < n_args; i++, p++) {
00322         if (p->id == UI_USER_NAME) {
00323             user_name = p->value;
00324         } else if (p->id == UI_HOST_NAME) {
00325             host_name = p->value;
00326         } else if (p->id == UI_DISPLAY_ID) {
00327             display_id = p->value;
00328         }
00329     }
00330     
00331     if (!user_name || !host_name || !display_id) {
00332         return NULL;
00333     }
00334     SUNIM_LOCK(desktop_mutex);
00335     desktop = find_user(If, user_name, host_name, display_id);
00336     if (!desktop) {
00337         desktop = new_user(If, user_name, host_name, display_id);
00338         ret = If->ifm->if_OpenDesktop(desktop, args, n_args);
00339         if (ret == False) {
00340             del_user(desktop);
00341            SUNIM_UNLOCK(desktop_mutex);
00342             return NULL;
00343         }
00344         /* Initialize the listener_id and listener_count */
00345         desktop->listener_id = 0;
00346         desktop->listener_count = 0;
00347     }
00348     /*
00349      * All LE specific remote file loading and hotkey registration 
00350      * has to be completed before unlocking desktop, if each connection
00351      * is assigned to one thread. If each user is assigned to one thread
00352      * locking is not needed.
00353      */
00354     SUNIM_UNLOCK(desktop_mutex);
00355     s = iml_construct_session(desktop, args, n_args);
00356     
00357     if (s) {
00358         add_session_to_desktop(s);
00359     }
00360     return s;
00361 }
00362 
00363 Public Bool
00364 if_DestroySC_WithoutDesktopDestruction(
00365     iml_session_t * s
00366 )
00367 {
00368     if (s) {
00369        int i;
00370         Bool ret;
00371        IMFeedbackList *flist;
00372         
00373         ret = s->If->ifm->if_DestroySC(s);
00374         
00375         s->If->m->iml_delete(s);
00376         s->If->m->iml_delete2(s);
00377         
00378         remove_session_from_desktop(s);
00379         
00380         if (s->status_cache.text && s->status_cache.text->text.utf_chars) {
00381             free((char *) s->status_cache.text->text.utf_chars);
00382        }
00383         if (s->status_cache.text && s->status_cache.text->feedback) {
00384            for(i=0;i<DEFAULTStatusCacheSize;i++){
00385               flist=&s->status_cache.text->feedback[i];
00386               if(flist){
00387                   free(flist->feedbacks);
00388               }
00389            }
00390            free(s->status_cache.text->feedback);
00391        }
00392 
00393         if (s->status_cache.text) {
00394             free((char *) s->status_cache.text);
00395        }
00396 
00397         if (s->PreEditTextInfo.text && s->PreEditTextInfo.text->text.utf_chars) {
00398             free((char *) s->PreEditTextInfo.text->text.utf_chars);
00399        }
00400         if (s->PreEditTextInfo.text && s->PreEditTextInfo.text->feedback) {
00401            for(i=0;i<s->PreEditTextBufferSize;i++){
00402               flist=&s->PreEditTextInfo.text->feedback[i];
00403               if(flist){
00404                   free(flist->feedbacks);
00405               }
00406            }
00407            free(s->PreEditTextInfo.text->feedback);
00408         }
00409 
00410         if (s->PreEditTextInfo.text) {
00411             free((char *) s->PreEditTextInfo.text);
00412        }
00413         
00414         free(s);
00415         
00416         return ret;
00417     }
00418     return False;
00419 }
00420 
00421 Public Bool
00422 if_DestroySC(
00423     iml_session_t * s
00424 )
00425 {
00426     if (s) {
00427         Bool ret;
00428         iml_desktop_t *desktop = s->desktop;
00429        iml_if_t *iml_if = s->If;
00430 
00431         ret = if_DestroySC_WithoutDesktopDestruction(s);
00432         if (!desktop->session_list) {
00433             iml_if->ifm->if_CloseDesktop(desktop);
00434             del_user(desktop);
00435         }
00436         return ret;
00437     }
00438     return False;
00439 }
00440 
00441 Public Bool
00442 if_SetSCValues(
00443     iml_session_t * s,
00444     IMArgList args,
00445     int n_args
00446 )
00447 {
00448     if (s) {
00449         Bool ret = s->If->ifm->if_SetSCValues(s, args, n_args);
00450         s->If->m->iml_delete(s);
00451         return ret;
00452     }
00453     return False;
00454 }
00455 
00456 Public Bool
00457 if_GetSCValues(
00458     iml_session_t * s,
00459     IMArgList args,
00460     int n_args
00461 )
00462 {
00463     if (s) {
00464        Bool ret=s->If->ifm->if_GetSCValues(s, args, n_args);
00465         s->If->m->iml_delete(s);
00466         return ret;
00467     }
00468     return False;
00469 }
00470 
00471 Public IMText *
00472 if_ResetSC(
00473     iml_session_t * s
00474 )
00475 {
00476     IMText *prs = NULL;
00477     if (s) {
00478         s->If->m->iml_delete(s);
00479         prs = s->If->ifm->if_ResetSC(s);
00480        if(!prs){
00481            s->If->m->iml_delete(s);
00482        }
00483     }
00484     return prs;
00485 }
00486 
00487 Public void
00488 if_SetSCFocus(
00489     iml_session_t * s
00490 )
00491 {
00492     iml_inst *rv = NULL;
00493     iml_inst *lp;
00494     if (s) {
00495         if (s->status_cache.text->char_length) {
00496             lp = s->If->m->iml_make_status_start_inst(s);
00497             s->If->m->iml_link_inst_tail(&rv, lp);
00498             lp = s->If->m->iml_make_status_draw_inst(s, s->status_cache.text);
00499             s->If->m->iml_link_inst_tail(&rv, lp);
00500             lp = s->If->m->iml_execute(s, &rv);
00501         }
00502         s->If->ifm->if_SetSCFocus(s);
00503         s->If->m->iml_delete(s);
00504     }
00505     return;
00506 }
00507 
00508 Public void
00509 if_UnsetSCFocus(
00510     iml_session_t * s
00511 )
00512 {
00513     iml_inst *lp;
00514 
00515     if (s) {
00516         lp = s->If->m->iml_make_status_done_inst(s);
00517         s->If->m->iml_execute(s, &lp);
00518         s->If->ifm->if_UnsetSCFocus(s);
00519         s->If->m->iml_delete(s);
00520     }
00521     return;
00522 }
00523 
00524 Public void
00525 if_SendEvent(
00526     iml_session_t * s,
00527     IMInputEvent * e
00528 )
00529 {
00530     if (s) {
00531         s->If->ifm->if_SendEvent(s, e);
00532         s->If->m->iml_delete(s);
00533     }
00534     return;
00535 }
00536 
00537 Public void
00538 if_SendEvent_AuxGet(
00539     iml_session_t * s,
00540     IMInputEvent * e
00541 )
00542 {
00543     if (s) {
00544         s->If->ifm->if_SendEvent(s, e);
00545         /* DON'T CALL s->If->m->iml_delete(s) HERE! */
00546     }
00547     return;
00548 }
00549 
00550 /*
00551  * generic constructor of imlogic session struct
00552  */
00553 
00554 Public iml_session_t *
00555 iml_construct_session(
00556     iml_desktop_t * desktop,
00557     IMArgList args,
00558     int num_args
00559 )
00560 {
00561     iml_if_t *If = desktop->If;
00562     iml_session_t *s;
00563     int i;
00564     Bool ret;
00565     
00566     s = (iml_session_t *) calloc(1, sizeof(iml_session_t));
00567     s->desktop = desktop;
00568     s->If = If;
00569     s->next = NULL;
00570 
00571 #ifdef ENABLE_EIMIL
00572     if (If->eh != EIMIL_VOID_HANDLE)
00573        EIMIL_duplicate_handle(&s->eh, If->eh);
00574 #endif
00575 
00576     if ((ret=If->ifm->if_CreateSC(s, args, num_args)) == False){
00577         free(s);
00578         return NULL;
00579     }
00580 
00581     s->status_cache.text = (IMText *) calloc(1, sizeof(IMText));
00582     s->status_cache.text->encoding = UTF16_CODESET;
00583     s->status_cache.text->feedback = (IMFeedbackList *) calloc(1, sizeof(IMFeedbackList) * DEFAULTStatusCacheSize);
00584     s->status_cache.text->text.utf_chars = (UTFCHAR *) calloc(1, sizeof(UTFCHAR) * DEFAULTStatusCacheSize);
00585     s->status_cache.text->char_length = 0;
00586     s->status_cache.text->count_annotations = 0;
00587     s->status_cache.text->annotations = NULL;
00588     for (i = 0; i < DEFAULTStatusCacheSize; i++) {
00589         IMFeedbackList *fbl = &s->status_cache.text->feedback[i];
00590         fbl->feedbacks = (IMFeedback *) calloc(1, sizeof(IMFeedback)*DEFAULTFeedbackSize);
00591     }
00592 
00593     s->PreEditTextInfo.text = (IMText *) calloc(1, sizeof(IMText));
00594     s->PreEditTextInfo.text->encoding = UTF16_CODESET;
00595     s->PreEditTextInfo.text->text.utf_chars = (UTFCHAR *) calloc(1, sizeof(UTFCHAR) * DEFAULTPreEditTextBufferSize);
00596     s->PreEditTextInfo.text->feedback = (IMFeedbackList *) calloc(1, sizeof(IMFeedbackList) * DEFAULTPreEditAttrBufferSize);
00597     for (i = 0; i < DEFAULTPreEditTextBufferSize; i++) {
00598         IMFeedbackList *fbl = &s->PreEditTextInfo.text->feedback[i];
00599         fbl->feedbacks = (IMFeedback *) calloc(1, sizeof(IMFeedback)*DEFAULTFeedbackSize);
00600     }
00601 
00602     s->PreEditTextBufferSize = DEFAULTPreEditTextBufferSize;
00603     s->PreEditAttrBufferSize = DEFAULTPreEditAttrBufferSize;
00604 
00605     return (iml_session_t *) s;
00606 }
00607 
00608 iml_desktop_t *
00609 find_user(
00610     iml_if_t * If,
00611     char *user_name,
00612     char *host_name,
00613     char *display_id
00614 )
00615 {
00616     iml_desktop_list p, *prev;
00617     if (If->desktop_list == NULL) {
00618        return (iml_desktop_t *) NULL;
00619     }
00620     for (prev = &If->desktop_list; (p = *prev) != 0; prev = &p->next) {
00621         if (strcmp(p->user_name, user_name) == 0 &&
00622            strcmp(p->host_name, host_name) == 0 &&
00623            strcmp(p->display_id, display_id) == 0) {
00624             return p;
00625         }
00626     }
00627     return (iml_desktop_t *) NULL;
00628 }
00629 
00630 void
00631 del_user(
00632     iml_desktop_t * desktop
00633 )
00634 {
00635     iml_if_t *If = desktop->If;
00636     iml_desktop_list p, *prev;
00637     for (prev = &If->desktop_list; (p = *prev) != 0; prev = &p->next) {
00638         if (strcmp(p->user_name, desktop->user_name) == 0 &&
00639            strcmp(p->host_name, desktop->host_name) == 0 &&
00640            strcmp(p->display_id, desktop->display_id) == 0) {
00641 #ifdef DEBUG
00642             printf("LE %s: %s@%s is deleted\n", If->if_name, p->user_name, p->host_name);
00643 #endif
00644             *prev = p->next;
00645             free(p->user_name);
00646             free(p->host_name);
00647             free(p->display_id);
00648             free(p);
00649             If->desktop_count--;
00650             break;
00651         }
00652         if (p->next == NULL) {
00653             break;
00654         }
00655     }
00656 }
00657 
00658 static listener_id_t 
00659 add_listener_to_desktop(
00660     iml_session_t *s,
00661     void *listener,
00662     void *value
00663 )
00664 {
00665     iml_desktop_t *desktop = s->desktop;
00666 
00667     // Create a Listener and return the unique ID
00668 
00669     iml_listener_t *l = (iml_listener_t *) calloc(1, sizeof(iml_listener_t));
00670     l->s = s;
00671     l->listener_name = "";
00672     desktop->listener_id++;
00673     l->listener_id = desktop->listener_id;
00674     l->listener = listener; /* Callback function that needs to be called */
00675     l->private_data = value;
00676     l->next = desktop->l_list;
00677     desktop->l_list = l;
00678     desktop->listener_count++;
00679     return l->listener_id;
00680 }
00681 
00682 iml_listener_t *
00683 lookup_listener_from_desktop(
00684     iml_session_t *s,
00685     int id
00686 )
00687 {
00688     iml_desktop_t *desktop = s->desktop;
00689     iml_listener_list p, *prev;
00690 
00691     for (prev = &desktop->l_list; (p = *prev) != 0; prev = &p->next) {
00692         if (p->listener_id == id) {
00693             return p;
00694         }
00695     }
00696 
00697     return (iml_listener_t *)NULL;
00698 }
00699 
00700 void 
00701 delete_listener_from_desktop(
00702     iml_session_t *s,
00703     int id
00704 )
00705 {
00706     iml_desktop_t *desktop = s->desktop;
00707     iml_listener_list p, *prev;
00708 
00709     for (prev = &desktop->l_list; (p = *prev) != 0; prev = &p->next) {
00710         if (p->listener_id == id) {
00711             *prev = p->next;
00712             free(p->listener_name);
00713             free(p);
00714             desktop->listener_count--;
00715             break;
00716         }
00717         if (p->next == NULL) {
00718            break;
00719         }
00720     }
00721     return;
00722 }
00723 
00724 Public char*
00725 secure_pathname(
00726     char *p
00727 )
00728 {
00729     char *q ;
00730     int index = 0;
00731 
00732     while (*p) {
00733         if ((q = (strstr(p, "../")))) {
00734             index = p - q;
00735             if (index < 0)
00736                 index = -index;
00737             strcpy(p+index, p+index+3);
00738         } else if ((q = (strstr(p, "//")))) {
00739             index = p - q;
00740             if (index < 0)
00741                 index = -index;
00742             strcpy(p+index, p+index+1);
00743         } else if ((q = (strstr(p, "./")))) {
00744             index = p - q;
00745             if (index < 0)
00746                 index = -index;
00747             strcpy(p+index, p+index+2);
00748         } else {
00749             break ;
00750         }
00751     }
00752     return p;
00753 }
00754 
00755 iml_file_status_t *
00756 get_fs_by_id(
00757     iml_nsc *nsc,
00758     int ns_id
00759 )
00760 {
00761     file_status_list fsl, *prev;
00762     iml_nsc_private *imlp = nsc->value;
00763 
00764     for (prev = &imlp->fslist; (fsl = *prev) != 0; prev = &fsl->next) {
00765        if (fsl->ns_id == ns_id) {
00766            return fsl;
00767        }
00768     }
00769     return (iml_file_status_t *) NULL;
00770 }
00771 
00772 void
00773 del_fs_by_id(
00774     iml_nsc *nsc,
00775     int ns_id
00776 )
00777 {
00778     iml_file_status_t *temp, *prev;
00779 
00780     if (nsc->value->fslist) {
00781        temp = get_fs_by_id(nsc, ns_id);
00782        prev = nsc->value->fslist;
00783        if (temp == prev) {
00784            if (temp->next == NULL)
00785               free(temp);
00786            else {
00787               prev = temp->next;
00788               free(temp);
00789               nsc->value->fslist = prev;
00790            }
00791        } else {
00792            while ((prev->next != temp) && (prev->next != NULL)) {
00793               prev = prev->next;
00794            }
00795            prev->next = temp->next;
00796            if (temp->next == NULL)
00797               temp->next = prev;
00798            free(temp);
00799        }
00800     }
00801 }
00802 
00803 int
00804 ns_open(
00805     iml_nsc *nsc,
00806     const char *defRepository,
00807     int oflag,
00808     ...
00809 )
00810 {
00811     va_list ap;
00812     int fd, ns_errno;
00813     iml_file_status_t *fs;
00814     mode_t mode;
00815     char *realRepository;
00816     iml_desktop_t *desktop = NULL;
00817 
00818     if (0 != (O_CREAT & oflag)) {
00819        va_start(ap, oflag);
00820        mode = va_arg(ap, mode_t);
00821        va_end(ap);
00822     } else {
00823        mode = 0;
00824     }
00825     nsc->value->location = lookup_config(nsc, defRepository, &realRepository );
00826 
00827     fs = (iml_file_status_t *) calloc(1, sizeof(iml_file_status_t));
00828     unique_ns_id++;
00829     fs->ns_id = unique_ns_id;
00830     fs->nitems = 0;
00831     fs->d_name = (char **)NULL;
00832     fs->d_reclen = 0;
00833     fs->next = nsc->value->fslist;
00834     fs->path_name = (char *) calloc(strlen(realRepository), sizeof(char));
00835     fs->path_name = secure_pathname(realRepository);
00836     nsc->value->fslist = fs;
00837 
00838     switch (nsc->value->location) {
00839       case NS_LOCAL:
00840       case NS_DATABASE:
00841       {
00842         if ((fs->fd = open(fs->path_name, oflag, mode)) < 0) {
00843           return fs->fd;
00844         } else return fs->ns_id;
00845       }
00846 
00847       case NS_REMOTE:
00848        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
00849          desktop = nsc->value->ns_owner;
00850        }
00851        fd = open_ns(desktop, fs->ns_id, fs->path_name, oflag, mode, &ns_errno);
00852        errno = ns_errno;
00853        return fd;
00854 
00855       default:
00856       {
00857         if ((fs->fd = open(fs->path_name, oflag, mode)) < 0) {
00858           return fs->fd;
00859         } else return fs->ns_id;
00860       }
00861     }
00862 }
00863 
00864 size_t
00865 ns_read(
00866     iml_nsc *nsc,
00867     int ns_id,
00868     void *ptr,
00869     size_t size
00870 )
00871 {
00872     int ret, ns_errno;
00873     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
00874     iml_desktop_t *desktop = NULL;
00875 
00876     switch (nsc->value->location) {
00877       case NS_LOCAL:
00878       case NS_DATABASE:
00879        return read(fs->fd, ptr, size);
00880 
00881       case NS_REMOTE:
00882        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
00883          desktop = nsc->value->ns_owner;
00884        }
00885        ret = read_ns(desktop, ns_id, ptr, size, &ns_errno);
00886        errno = ns_errno;
00887        break;
00888 
00889       default:
00890        return read(fs->fd, ptr, size);
00891     }
00892     return ret;
00893 }
00894 
00895 int
00896 ns_stat(
00897     iml_nsc *nsc,
00898     const char *file_name,
00899     struct stat *buf
00900 )
00901 {
00902     int ret, ns_errno, ns_id, location;
00903     char *realRepository;
00904     iml_desktop_t *desktop = NULL;
00905 
00906     location = lookup_config(nsc, file_name, &realRepository );
00907 
00908     switch (location) {
00909       case NS_LOCAL:
00910       case NS_DATABASE:
00911        return stat(secure_pathname(realRepository), buf);
00912 
00913       case NS_REMOTE:
00914        unique_ns_id++;
00915        ns_id = unique_ns_id;
00916        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
00917          desktop = nsc->value->ns_owner;
00918        }
00919        ret = stat_ns(desktop, ns_id, realRepository, buf, &ns_errno);
00920        errno = ns_errno;
00921        return ret;
00922 
00923       default:
00924        return stat(secure_pathname(realRepository), buf);
00925     }
00926 }
00927 
00928 int
00929 ns_lstat(
00930     iml_nsc *nsc,
00931     const char *file_name,
00932     struct stat *buf
00933 )
00934 {
00935     int ret, ns_errno, ns_id, location;
00936     char *realRepository;
00937     iml_desktop_t *desktop = NULL;
00938 
00939     location = lookup_config(nsc, file_name, &realRepository );
00940 
00941     switch (location) {
00942       case NS_LOCAL:
00943       case NS_DATABASE:
00944        return lstat(secure_pathname(realRepository), buf);
00945 
00946       case NS_REMOTE:
00947        unique_ns_id++;
00948        ns_id = unique_ns_id;
00949        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
00950          desktop = nsc->value->ns_owner;
00951        }
00952        ret = lstat_ns(desktop, ns_id, realRepository, buf, &ns_errno);
00953        errno = ns_errno;
00954        return ret;
00955 
00956       default:
00957        return lstat(secure_pathname(realRepository), buf);
00958     }
00959 }
00960 
00961 size_t
00962 ns_write(
00963     iml_nsc *nsc,
00964     int ns_id,
00965     void *ptr,
00966     size_t size
00967 )
00968 {
00969     int ret, ns_errno;
00970     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
00971     iml_desktop_t *desktop = NULL;
00972 
00973     switch (nsc->value->location) {
00974       case NS_LOCAL:
00975       case NS_DATABASE:
00976        return write(fs->fd, ptr, size);
00977 
00978       case NS_REMOTE:
00979        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
00980          desktop = nsc->value->ns_owner;
00981        }
00982        ret = write_ns(desktop, ns_id, ptr, size, &ns_errno);
00983        errno = ns_errno;
00984        break;
00985 
00986       default:
00987        return write(fs->fd, ptr, size);
00988     }
00989     return ret;
00990 }
00991 
00992 int
00993 ns_close(
00994     iml_nsc *nsc,
00995     int ns_id
00996 )
00997 {
00998     int ret, ns_errno;
00999     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
01000     iml_desktop_t *desktop = NULL;
01001 
01002     switch (nsc->value->location) {
01003       case NS_LOCAL:
01004       case NS_DATABASE:
01005        ret = close(fs->fd);
01006        break;
01007 
01008       case NS_REMOTE:
01009        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01010          desktop = nsc->value->ns_owner;
01011        }
01012        ret = close_ns(desktop, ns_id, &ns_errno);
01013        errno = ns_errno;
01014        break;
01015 
01016       default:
01017        ret = close(fs->fd);
01018        break;
01019     }
01020 
01021     del_fs_by_id (nsc, ns_id);
01022 
01023     return ret;
01024 }
01025 
01026 DIR * 
01027 ns_opendir(
01028     iml_nsc *nsc,
01029     const char *defRepository
01030 )
01031 {
01032     int pns_id, ns_errno;
01033     iml_file_status_t *fs;
01034     char *realRepository;
01035     iml_desktop_t *desktop = NULL;
01036 
01037     dirID=0;
01038 
01039     nsc->value->location = lookup_config(nsc, defRepository, &realRepository );
01040 
01041     fs = (iml_file_status_t *) calloc(1, sizeof(iml_file_status_t));
01042     unique_ns_id++;
01043     fs->ns_id = unique_ns_id;
01044     fs->nitems = 0;
01045     fs->d_name = (char **)NULL;
01046     fs->d_reclen = 0;
01047     fs->path_name = (char *) calloc(strlen(realRepository), sizeof(char));
01048     fs->path_name = secure_pathname(realRepository);
01049     fs->next = nsc->value->fslist;
01050     nsc->value->fslist = fs;
01051 
01052     switch (nsc->value->location) {
01053       case NS_LOCAL:
01054       case NS_DATABASE:
01055       {
01056         if ((fs->dirp = opendir(fs->path_name)) == NULL) {
01057           return fs->dirp;
01058         } else 
01059             return (DIR *)fs->ns_id;
01060       }
01061 
01062       case NS_REMOTE:
01063        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01064          desktop = nsc->value->ns_owner;
01065        }
01066        fs->d_name = opendir_ns(desktop, fs->ns_id, fs->path_name, &fs->nitems, &pns_id, &fs->d_reclen, &ns_errno);
01067        errno = ns_errno;
01068        return (DIR *)pns_id;
01069 
01070       default:
01071       {
01072         if ((fs->dirp = opendir(fs->path_name)) == NULL) {
01073           return fs->dirp;
01074         } else 
01075             return (DIR *)fs->ns_id;
01076       }
01077     }
01078 }
01079 
01080 struct dirent * 
01081 ns_readdir(
01082     iml_nsc *nsc,
01083     DIR *dirp
01084 )
01085 {
01086     struct dirent *p;
01087     int ns_id = (int) dirp;
01088     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
01089 
01090     switch (nsc->value->location) {
01091       case NS_LOCAL:
01092       case NS_DATABASE:
01093        return readdir(fs->dirp);
01094 
01095       case NS_REMOTE:
01096        /* TODO!! Need to get the errno for readdir() */
01097        errno = 0;
01098        if (dirID < fs->nitems) {
01099           p = (struct dirent *) calloc(1, sizeof(struct dirent));
01100           p->d_reclen = (int)fs->d_reclen[dirID];
01101           strncpy(p->d_name, fs->d_name[dirID], p->d_reclen);
01102           dirID++;
01103           return p;
01104        }
01105        dirID = 0;
01106        return (struct dirent *)NULL;
01107 
01108       default:
01109        return readdir(fs->dirp);
01110     }
01111 }
01112 
01113 int 
01114 ns_closedir(
01115     iml_nsc *nsc,
01116     DIR *dirp
01117 )
01118 {
01119     int ns_id, ret, ns_errno;
01120     ns_id = (int) dirp;
01121     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
01122     iml_desktop_t *desktop = NULL;
01123 
01124     switch (nsc->value->location) {
01125       case NS_LOCAL:
01126       case NS_DATABASE:
01127        ret = closedir(fs->dirp);
01128        break;
01129 
01130       case NS_REMOTE:
01131        ns_id = (int) dirp;
01132        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01133          desktop = nsc->value->ns_owner;
01134        }
01135        ret = closedir_ns(desktop, ns_id, &ns_errno);
01136        errno = ns_errno;
01137        break;
01138 
01139       default:
01140        ret = closedir(fs->dirp);
01141        break;
01142     }
01143     del_fs_by_id (nsc, ns_id);
01144     return ret;
01145 }
01146 
01147 int 
01148 ns_mkdir(
01149     iml_nsc *nsc,
01150     const char *path,
01151     mode_t mode
01152 )
01153 {
01154     int ret, ns_errno, ns_id, location;
01155     char *realRepository;
01156     iml_desktop_t *desktop = NULL;
01157 
01158     location = lookup_config(nsc, path, &realRepository );
01159 
01160     switch (location) {
01161       case NS_LOCAL:
01162       case NS_DATABASE:
01163        return mkdir(secure_pathname(realRepository), mode);
01164 
01165       case NS_REMOTE:
01166        unique_ns_id++;
01167        ns_id = unique_ns_id;
01168        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01169          desktop = nsc->value->ns_owner;
01170        }
01171        ret = mkdir_ns(desktop, ns_id, realRepository, mode, &ns_errno);
01172        errno = ns_errno;
01173        return ret;
01174 
01175       default:
01176        return mkdir(secure_pathname(realRepository), mode);
01177     }
01178 }
01179 
01180 int
01181 ns_rmdir(
01182     iml_nsc *nsc,
01183     const char *path
01184 )
01185 {
01186     int ret, ns_id, ns_errno, location;
01187     char *realRepository;
01188     iml_desktop_t *desktop = NULL;
01189 
01190     location = lookup_config(nsc, path, &realRepository );
01191 
01192     switch (nsc->value->location) {
01193       case NS_LOCAL:
01194       case NS_DATABASE:
01195        return rmdir(secure_pathname(realRepository));
01196 
01197       case NS_REMOTE:
01198        unique_ns_id++;
01199        ns_id = unique_ns_id;
01200        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01201          desktop = nsc->value->ns_owner;
01202        }
01203        ret = rmdir_ns(desktop, ns_id, realRepository, &ns_errno);
01204        errno = ns_errno;
01205        return ret;
01206 
01207       default:
01208        return rmdir(secure_pathname(realRepository));
01209     }
01210 }
01211 
01212 int 
01213 ns_symlink(
01214     iml_nsc *nsc,
01215     const char *dest_path,
01216     const char *src_path
01217 )
01218 {
01219     int ret, ns_errno, ns_id, destL, srcL;
01220     char *destRepository, *srcRepository;
01221     iml_desktop_t *desktop = NULL;
01222 
01223     destL = lookup_config(nsc, dest_path, &destRepository );
01224     srcL = lookup_config(nsc, dest_path, &srcRepository );
01225 
01226     if ((destL == NS_REMOTE) || (srcL == NS_REMOTE)) {
01227        destL = NS_REMOTE;
01228     }
01229 
01230     switch (destL) {
01231       case NS_LOCAL:
01232       case NS_DATABASE:
01233        return symlink(secure_pathname(destRepository), secure_pathname(srcRepository));
01234 
01235       case NS_REMOTE:
01236        unique_ns_id++;
01237        ns_id = unique_ns_id;
01238        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01239          desktop = nsc->value->ns_owner;
01240        }
01241        ret = symlink_ns(desktop, ns_id, destRepository, srcRepository, &ns_errno);
01242        errno = ns_errno;
01243        return ret;
01244 
01245       default:
01246        return symlink(secure_pathname(destRepository), secure_pathname(srcRepository));
01247     }
01248 }
01249 
01250 int
01251 ns_fstat(
01252     iml_nsc *nsc,
01253     int ns_id,
01254     struct stat *buf
01255 )
01256 {
01257     int ns_errno, ret;
01258     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
01259     iml_desktop_t *desktop = NULL;
01260 
01261     switch (nsc->value->location) {
01262       case NS_LOCAL:
01263       case NS_DATABASE:
01264        return fstat(fs->fd, buf);
01265 
01266       case NS_REMOTE:
01267        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01268          desktop = nsc->value->ns_owner;
01269        }
01270        ret = fstat_ns(desktop, ns_id, buf, &ns_errno);
01271        errno = ns_errno;
01272        return ret;
01273 
01274       default:
01275        return fstat(fs->fd, buf);
01276     }
01277 }
01278 
01279 int 
01280 ns_creat(
01281     iml_nsc *nsc,
01282     const char *defRepository,
01283     mode_t mode
01284 )
01285 {
01286     int fd;
01287     iml_file_status_t *fs = (iml_file_status_t *) calloc(1, sizeof(iml_file_status_t));
01288     int ns_errno;
01289     char *realRepository;
01290     iml_desktop_t *desktop = NULL;
01291 
01292     unique_ns_id++;
01293     fs->ns_id = unique_ns_id;
01294     fs->nitems = 0;
01295     fs->d_name = (char **)NULL;
01296     fs->d_reclen = 0;
01297     fs->next = nsc->value->fslist;
01298     nsc->value->location = lookup_config(nsc, defRepository, &realRepository );
01299     fs->path_name = (char *) calloc(strlen(realRepository), sizeof(char));
01300     fs->path_name = secure_pathname(realRepository);
01301     nsc->value->fslist = fs;
01302 
01303     switch (nsc->value->location) {
01304       case NS_LOCAL:
01305       case NS_DATABASE:
01306       {
01307         if ((fs->fd = creat(fs->path_name, mode)) < 0) {
01308           return fs->fd;
01309         } else return fs->ns_id;
01310       }
01311 
01312       case NS_REMOTE:
01313        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01314          desktop = nsc->value->ns_owner;
01315        }
01316        fd = creat_ns(desktop, fs->ns_id, fs->path_name, mode, &ns_errno);
01317        errno = ns_errno;
01318        return fd;
01319 
01320       default:
01321       {
01322         if ((fs->fd = creat(fs->path_name, mode)) < 0) {
01323           return fs->fd;
01324         } else return fs->ns_id;
01325       }
01326     }
01327 }
01328 
01329 off_t 
01330 ns_lseek(
01331     iml_nsc *nsc,
01332     int ns_id,
01333     off_t offset,
01334     int whence
01335 )
01336 {
01337     int ret, ns_errno;
01338     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
01339     iml_desktop_t *desktop = NULL;
01340 
01341     switch (nsc->value->location) {
01342       case NS_LOCAL:
01343       case NS_DATABASE:
01344        return lseek(fs->fd, offset, whence);
01345 
01346       case NS_REMOTE:
01347        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01348          desktop = nsc->value->ns_owner;
01349        }
01350        ret = lseek_ns(desktop, ns_id, offset, whence, &ns_errno);
01351        errno = ns_errno;
01352        return ret;
01353 
01354       default:
01355        return lseek(fs->fd, offset, whence);
01356     }
01357 }
01358 
01359 int 
01360 ns_unlink(
01361     iml_nsc *nsc,
01362     const char *path
01363 )
01364 {
01365     int ret, ns_errno, ns_id, location;
01366     char *realRepository;
01367     iml_desktop_t *desktop = NULL;
01368 
01369     location = lookup_config(nsc, path, &realRepository );
01370 
01371     switch (location) {
01372       case NS_LOCAL:
01373       case NS_DATABASE:
01374        return unlink(secure_pathname(realRepository));
01375 
01376       case NS_REMOTE:
01377        unique_ns_id++;
01378        ns_id = unique_ns_id;
01379        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01380          desktop = nsc->value->ns_owner;
01381        }
01382        ret = unlink_ns(desktop, ns_id, realRepository, &ns_errno);
01383        errno = ns_errno;
01384        return ret;
01385 
01386       default:
01387        return unlink(secure_pathname(realRepository));
01388     }
01389 }
01390 
01391 int 
01392 ns_rename(
01393     iml_nsc *nsc,
01394     const char *old_name,
01395     const char *new_name
01396 )
01397 {
01398     int ns_id, ret, ns_errno, oldL, newL;
01399     char *oldRepository, *newRepository;
01400     iml_desktop_t *desktop = NULL;
01401 
01402     oldL = lookup_config(nsc, old_name, &oldRepository );
01403     newL = lookup_config(nsc, new_name, &newRepository );
01404 
01405     if ((oldL == NS_REMOTE) || (newL == NS_REMOTE)) {
01406        oldL = NS_REMOTE;
01407     }
01408 
01409     switch (oldL) {
01410       case NS_LOCAL:
01411       case NS_DATABASE:
01412        return rename(secure_pathname(oldRepository), secure_pathname(newRepository));
01413 
01414       case NS_REMOTE:
01415        unique_ns_id++;
01416        ns_id = unique_ns_id;
01417        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01418          desktop = nsc->value->ns_owner;
01419        }
01420        ret = rename_ns(desktop, ns_id, oldRepository, newRepository, &ns_errno);
01421        errno = ns_errno;
01422        return ret;
01423 
01424       default:
01425        return rename(secure_pathname(oldRepository), secure_pathname(newRepository));
01426     }
01427 }
01428 
01429 int 
01430 ns_fcntl(
01431     iml_nsc *nsc,
01432     int ns_id,
01433     int cmd,
01434     int arg
01435 )
01436 {
01437     int ret, ns_errno;
01438     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
01439     iml_desktop_t *desktop = NULL;
01440 
01441     switch (nsc->value->location) {
01442       case NS_LOCAL:
01443       case NS_DATABASE:
01444        return fcntl(fs->fd, cmd, arg);
01445 
01446       case NS_REMOTE:
01447        unique_ns_id++;
01448        ns_id = unique_ns_id;
01449        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01450          desktop = nsc->value->ns_owner;
01451        }
01452        ret = fcntl_ns(desktop, ns_id, cmd, arg, &ns_errno);
01453        errno = ns_errno;
01454        return ret;
01455 
01456       default:
01457        return fcntl(fs->fd, cmd, arg);
01458     }
01459 }
01460 
01461 int 
01462 ns_truncate(
01463     iml_nsc *nsc,
01464     const char *path,
01465     off_t length
01466 )
01467 {
01468     int ns_id, ret, ns_errno, location;
01469     char *realRepository;
01470     iml_desktop_t *desktop = NULL;
01471 
01472     location = lookup_config(nsc, path, &realRepository );
01473 
01474     switch (location) {
01475       case NS_LOCAL:
01476       case NS_DATABASE:
01477        return truncate(secure_pathname(realRepository), length);
01478 
01479       case NS_REMOTE:
01480        unique_ns_id++;
01481        ns_id = unique_ns_id;
01482        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01483          desktop = nsc->value->ns_owner;
01484        }
01485        ret = truncate_ns(desktop, ns_id, realRepository, length, &ns_errno);
01486        errno = ns_errno;
01487        return ret;
01488 
01489       default:
01490        return truncate(secure_pathname(realRepository), length);
01491     }
01492 }
01493 
01494 int 
01495 ns_ftruncate(
01496     iml_nsc *nsc,
01497     int ns_id,
01498     off_t length
01499 )
01500 {
01501     int ret, ns_errno;
01502     iml_file_status_t *fs = get_fs_by_id(nsc, ns_id);
01503     iml_desktop_t *desktop = NULL;
01504 
01505     switch (nsc->value->location) {
01506       case NS_LOCAL:
01507       case NS_DATABASE:
01508        return ftruncate(fs->fd, length);
01509 
01510       case NS_REMOTE:
01511        if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01512          desktop = nsc->value->ns_owner;
01513        }
01514        ret = ftruncate_ns(desktop, ns_id, length, &ns_errno);
01515        errno = ns_errno;
01516        return ret;
01517 
01518       default:
01519        return ftruncate(fs->fd, length);
01520     }
01521 }
01522 
01523 Private int
01524 lookup_config(
01525     iml_nsc *nsc,
01526     const char *default_path,
01527     char **real_path
01528 )
01529 {
01530     int i;
01531     iml_if_t *If = 0;
01532 
01533     if (nsc->value->ns_type == IML_NSC_TYPE_LE) {
01534        If = nsc->value->ns_owner;
01535     } else if (nsc->value->ns_type == IML_NSC_TYPE_DESKTOP) {
01536        iml_desktop_t *desktop = nsc->value->ns_owner;
01537        If = desktop->If;
01538     }
01539     
01540     if (!If->num_nsm_entries) {
01541        *real_path = (char *) default_path;
01542        return NS_LOCAL;
01543     }
01544 
01545     for (i=0; i<If->num_nsm_entries; i++) {
01546        if (!strcmp(If->ns_map[i].src_entry, default_path)) {
01547            *real_path = If->ns_map[i].dest_entry;
01548            if (!strcmp(If->ns_map[i].location, "REMOTE")) {
01549               return NS_REMOTE;
01550            } 
01551        }
01552     }
01553 
01554     *real_path = (char *)default_path;
01555 
01556     return NS_LOCAL;
01557 
01558 }
01559 
01560 Private int 
01561 updateSupportedLocales(
01562     iml_desktop_t *desktop,
01563     IMLEName *lename,
01564     IMLocale *locales,
01565     int nlocale
01566 )
01567 { 
01568     return update_supported_langlist_for_le(desktop, lename, locales, nlocale);
01569 }
01570 
01571 Private int 
01572 switchLEProfile(
01573     iml_session_t *s,
01574     int le_profile_id,
01575     IMLEName *lename
01576 )
01577 {
01578     return switch_le_profile(s, le_profile_id, lename);
01579 }
01580 
01581 Private void *
01582 ns_create(
01583     const char *le_name,
01584     int ns_type, 
01585     void *ns_owner
01586 )
01587 {
01588     iml_nsc *nsc;
01589 
01590     nsc = (iml_nsc *) calloc(1, sizeof(iml_nsc));
01591     nsc->value = (iml_nsc_private *) calloc(1, sizeof(iml_nsc_private));
01592     nsc->value->ns_type = ns_type;
01593     nsc->value->ns_owner = ns_owner;
01594     nsc->value->fslist = 0;
01595 
01596     return nsc;
01597 }
01598 
01599 Private void *
01600 hk_create(
01601     const char *le_name,
01602     int hk_type, 
01603     void *hk_owner
01604 )
01605 {
01606     iml_hkc *hkc;
01607 
01608     hkc = (iml_hkc *) calloc(1, sizeof(iml_hkc));
01609     hkc->value = (iml_hkc_private *) calloc(1, sizeof(iml_hkc_private));
01610     hkc->value->hk_type = hk_type;
01611     hkc->value->hk_owner = hk_owner;
01612 
01613     return hkc;
01614 }
01615 
01616 Private void 
01617 ns_free(
01618     iml_nsc *nsc
01619 )
01620 {
01621     if (nsc) free(nsc);
01622     return;
01623 }
01624 
01625 Private void 
01626 hk_free(
01627     iml_hkc *hkc
01628 )
01629 {
01630     if (hkc) free(hkc);
01631     return;
01632 }
01633 
01634 Private void * 
01635 nsc_GetFunction(
01636     const char *name
01637 )
01638 {
01639     name_fn_map *p;
01640     
01641     if (!strcmp(name, "_nsc_basicfioset")) {
01642        iml_nsc_basic_fioset_t *bfioset;
01643        bfioset = (iml_nsc_basic_fioset_t *) calloc(1, sizeof(iml_nsc_basic_fioset_t));
01644        bfioset->open = ns_open;
01645        bfioset->read = ns_read;
01646        bfioset->stat = ns_stat;
01647        bfioset->write = ns_write;
01648        bfioset->close = ns_close;
01649        bfioset->mkdir = ns_mkdir;
01650        bfioset->rmdir = ns_rmdir;
01651        bfioset->symlink = ns_symlink;
01652        bfioset->lstat = ns_lstat;
01653        bfioset->creat = ns_creat;
01654        bfioset->lseek = ns_lseek;
01655        bfioset->unlink = ns_unlink;
01656        bfioset->rename = ns_rename;
01657        bfioset->fcntl = ns_fcntl;
01658        bfioset->truncate = ns_truncate;
01659        bfioset->opendir = ns_opendir;
01660        bfioset->readdir = ns_readdir;
01661        bfioset->closedir = ns_closedir;
01662        bfioset->fstat = ns_fstat;
01663        bfioset->ftruncate = ns_ftruncate;
01664        return bfioset;
01665     }
01666 
01667     for (p = iof_map_list; p != 0; p++) {
01668        if (!strcmp(p->name, name)) {
01669            return p->fn_ptr;
01670        }
01671     }
01672     return (void *)NULL;
01673 }
01674 
01675 Private void * 
01676 hkc_GetFunction(
01677     const char *name
01678 )
01679 {
01680     name_fn_map *p;
01681     
01682     for (p = hkf_map_list; p != 0; p++) {
01683        if (!strcmp(p->name, name)) {
01684            return p->fn_ptr;
01685        }
01686     }
01687     return (void *)NULL;
01688 }
01689 
01690 iml_desktop_t *
01691 new_user(
01692     iml_if_t * If,
01693     const char *user_name,
01694     const char *host_name,
01695     const char *display_id
01696 )
01697 {
01698     iml_desktop_t *p = (iml_desktop_t *) calloc(1, sizeof(iml_desktop_t));
01699 
01700     p->addListenerToDesktop = add_listener_to_desktop;
01701     p->lookupListenerFromDesktop = lookup_listener_from_desktop;
01702     p->deleteListenerFromDesktop = delete_listener_from_desktop;
01703 
01704     p->user_name = strdup(user_name);
01705     p->host_name = strdup(host_name);
01706     p->display_id = strdup(display_id);
01707     p->next = If->desktop_list;
01708     p->If = If;
01709     If->desktop_list = p;
01710     If->desktop_count++;
01711 #ifdef DEBUG
01712     printf("LE %s: %s@%s is entered\n", If->if_name, user_name, host_name);
01713 #endif
01714     return (iml_desktop_t *) p;
01715 }
01716 
01717 void
01718 remove_session_from_desktop(
01719     iml_session_t * s
01720 )
01721 {
01722     iml_session_list p, *prev;
01723     for (prev = &s->desktop->session_list; (p = *prev) != 0; prev = &p->next) {
01724         if (p == s) {
01725             *prev = p->next;
01726             s->desktop->session_count--;
01727             break;
01728         }
01729         if (p->next == NULL) {
01730             break;
01731         }
01732     }
01733 }
01734 
01735 void
01736 add_session_to_desktop(
01737     iml_session_t * s
01738 )
01739 {
01740     s->next = s->desktop->session_list;
01741     s->desktop->session_list = s;
01742     s->desktop->session_count++;
01743 }
01744 
01745 /* Local Variables: */
01746 /* c-file-style: "iiim-project" */
01747 /* End: */