//stop homie PUBLIC RETCODE HomieStop(hHOMIE homie, hGMAP map) { CLEARFLAG(homie->status, HOMIE_MOVING); CLEARFLAG(homie->status, HOMIE_STUCK); //adjust to current tile hTILE tile = &CELL(map->tiles, homie->indY, homie->indX, map->indSizeX); //check if the tile is one of the arrows if(tile->type >= eTILE_ARROW_N && tile->type <= eTILE_ARROW_W) { SETFLAG(homie->status, HOMIE_STUCK); OBJSetState(homie->obj, eHOMIESTATE_STAND); } //else if(tile->type == eTILE_FLOOR) // OBJSetState(homie->obj, eHOMIESTATE_STAND); else if(tile->type == eTILE_GOAL && tile->homieType == homie->type) OBJSetState(homie->obj, eHOMIESTATE_CELEBRATE); else OBJSetState(homie->obj, eHOMIESTATE_STAND); float homieLoc[eMaxPt]; OBJGetLoc(homie->obj, homieLoc); float tileLoc[eMaxPt]; OBJGetLoc(tile->obj, tileLoc); OBJSetLoc(homie->obj, tileLoc[eX], homieLoc[eY], tileLoc[eZ]); SETFLAG(tile->status, TILE_OCCUPIED); return RETCODE_SUCCESS; }
///////////////////////////////////// // Name: // Purpose: // Output: // Return: ///////////////////////////////////// PUBLIC void OBJPauseOnStateEnd(hOBJ obj, bool bPause) { if(bPause) SETFLAG(obj->status, OBJ_PAUSE_ON_END); else { CLEARFLAG(obj->status, OBJ_PAUSE_ON_END); CLEARFLAG(obj->status, OBJ_PAUSE); } }
///////////////////////////////////// // Name: OBJPause // Purpose: pause / resume object // (true) (false) // Output: Obj flag is set // Return: none ///////////////////////////////////// PUBLIC void OBJPause(hOBJ obj, bool bPause) { if(bPause) SETFLAG(obj->status, OBJ_PAUSE); else CLEARFLAG(obj->status, OBJ_PAUSE); }
//move homie PUBLIC RETCODE HomieMove(hHOMIE homie, eDIR dir, hGMAP map) { if(!TESTFLAGS(homie->status, HOMIE_MOVING) && !TESTFLAGS(homie->status, HOMIE_STUCK) && !TESTFLAGS(homie->status, HOMIE_DEAD)) { switch(dir) { case eDIR_SOUTH: OBJSetOrientation(homie->obj, 0, 0, GCFGGetHomieSpd()); break; case eDIR_NORTH: OBJSetOrientation(homie->obj, 0, 0, -GCFGGetHomieSpd()); break; case eDIR_EAST: OBJSetOrientation(homie->obj, -GCFGGetHomieSpd(), 0, 0); break; case eDIR_WEST: OBJSetOrientation(homie->obj, GCFGGetHomieSpd(), 0, 0); break; } OBJSetState(homie->obj, eHOMIESTATE_MOVE); SETFLAG(homie->status, HOMIE_MOVING); hTILE tile = &CELL(map->tiles, homie->indY, homie->indX, map->indSizeX); CLEARFLAG(tile->status, TILE_OCCUPIED); } return RETCODE_SUCCESS; }
///////////////////////////////////// // Purpose: enable/disable model // If disabled, model will // not perform collision // and will not be displayed // Output: model status set // Return: none ///////////////////////////////////// void IgfxQBSP::ModelEnable(u32 modelInd, u8 bEnable) { if(bEnable) CLEARFLAG(m_models[modelInd].status, QBSP_MODEL_FLAG_DISABLE); else SETFLAG(m_models[modelInd].status, QBSP_MODEL_FLAG_DISABLE); }
///////////////////////////////////// // Purpose: enable/disable shadow // volume for given object // Output: set/clear shadow flag // Return: none ///////////////////////////////////// void IgfxObject::EnableShadow(u8 bYes) { if(bYes) SETFLAG(m_flag, OBJECT_SHADOW); else CLEARFLAG(m_flag, OBJECT_SHADOW); }
///////////////////////////////////// // Purpose: enable/disable shadow // receiving // Output: set/clear shadow flag // Return: none ///////////////////////////////////// void IgfxObject::ReceiveShadow(u8 bYes) { if(bYes) CLEARFLAG(m_flag, OBJECT_NORECSHADOW); else SETFLAG(m_flag, OBJECT_NORECSHADOW); }
///////////////////////////////////// // Name: LightEnableRender // Purpose: enable/disable lighting // Output: light enabled/disabled // Return: none ///////////////////////////////////// void F_API LightEnableRender(u8 bYes) { if(bYes) SETFLAG(g_FLAGS, GFX_LIGHTENABLE); else CLEARFLAG(g_FLAGS, GFX_LIGHTENABLE); g_p3DDevice->SetRenderState(D3DRS_LIGHTING,bYes); }
///////////////////////////////////// // Name: OBJRemoveFromParent // Purpose: remove object from it's // parent, if it has one // Output: parent free object // Return: success if success ///////////////////////////////////// PUBLIC RETCODE OBJRemoveFromParent(hOBJ obj) { if(obj && TESTFLAGS(obj->status, OBJ_CHILD)) //does it still exist? It damn better! { OBJDestroy(&obj); //this will just subtract the child ref //remove child from it's refered list //in this case, it would be obj's children list obj->listRef->remove((unsigned int)obj); //put the child back to the global object list g_OBJLIST.push_back((unsigned int)obj); obj->listRef = &g_OBJLIST; CLEARFLAG(obj->status, OBJ_CHILD); CLEARFLAG(obj->status, OBJ_SYNCSTATE); } return RETCODE_SUCCESS; }
///////////////////////////////////// // Name: OBJDeactivate // Purpose: activates obj // Output: stuff happens // Return: none ///////////////////////////////////// PUBLIC void OBJActivate(hOBJ obj) { if(OBJIsDeactivate(obj)) { //remove object from previous list obj->listRef->remove((unsigned int)obj); //add object to deactivated object list g_OBJLIST.insert(g_OBJLIST.end(), (unsigned int)obj); obj->listRef = &g_OBJLIST; CLEARFLAG(obj->status, OBJ_DEACTIVATE); } }
///////////////////////////////////// // Name: // Purpose: // Output: // Return: ///////////////////////////////////// PUBLIC RETCODE OBJRemoveChild(hOBJ obj, hOBJ child) { //we will assume that the child was added to the given obj //before this function. OBJDestroy(&child); //this will just subtract the child ref if(child) //does it still exist? It damn better! { //remove child from it's refered list //in this case, it would be obj's children list child->listRef->remove((unsigned int)child); //put the child back to the global object list g_OBJLIST.insert(g_OBJLIST.end(), (unsigned int)child); child->listRef = &g_OBJLIST; } CLEARFLAG(child->status, OBJ_CHILD); CLEARFLAG(child->status, OBJ_SYNCSTATE); return RETCODE_SUCCESS; }
///////////////////////////////////// // Name: GFXEnableLight // Purpose: enable/disable light // also set ambient color // Output: light is enabled/disabled // Return: none ///////////////////////////////////// PUBLIC void GFXEnableLight(bool do_you_want_light) { //set light if(do_you_want_light) { g_p3DDevice->SetRenderState(D3DRS_LIGHTING,TRUE); CLEARFLAG(g_FLAGS, GFX_LIGHTDISABLE); } else { g_p3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE); SETFLAG(g_FLAGS, GFX_LIGHTDISABLE); } }
///////////////////////////////////// // Name: OBJDeactivate // Purpose: activates/deactivates obj // Output: stuff happens // Return: none ///////////////////////////////////// PUBLIC void OBJActivate(hOBJ obj, bool bActivate) { //remove child from it's refered list if(TESTFLAGS(obj->status, OBJ_CHILD)) { OBJDestroy(&obj); //this will just subtract the child ref CLEARFLAG(obj->status, OBJ_CHILD); CLEARFLAG(obj->status, OBJ_SYNCSTATE); //OBJRemoveFromParent(obj); } if(bActivate) { if(OBJIsDeactivate(obj)) { //remove object from previous list obj->listRef->remove((unsigned int)obj); //add object to main object list g_OBJLIST.push_back((unsigned int)obj); obj->listRef = &g_OBJLIST; CLEARFLAG(obj->status, OBJ_DEACTIVATE); } } else { //remove object from previous list obj->listRef->remove((unsigned int)obj); //add object to deactivated object list g_OBJDEACTIVATELIST.push_back((unsigned int)obj); obj->listRef = &g_OBJDEACTIVATELIST; SETFLAG(obj->status, OBJ_DEACTIVATE); } }
///////////////////////////////////// // Name: // Purpose: // Output: // Return: ///////////////////////////////////// PROTECTED void GFXPageRemove(gfxID *pID) { gfxPage *page; //page where obj resides int pIndex = pID->ID / NUMPP; // Page index // Ascertain that ID is within the messager's pages ASSERT_MSG(pIndex < g_gfxList.nPages,"Invalid ID","GFXPageRemove"); page = g_gfxList.Pages [pIndex]; // Obtain client page pID->ID %= NUMPP; // Convert client ID to index CLEARFLAG(page->status,GFX_SPOT_ONE << pID->ID); // Mark block as available //if(page->status == GFX_PAGE_EMPTY && pIndex + 1 == g_gfxList.nPages) // Check whether last page is empty // _GFXPageRemoveLast(); --g_gfxList.nStuff; // Decrement obj count }
/* init phase : * 1) read configuration file * 2) read command line arguments * 3) daemonize * 4) check and write pid file * 5) set startup time stamp * 6) compute presentation URL * 7) set signal handlers */ static int init(int argc, char **argv) { int i; int pid; int debug_flag = 0; int verbose_flag = 0; int options_flag = 0; struct sigaction sa; const char * presurl = NULL; const char * optionsfile = "/etc/minidlna.conf"; char mac_str[13]; char *string, *word; char *path; char buf[PATH_MAX]; char log_str[75] = "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn"; char *log_level = NULL; struct media_dir_s *media_dir; int ifaces = 0; media_types types; uid_t uid = 0; /* first check if "-f" option is used */ for (i=2; i<argc; i++) { if (strcmp(argv[i-1], "-f") == 0) { optionsfile = argv[i]; options_flag = 1; break; } } /* set up uuid based on mac address */ if (getsyshwaddr(mac_str, sizeof(mac_str)) < 0) { DPRINTF(E_OFF, L_GENERAL, "No MAC address found. Falling back to generic UUID.\n"); strcpy(mac_str, "554e4b4e4f57"); } strcpy(uuidvalue+5, "4d696e69-444c-164e-9d41-"); strncat(uuidvalue, mac_str, 12); getfriendlyname(friendly_name, FRIENDLYNAME_MAX_LEN); runtime_vars.port = 8200; runtime_vars.notify_interval = 895; /* seconds between SSDP announces */ runtime_vars.max_connections = 50; runtime_vars.root_container = NULL; runtime_vars.ifaces[0] = NULL; /* read options file first since * command line arguments have final say */ if (readoptionsfile(optionsfile) < 0) { /* only error if file exists or using -f */ if(access(optionsfile, F_OK) == 0 || options_flag) DPRINTF(E_FATAL, L_GENERAL, "Error reading configuration file %s\n", optionsfile); } for (i=0; i<num_options; i++) { switch (ary_options[i].id) { case UPNPIFNAME: for (string = ary_options[i].value; (word = strtok(string, ",")); string = NULL) { if (ifaces >= MAX_LAN_ADDR) { DPRINTF(E_ERROR, L_GENERAL, "Too many interfaces (max: %d), ignoring %s\n", MAX_LAN_ADDR, word); break; } runtime_vars.ifaces[ifaces++] = word; } break; case UPNPPORT: runtime_vars.port = atoi(ary_options[i].value); break; case UPNPPRESENTATIONURL: presurl = ary_options[i].value; break; case UPNPNOTIFY_INTERVAL: runtime_vars.notify_interval = atoi(ary_options[i].value); break; case UPNPSERIAL: strncpyt(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN); break; case UPNPMODEL_NAME: strncpyt(modelname, ary_options[i].value, MODELNAME_MAX_LEN); break; case UPNPMODEL_NUMBER: strncpyt(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN); break; case UPNPFRIENDLYNAME: strncpyt(friendly_name, ary_options[i].value, FRIENDLYNAME_MAX_LEN); break; case UPNPMEDIADIR: types = ALL_MEDIA; path = ary_options[i].value; word = strchr(path, ','); if (word && (access(path, F_OK) != 0)) { types = 0; while (*path) { if (*path == ',') { path++; break; } else if (*path == 'A' || *path == 'a') types |= TYPE_AUDIO; else if (*path == 'V' || *path == 'v') types |= TYPE_VIDEO; else if (*path == 'P' || *path == 'p') types |= TYPE_IMAGES; else DPRINTF(E_FATAL, L_GENERAL, "Media directory entry not understood [%s]\n", ary_options[i].value); path++; } } path = realpath(path, buf); if (!path || access(path, F_OK) != 0) { DPRINTF(E_ERROR, L_GENERAL, "Media directory \"%s\" not accessible [%s]\n", ary_options[i].value, strerror(errno)); break; } media_dir = calloc(1, sizeof(struct media_dir_s)); media_dir->path = strdup(path); media_dir->types = types; if (media_dirs) { struct media_dir_s *all_dirs = media_dirs; while( all_dirs->next ) all_dirs = all_dirs->next; all_dirs->next = media_dir; } else media_dirs = media_dir; break; case UPNPALBUMART_NAMES: for (string = ary_options[i].value; (word = strtok(string, "/")); string = NULL) { struct album_art_name_s * this_name = calloc(1, sizeof(struct album_art_name_s)); int len = strlen(word); if (word[len-1] == '*') { word[len-1] = '\0'; this_name->wildcard = 1; } this_name->name = strdup(word); if (album_art_names) { struct album_art_name_s * all_names = album_art_names; while( all_names->next ) all_names = all_names->next; all_names->next = this_name; } else album_art_names = this_name; } break; case UPNPDBDIR: path = realpath(ary_options[i].value, buf); if (!path) path = (ary_options[i].value); make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); if (access(path, F_OK) != 0) DPRINTF(E_FATAL, L_GENERAL, "Database path not accessible! [%s]\n", path); strncpyt(db_path, path, PATH_MAX); break; case UPNPLOGDIR: path = realpath(ary_options[i].value, buf); if (!path) path = (ary_options[i].value); make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); if (access(path, F_OK) != 0) DPRINTF(E_FATAL, L_GENERAL, "Log path not accessible! [%s]\n", path); strncpyt(log_path, path, PATH_MAX); break; case UPNPLOGLEVEL: log_level = ary_options[i].value; break; case UPNPINOTIFY: if (!strtobool(ary_options[i].value)) CLEARFLAG(INOTIFY_MASK); break; case ENABLE_TIVO: if (strtobool(ary_options[i].value)) SETFLAG(TIVO_MASK); break; case ENABLE_DLNA_STRICT: if (strtobool(ary_options[i].value)) SETFLAG(DLNA_STRICT_MASK); break; case ROOT_CONTAINER: switch (ary_options[i].value[0]) { case '.': runtime_vars.root_container = NULL; break; case 'B': case 'b': runtime_vars.root_container = BROWSEDIR_ID; break; case 'M': case 'm': runtime_vars.root_container = MUSIC_ID; break; case 'V': case 'v': runtime_vars.root_container = VIDEO_ID; break; case 'P': case 'p': runtime_vars.root_container = IMAGE_ID; break; default: runtime_vars.root_container = ary_options[i].value; DPRINTF(E_WARN, L_GENERAL, "Using arbitrary root container [%s]\n", ary_options[i].value); break; } break; case UPNPMINISSDPDSOCKET: minissdpdsocketpath = ary_options[i].value; break; case UPNPUUID: strcpy(uuidvalue+5, ary_options[i].value); break; case USER_ACCOUNT: uid = strtoul(ary_options[i].value, &string, 0); if (*string) { /* Symbolic username given, not UID. */ struct passwd *entry = getpwnam(ary_options[i].value); if (!entry) DPRINTF(E_FATAL, L_GENERAL, "Bad user '%s'.\n", argv[i]); uid = entry->pw_uid; } break; case FORCE_SORT_CRITERIA: force_sort_criteria = ary_options[i].value; break; case MAX_CONNECTIONS: runtime_vars.max_connections = atoi(ary_options[i].value); break; case MERGE_MEDIA_DIRS: if (strtobool(ary_options[i].value)) SETFLAG(MERGE_MEDIA_DIRS_MASK); break; default: DPRINTF(E_ERROR, L_GENERAL, "Unknown option in file %s\n", optionsfile); } } if (log_path[0] == '\0') { if (db_path[0] == '\0') strncpyt(log_path, DEFAULT_LOG_PATH, PATH_MAX); else strncpyt(log_path, db_path, PATH_MAX); } if (db_path[0] == '\0') strncpyt(db_path, DEFAULT_DB_PATH, PATH_MAX); /* command line arguments processing */ for (i=1; i<argc; i++) { if (argv[i][0] != '-') { DPRINTF(E_FATAL, L_GENERAL, "Unknown option: %s\n", argv[i]); } else if (strcmp(argv[i], "--help") == 0) { runtime_vars.port = -1; break; } else switch(argv[i][1]) { case 't': if (i+1 < argc) runtime_vars.notify_interval = atoi(argv[++i]); else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 's': if (i+1 < argc) strncpyt(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN); else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'm': if (i+1 < argc) strncpyt(modelnumber, argv[++i], MODELNUMBER_MAX_LEN); else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'p': if (i+1 < argc) runtime_vars.port = atoi(argv[++i]); else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'P': if (i+1 < argc) { if (argv[++i][0] != '/') DPRINTF(E_FATAL, L_GENERAL, "Option -%c requires an absolute filename.\n", argv[i-1][1]); else pidfilename = argv[i]; } else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'd': debug_flag = 1; case 'v': verbose_flag = 1; break; case 'L': SETFLAG(NO_PLAYLIST_MASK); break; case 'w': if (i+1 < argc) presurl = argv[++i]; else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'i': if (i+1 < argc) { i++; if (ifaces >= MAX_LAN_ADDR) { DPRINTF(E_ERROR, L_GENERAL, "Too many interfaces (max: %d), ignoring %s\n", MAX_LAN_ADDR, argv[i]); break; } runtime_vars.ifaces[ifaces++] = argv[i]; } else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'f': i++; /* discarding, the config file is already read */ break; case 'h': runtime_vars.port = -1; // triggers help display break; case 'R': snprintf(buf, sizeof(buf), "rm -rf %s/files.db %s/art_cache", db_path, db_path); if (system(buf) != 0) DPRINTF(E_FATAL, L_GENERAL, "Failed to clean old file cache. EXITING\n"); break; case 'u': if (i+1 != argc) { i++; uid = strtoul(argv[i], &string, 0); if (*string) { /* Symbolic username given, not UID. */ struct passwd *entry = getpwnam(argv[i]); if (!entry) DPRINTF(E_FATAL, L_GENERAL, "Bad user '%s'.\n", argv[i]); uid = entry->pw_uid; } } else DPRINTF(E_FATAL, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; break; #ifdef __linux__ case 'S': SETFLAG(SYSTEMD_MASK); break; #endif case 'V': printf("Version " MINIDLNA_VERSION "\n"); exit(0); break; default: DPRINTF(E_ERROR, L_GENERAL, "Unknown option: %s\n", argv[i]); runtime_vars.port = -1; // triggers help display } } if (runtime_vars.port <= 0) { printf("Usage:\n\t" "%s [-d] [-v] [-f config_file] [-p port]\n" "\t\t[-i network_interface] [-u uid_to_run_as]\n" "\t\t[-t notify_interval] [-P pid_filename]\n" "\t\t[-s serial] [-m model_number]\n" #ifdef __linux__ "\t\t[-w url] [-R] [-L] [-S] [-V] [-h]\n" #else "\t\t[-w url] [-R] [-L] [-V] [-h]\n" #endif "\nNotes:\n\tNotify interval is in seconds. Default is 895 seconds.\n" "\tDefault pid file is %s.\n" "\tWith -d minidlna will run in debug mode (not daemonize).\n" "\t-w sets the presentation url. Default is http address on port 80\n" "\t-v enables verbose output\n" "\t-h displays this text\n" "\t-R forces a full rescan\n" "\t-L do not create playlists\n" #ifdef __linux__ "\t-S changes behaviour for systemd\n" #endif "\t-V print the version number\n", argv[0], pidfilename); return 1; } if (verbose_flag) { strcpy(log_str+65, "debug"); log_level = log_str; } else if (!log_level) log_level = log_str; /* Set the default log file path to NULL (stdout) */ path = NULL; if (debug_flag) { pid = getpid(); strcpy(log_str+65, "maxdebug"); log_level = log_str; } else if (GETFLAG(SYSTEMD_MASK)) { pid = getpid(); } else { pid = process_daemonize(); #ifdef READYNAS unlink("/ramfs/.upnp-av_scan"); path = "/var/log/upnp-av.log"; #else if (access(db_path, F_OK) != 0) make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); snprintf(buf, sizeof(buf), "%s/minidlna.log", log_path); path = buf; #endif } log_init(path, log_level); if (process_check_if_running(pidfilename) < 0) { DPRINTF(E_ERROR, L_GENERAL, SERVER_NAME " is already running. EXITING.\n"); return 1; } set_startup_time(); /* presentation url */ if (presurl) strncpyt(presentationurl, presurl, PRESENTATIONURL_MAX_LEN); else strcpy(presentationurl, "/"); /* set signal handlers */ memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = sigterm; if (sigaction(SIGTERM, &sa, NULL)) DPRINTF(E_FATAL, L_GENERAL, "Failed to set %s handler. EXITING.\n", "SIGTERM"); if (sigaction(SIGINT, &sa, NULL)) DPRINTF(E_FATAL, L_GENERAL, "Failed to set %s handler. EXITING.\n", "SIGINT"); if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) DPRINTF(E_FATAL, L_GENERAL, "Failed to set %s handler. EXITING.\n", "SIGPIPE"); if (signal(SIGHUP, &sighup) == SIG_ERR) DPRINTF(E_FATAL, L_GENERAL, "Failed to set %s handler. EXITING.\n", "SIGHUP"); signal(SIGUSR1, &sigusr1); sa.sa_handler = process_handle_child_termination; if (sigaction(SIGCHLD, &sa, NULL)) DPRINTF(E_FATAL, L_GENERAL, "Failed to set %s handler. EXITING.\n", "SIGCHLD"); if (writepidfile(pidfilename, pid, uid) != 0) pidfilename = NULL; if (uid > 0) { struct stat st; if (stat(db_path, &st) == 0 && st.st_uid != uid && chown(db_path, uid, -1) != 0) DPRINTF(E_ERROR, L_GENERAL, "Unable to set db_path [%s] ownership to %d: %s\n", db_path, uid, strerror(errno)); } if (uid > 0 && setuid(uid) == -1) DPRINTF(E_FATAL, L_GENERAL, "Failed to switch to uid '%d'. [%s] EXITING.\n", uid, strerror(errno)); children = calloc(runtime_vars.max_connections, sizeof(struct child)); if (!children) { DPRINTF(E_ERROR, L_GENERAL, "Allocation failed\n"); return 1; } // remove working flag remove_scantag(); return 0; }
/* init phase : * 1) read configuration file * 2) read command line arguments * 3) daemonize * 4) check and write pid file * 5) set startup time stamp * 6) compute presentation URL * 7) set signal handlers */ static int init(int argc, char * * argv) { int i; int pid; int debug_flag = 0; int options_flag = 0; struct sigaction sa; /*const char * logfilename = 0;*/ const char * presurl = 0; #if 1 const char * optionsfile = "/system/etc/minidlna.conf"; #else const char * optionsfile = "/etc/minidlna.conf"; #endif char mac_str[13]; char * string, * word; enum media_types type; char * path; char real_path[PATH_MAX]; char ext_ip_addr[INET_ADDRSTRLEN] = {'\0'}; /* first check if "-f" option is used */ for(i=2; i<argc; i++) { if(0 == strcmp(argv[i-1], "-f")) { optionsfile = argv[i]; options_flag = 1; break; } } /* set up uuid based on mac address */ if( getsyshwaddr(mac_str, sizeof(mac_str)) < 0 ) { DPRINTF(E_OFF, L_GENERAL, "No MAC address found. Falling back to generic UUID.\n"); strcpy(mac_str, "554e4b4e4f57"); } strcpy(uuidvalue+5, "4d696e69-444c-164e-9d41-"); strncat(uuidvalue, mac_str, 12); getfriendlyname(friendly_name, FRIENDLYNAME_MAX_LEN); runtime_vars.port = -1; runtime_vars.notify_interval = 895; /* seconds between SSDP announces */ /* read options file first since * command line arguments have final say */ if(readoptionsfile(optionsfile) < 0) { /* only error if file exists or using -f */ if(access(optionsfile, F_OK) == 0 || options_flag) fprintf(stderr, "Error reading configuration file %s\n", optionsfile); } else { for(i=0; i<num_options; i++) { switch(ary_options[i].id) { case UPNPIFNAME: if(getifaddr(ary_options[i].value, ext_ip_addr, INET_ADDRSTRLEN) >= 0) { if( *ext_ip_addr && parselanaddr(&lan_addr[n_lan_addr], ext_ip_addr) == 0 ) n_lan_addr++; } else fprintf(stderr, "Interface %s not found, ignoring.\n", ary_options[i].value); break; case UPNPLISTENING_IP: if(n_lan_addr < MAX_LAN_ADDR) { if(parselanaddr(&lan_addr[n_lan_addr], ary_options[i].value) == 0) n_lan_addr++; } else { fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n", MAX_LAN_ADDR, ary_options[i].value); } break; case UPNPPORT: runtime_vars.port = atoi(ary_options[i].value); break; case UPNPPRESENTATIONURL: presurl = ary_options[i].value; break; case UPNPNOTIFY_INTERVAL: runtime_vars.notify_interval = atoi(ary_options[i].value); break; case UPNPSERIAL: strncpy(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN); serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0'; break; case UPNPMODEL_NUMBER: strncpy(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN); modelnumber[MODELNUMBER_MAX_LEN-1] = '\0'; break; case UPNPFRIENDLYNAME: strncpy(friendly_name, ary_options[i].value, FRIENDLYNAME_MAX_LEN); friendly_name[FRIENDLYNAME_MAX_LEN-1] = '\0'; break; case UPNPMEDIADIR: type = ALL_MEDIA; char * myval = NULL; switch( ary_options[i].value[0] ) { case 'A': case 'a': if( ary_options[i].value[0] == 'A' || ary_options[i].value[0] == 'a' ) type = AUDIO_ONLY; case 'V': case 'v': if( ary_options[i].value[0] == 'V' || ary_options[i].value[0] == 'v' ) type = VIDEO_ONLY; case 'P': case 'p': if( ary_options[i].value[0] == 'P' || ary_options[i].value[0] == 'p' ) type = IMAGES_ONLY; myval = index(ary_options[i].value, '/'); case '/': path = realpath(myval ? myval:ary_options[i].value, real_path); if( !path ) path = (myval ? myval:ary_options[i].value); if( access(path, F_OK) != 0 ) { fprintf(stderr, "Media directory not accessible! [%s]\n", path); break; } struct media_dir_s * this_dir = calloc(1, sizeof(struct media_dir_s)); this_dir->path = strdup(path); this_dir->type = type; if( !media_dirs ) { media_dirs = this_dir; } else { struct media_dir_s * all_dirs = media_dirs; while( all_dirs->next ) all_dirs = all_dirs->next; all_dirs->next = this_dir; } break; default: fprintf(stderr, "Media directory entry not understood! [%s]\n", ary_options[i].value); break; } break; case UPNPALBUMART_NAMES: for( string = ary_options[i].value; (word = strtok(string, "/")); string = NULL ) { struct album_art_name_s * this_name = calloc(1, sizeof(struct album_art_name_s)); this_name->name = strdup(word); if( !album_art_names ) { album_art_names = this_name; } else { struct album_art_name_s * all_names = album_art_names; while( all_names->next ) all_names = all_names->next; all_names->next = this_name; } } break; case UPNPINOTIFY: if( (strcmp(ary_options[i].value, "yes") != 0) && !atoi(ary_options[i].value) ) CLEARFLAG(INOTIFY_MASK); break; case ENABLE_TIVO: if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) ) SETFLAG(TIVO_MASK); break; case ENABLE_DLNA_STRICT: if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) ) SETFLAG(DLNA_STRICT_MASK); break; default: fprintf(stderr, "Unknown option in file %s\n", optionsfile); } } } /* command line arguments processing */ for(i=1; i<argc; i++) { if(argv[i][0]!='-') { fprintf(stderr, "Unknown option: %s\n", argv[i]); } else if(strcmp(argv[i], "--help")==0) { runtime_vars.port = 0; break; } else switch(argv[i][1]) { case 't': if(i+1 < argc) runtime_vars.notify_interval = atoi(argv[++i]); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 's': if(i+1 < argc) strncpy(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0'; break; case 'm': if(i+1 < argc) strncpy(modelnumber, argv[++i], MODELNUMBER_MAX_LEN); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); modelnumber[MODELNUMBER_MAX_LEN-1] = '\0'; break; /*case 'l': logfilename = argv[++i]; break;*/ case 'p': if(i+1 < argc) runtime_vars.port = atoi(argv[++i]); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'P': if(i+1 < argc) pidfilename = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'd': debug_flag = 1; break; case 'w': if(i+1 < argc) presurl = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'a': if(i+1 < argc) { int address_already_there = 0; int j; i++; for(j=0; j<n_lan_addr; j++) { struct lan_addr_s tmpaddr; parselanaddr(&tmpaddr, argv[i]); if(0 == strcmp(lan_addr[j].str, tmpaddr.str)) address_already_there = 1; } if(address_already_there) break; if(n_lan_addr < MAX_LAN_ADDR) { if(parselanaddr(&lan_addr[n_lan_addr], argv[i]) == 0) n_lan_addr++; } else { fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n", MAX_LAN_ADDR, argv[i]); } } else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'i': if(i+1 < argc) { int address_already_there = 0; int j; i++; if( getifaddr(argv[i], ext_ip_addr, INET_ADDRSTRLEN) < 0 ) { fprintf(stderr, "Network interface '%s' not found.\n", argv[i]); exit(-1); } for(j=0; j<n_lan_addr; j++) { struct lan_addr_s tmpaddr; parselanaddr(&tmpaddr, ext_ip_addr); if(0 == strcmp(lan_addr[j].str, tmpaddr.str)) address_already_there = 1; } if(address_already_there) break; if(n_lan_addr < MAX_LAN_ADDR) { if(parselanaddr(&lan_addr[n_lan_addr], ext_ip_addr) == 0) n_lan_addr++; } else { fprintf(stderr, "Too many listening ips (max: %d), ignoring %s\n", MAX_LAN_ADDR, argv[i]); } } else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'f': i++; /* discarding, the config file is already read */ break; case 'h': runtime_vars.port = 0; // triggers help display break; case 'R': system("rm -rf " DB_PATH); // triggers a full rescan break; case 'V': printf("Version " MINIDLNA_VERSION "\n"); exit(0); break; default: fprintf(stderr, "Unknown option: %s\n", argv[i]); } } /* If no IP was specified, try to detect one */ if( n_lan_addr < 1 ) { if( (getsysaddr(ext_ip_addr, INET_ADDRSTRLEN) < 0) && (getifaddr("eth0", ext_ip_addr, INET_ADDRSTRLEN) < 0) && (getifaddr("eth1", ext_ip_addr, INET_ADDRSTRLEN) < 0) ) { DPRINTF(E_OFF, L_GENERAL, "No IP address automatically detected!\n"); } if( *ext_ip_addr && parselanaddr(&lan_addr[n_lan_addr], ext_ip_addr) == 0 ) { n_lan_addr++; } } if( (n_lan_addr==0) || (runtime_vars.port<=0) ) { fprintf(stderr, "Usage:\n\t" "%s [-d] [-f config_file]\n" "\t\t[-a listening_ip] [-p port]\n" /*"[-l logfile] " not functionnal */ "\t\t[-s serial] [-m model_number] \n" "\t\t[-t notify_interval] [-P pid_filename]\n" "\t\t[-w url] [-R] [-V] [-h]\n" "\nNotes:\n\tNotify interval is in seconds. Default is 895 seconds.\n" "\tDefault pid file is %s.\n" "\tWith -d minidlna will run in debug mode (not daemonize).\n" "\t-w sets the presentation url. Default is http address on port 80\n" "\t-h displays this text\n" "\t-R forces a full rescan\n" "\t-V print the version number\n", argv[0], pidfilename); return 1; } if(debug_flag) { pid = getpid(); log_init(NULL, "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=debug"); } else { #ifdef USE_DAEMON if(daemon(0, 0)<0) { perror("daemon()"); } pid = getpid(); #else pid = daemonize(); #endif #ifdef READYNAS log_init("/var/log/upnp-av.log", "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn"); #else log_init(DB_PATH "/minidlna.log", "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn"); #endif } if(checkforrunning(pidfilename) < 0) { DPRINTF(E_ERROR, L_GENERAL, "MiniDLNA is already running. EXITING.\n"); return 1; } set_startup_time(); /* presentation url */ if(presurl) { strncpy(presentationurl, presurl, PRESENTATIONURL_MAX_LEN); presentationurl[PRESENTATIONURL_MAX_LEN-1] = '\0'; } else { #ifdef READYNAS snprintf(presentationurl, PRESENTATIONURL_MAX_LEN, "https://%s/admin/", lan_addr[0].str); #else snprintf(presentationurl, PRESENTATIONURL_MAX_LEN, "http://%s:%d/", lan_addr[0].str, runtime_vars.port); #endif } /* set signal handler */ //signal(SIGCLD, SIG_IGN); signal(SIGCHLD, SIG_IGN); memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = sigterm; if (sigaction(SIGTERM, &sa, NULL)) { DPRINTF(E_FATAL, L_GENERAL, "Failed to set SIGTERM handler. EXITING.\n"); } if (sigaction(SIGINT, &sa, NULL)) { DPRINTF(E_FATAL, L_GENERAL, "Failed to set SIGINT handler. EXITING.\n"); } if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) { DPRINTF(E_FATAL, L_GENERAL, "Failed to ignore SIGPIPE signals. EXITING.\n"); } writepidfile(pidfilename, pid); return 0; }
/* init phase : * 1) read configuration file * 2) read command line arguments * 3) daemonize * 4) open syslog * 5) check and write pid file * 6) set startup time stamp * 7) compute presentation URL * 8) set signal handlers */ static int init(int argc, char * * argv, struct runtime_vars * v) { int i; int pid; int debug_flag = 0; int openlog_option; struct sigaction sa; /*const char * logfilename = 0;*/ const char * presurl = 0; #ifndef DISABLE_CONFIG_FILE int options_flag = 0; const char * optionsfile = DEFAULT_CONFIG; #endif /* DISABLE_CONFIG_FILE */ struct lan_addr_s * lan_addr; struct lan_addr_s * lan_addr2; /* only print usage if -h is used */ for(i=1; i<argc; i++) { if(0 == strcmp(argv[i], "-h")) goto print_usage; } #ifndef DISABLE_CONFIG_FILE /* first check if "-f" option is used */ for(i=2; i<argc; i++) { if(0 == strcmp(argv[i-1], "-f")) { optionsfile = argv[i]; options_flag = 1; break; } } #endif /* DISABLE_CONFIG_FILE */ /* set initial values */ SETFLAG(ENABLEUPNPMASK); /* UPnP is enabled by default */ LIST_INIT(&lan_addrs); v->port = -1; v->notify_interval = 30; /* seconds between SSDP announces */ v->clean_ruleset_threshold = 20; v->clean_ruleset_interval = 0; /* interval between ruleset check. 0=disabled */ #ifndef DISABLE_CONFIG_FILE /* read options file first since * command line arguments have final say */ if(readoptionsfile(optionsfile) < 0) { /* only error if file exists or using -f */ if(access(optionsfile, F_OK) == 0 || options_flag) fprintf(stderr, "Error reading configuration file %s\n", optionsfile); } else { for(i=0; i<(int)num_options; i++) { switch(ary_options[i].id) { case UPNPEXT_IFNAME: ext_if_name = ary_options[i].value; break; case UPNPEXT_IP: use_ext_ip_addr = ary_options[i].value; break; case UPNPLISTENING_IP: lan_addr = (struct lan_addr_s *) malloc(sizeof(struct lan_addr_s)); if (lan_addr == NULL) { fprintf(stderr, "malloc(sizeof(struct lan_addr_s)): %m"); break; } if(parselanaddr(lan_addr, ary_options[i].value) != 0) { fprintf(stderr, "can't parse \"%s\" as valid lan address\n", ary_options[i].value); free(lan_addr); break; } LIST_INSERT_HEAD(&lan_addrs, lan_addr, list); break; case UPNPPORT: v->port = atoi(ary_options[i].value); break; case UPNPBITRATE_UP: upstream_bitrate = strtoul(ary_options[i].value, 0, 0); break; case UPNPBITRATE_DOWN: downstream_bitrate = strtoul(ary_options[i].value, 0, 0); break; case UPNPPRESENTATIONURL: presurl = ary_options[i].value; break; case UPNPFRIENDLY_NAME: strncpy(friendly_name, ary_options[i].value, FRIENDLY_NAME_MAX_LEN); friendly_name[FRIENDLY_NAME_MAX_LEN-1] = '\0'; break; #ifdef USE_NETFILTER case UPNPFORWARDCHAIN: miniupnpd_forward_chain = ary_options[i].value; break; case UPNPNATCHAIN: miniupnpd_nat_chain = ary_options[i].value; break; #endif case UPNPNOTIFY_INTERVAL: v->notify_interval = atoi(ary_options[i].value); break; case UPNPSYSTEM_UPTIME: if(strcmp(ary_options[i].value, "yes") == 0) SETFLAG(SYSUPTIMEMASK); /*sysuptime = 1;*/ break; #if defined(USE_PF) || defined(USE_IPF) case UPNPPACKET_LOG: if(strcmp(ary_options[i].value, "yes") == 0) SETFLAG(LOGPACKETSMASK); /*logpackets = 1;*/ break; #endif case UPNPUUID: strncpy(uuidvalue+5, ary_options[i].value, strlen(uuidvalue+5) + 1); break; case UPNPSERIAL: strncpy(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN); serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0'; break; case UPNPMODEL_NUMBER: strncpy(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN); modelnumber[MODELNUMBER_MAX_LEN-1] = '\0'; break; case UPNPCLEANTHRESHOLD: v->clean_ruleset_threshold = atoi(ary_options[i].value); break; case UPNPCLEANINTERVAL: v->clean_ruleset_interval = atoi(ary_options[i].value); break; #ifdef USE_PF case UPNPANCHOR: anchor_name = ary_options[i].value; break; case UPNPQUEUE: queue = ary_options[i].value; break; case UPNPTAG: tag = ary_options[i].value; break; #endif #ifdef ENABLE_NATPMP case UPNPENABLENATPMP: if(strcmp(ary_options[i].value, "yes") == 0) SETFLAG(ENABLENATPMPMASK); /*enablenatpmp = 1;*/ else if(atoi(ary_options[i].value)) SETFLAG(ENABLENATPMPMASK); /*enablenatpmp = atoi(ary_options[i].value);*/ break; #endif #ifdef PF_ENABLE_FILTER_RULES case UPNPQUICKRULES: if(strcmp(ary_options[i].value, "no") == 0) SETFLAG(PFNOQUICKRULESMASK); break; #endif case UPNPENABLE: if(strcmp(ary_options[i].value, "yes") != 0) CLEARFLAG(ENABLEUPNPMASK); break; case UPNPSECUREMODE: if(strcmp(ary_options[i].value, "yes") == 0) SETFLAG(SECUREMODEMASK); break; #ifdef ENABLE_LEASEFILE case UPNPLEASEFILE: lease_file = ary_options[i].value; break; #endif case UPNPMINISSDPDSOCKET: minissdpdsocketpath = ary_options[i].value; break; default: fprintf(stderr, "Unknown option in file %s\n", optionsfile); } } } #endif /* DISABLE_CONFIG_FILE */ /* command line arguments processing */ for(i=1; i<argc; i++) { if(argv[i][0]!='-') { fprintf(stderr, "Unknown option: %s\n", argv[i]); } else switch(argv[i][1]) { case 'o': if(i+1 < argc) use_ext_ip_addr = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 't': if(i+1 < argc) v->notify_interval = atoi(argv[++i]); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'r': if(i+1 < argc) v->clean_ruleset_interval = atoi(argv[++i]); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'u': if(i+1 < argc) strncpy(uuidvalue+5, argv[++i], strlen(uuidvalue+5) + 1); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'z': if(i+1 < argc) strncpy(friendly_name, argv[++i], FRIENDLY_NAME_MAX_LEN); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); friendly_name[FRIENDLY_NAME_MAX_LEN-1] = '\0'; break; case 's': if(i+1 < argc) strncpy(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); serialnumber[SERIALNUMBER_MAX_LEN-1] = '\0'; break; case 'm': if(i+1 < argc) strncpy(modelnumber, argv[++i], MODELNUMBER_MAX_LEN); else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); modelnumber[MODELNUMBER_MAX_LEN-1] = '\0'; break; #ifdef ENABLE_NATPMP case 'N': /*enablenatpmp = 1;*/ SETFLAG(ENABLENATPMPMASK); break; #endif case 'U': /*sysuptime = 1;*/ SETFLAG(SYSUPTIMEMASK); break; /*case 'l': logfilename = argv[++i]; break;*/ #if defined(USE_PF) || defined(USE_IPF) case 'L': /*logpackets = 1;*/ SETFLAG(LOGPACKETSMASK); break; #endif case 'S': SETFLAG(SECUREMODEMASK); break; case 'i': if(i+1 < argc) ext_if_name = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; #ifdef USE_PF case 'q': if(i+1 < argc) queue = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'T': if(i+1 < argc) tag = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; #endif case 'p': if(i+1 < argc) v->port = atoi(argv[++i]); else #ifdef ENABLE_NFQUEUE case 'Q': if(i+1<argc) { nfqueue = atoi(argv[++i]); } else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'n': if (i+1 < argc) { i++; if(n_nfqix < MAX_LAN_ADDR) { nfqix[n_nfqix++] = if_nametoindex(argv[i]); } else { fprintf(stderr,"Too many nfq interfaces. Ignoring %s\n", argv[i]); } } else { fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); } break; #endif fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'P': if(i+1 < argc) pidfilename = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'd': debug_flag = 1; break; case 'w': if(i+1 < argc) presurl = argv[++i]; else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'B': if(i+2<argc) { downstream_bitrate = strtoul(argv[++i], 0, 0); upstream_bitrate = strtoul(argv[++i], 0, 0); } else fprintf(stderr, "Option -%c takes two arguments.\n", argv[i][1]); break; case 'a': #ifndef MULTIPLE_EXTERNAL_IP if(i+1 < argc) { i++; lan_addr = (struct lan_addr_s *) malloc(sizeof(struct lan_addr_s)); if (lan_addr == NULL) { fprintf(stderr, "malloc(sizeof(struct lan_addr_s)): %m"); break; } if(parselanaddr(lan_addr, argv[i]) != 0) { fprintf(stderr, "can't parse \"%s\" as valid lan address\n", argv[i]); free(lan_addr); break; } /* check if we already have this address */ for(lan_addr2 = lan_addrs.lh_first; lan_addr2 != NULL; lan_addr2 = lan_addr2->list.le_next) { if (0 == strncmp(lan_addr2->str, lan_addr->str, 15)) break; } if (lan_addr2 == NULL) LIST_INSERT_HEAD(&lan_addrs, lan_addr, list); } else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); #else if(i+2 < argc) { char *val=calloc((strlen(argv[i+1]) + strlen(argv[i+2]) + 1), sizeof(char)); if (val == NULL) { fprintf(stderr, "memory allocation error for listen address storage\n"); break; } sprintf(val, "%s %s", argv[i+1], argv[i+2]); lan_addr = (struct lan_addr_s *) malloc(sizeof(struct lan_addr_s)); if (lan_addr == NULL) { fprintf(stderr, "malloc(sizeof(struct lan_addr_s)): %m"); free(val); break; } if(parselanaddr(lan_addr, val) != 0) { fprintf(stderr, "can't parse \"%s\" as valid lan address\n", val); free(lan_addr); free(val); break; } /* check if we already have this address */ for(lan_addr2 = lan_addrs.lh_first; lan_addr2 != NULL; lan_addr2 = lan_addr2->list.le_next) { if (0 == strncmp(lan_addr2->str, lan_addr->str, 15)) break; } if (lan_addr2 == NULL) LIST_INSERT_HEAD(&lan_addrs, lan_addr, list); free(val); i+=2; } else fprintf(stderr, "Option -%c takes two arguments.\n", argv[i][1]); #endif break; case 'A': if(i+1 < argc) { void * tmp; tmp = realloc(upnppermlist, sizeof(struct upnpperm) * (num_upnpperm+1)); if(tmp == NULL) { fprintf(stderr, "memory allocation error for permission\n"); } else { upnppermlist = tmp; if(read_permission_line(upnppermlist + num_upnpperm, argv[++i]) >= 0) { num_upnpperm++; } else { fprintf(stderr, "Permission rule parsing error :\n%s\n", argv[i]); } } } else fprintf(stderr, "Option -%c takes one argument.\n", argv[i][1]); break; case 'f': i++; /* discarding, the config file is already read */ break; default: fprintf(stderr, "Unknown option: %s\n", argv[i]); } } if(!ext_if_name || !lan_addrs.lh_first) { /* bad configuration */ goto print_usage; } if(debug_flag) { pid = getpid(); } else { #ifdef USE_DAEMON if(daemon(0, 0)<0) { perror("daemon()"); } pid = getpid(); #else pid = daemonize(); #endif } openlog_option = LOG_PID|LOG_CONS; if(debug_flag) { openlog_option |= LOG_PERROR; /* also log on stderr */ } openlog("miniupnpd", openlog_option, LOG_MINIUPNPD); if(!debug_flag) { /* speed things up and ignore LOG_INFO and LOG_DEBUG */ setlogmask(LOG_UPTO(LOG_NOTICE)); } if(checkforrunning(pidfilename) < 0) { syslog(LOG_ERR, "MiniUPnPd is already running. EXITING"); return 1; } set_startup_time(GETFLAG(SYSUPTIMEMASK)); /* presentation url */ if(presurl) { strncpy(presentationurl, presurl, PRESENTATIONURL_MAX_LEN); presentationurl[PRESENTATIONURL_MAX_LEN-1] = '\0'; } else { snprintf(presentationurl, PRESENTATIONURL_MAX_LEN, "http://%s/", lan_addrs.lh_first->str); /*"http://%s:%d/", lan_addrs.lh_first->str, 80);*/ } /* set signal handler */ memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = sigterm; if(sigaction(SIGTERM, &sa, NULL) < 0) { syslog(LOG_ERR, "Failed to set %s handler. EXITING", "SIGTERM"); return 1; } if(sigaction(SIGINT, &sa, NULL) < 0) { syslog(LOG_ERR, "Failed to set %s handler. EXITING", "SIGINT"); return 1; } sa.sa_handler = SIG_IGN; if(sigaction(SIGPIPE, &sa, NULL) < 0) { syslog(LOG_ERR, "Failed to ignore SIGPIPE signals"); } sa.sa_handler = sigusr1; if(sigaction(SIGUSR1, &sa, NULL) < 0) { syslog(LOG_NOTICE, "Failed to set %s handler", "SIGUSR1"); } if(init_redirect() < 0) { syslog(LOG_ERR, "Failed to init redirection engine. EXITING"); return 1; } #ifdef ENABLE_6FC_SERVICE #ifdef USE_NETFILTER init_iptpinhole(); #endif #endif if(writepidfile(pidfilename, pid) < 0) pidfilename = NULL; #ifdef ENABLE_LEASEFILE /*remove(lease_file);*/ syslog(LOG_INFO, "Reloading rules from lease file"); reload_from_lease_file(); #endif return 0; print_usage: fprintf(stderr, "Usage:\n\t" "%s " #ifndef DISABLE_CONFIG_FILE "[-f config_file] " #endif "[-i ext_ifname] [-o ext_ip]\n" #ifndef MULTIPLE_EXTERNAL_IP "\t\t[-a listening_ip]" #else "\t\t[-a listening_ip ext_ip]" #endif " [-p port] [-d]" #if defined(USE_PF) || defined(USE_IPF) " [-L]" #endif " [-U] [-S]" #ifdef ENABLE_NATPMP " [-N]" #endif "\n" /*"[-l logfile] " not functionnal */ "\t\t[-u uuid] [-s serial] [-m model_number] \n" "\t\t[-t notify_interval] [-P pid_filename] [-z fiendly_name]\n" "\t\t[-B down up] [-w url] [-r clean_ruleset_interval]\n" #ifdef USE_PF "\t\t[-q queue] [-T tag]\n" #endif #ifdef ENABLE_NFQUEUE "\t\t[-Q queue] [-n name]\n" #endif "\t\t[-A \"permission rule\"]\n" "\nNotes:\n\tThere can be one or several listening_ips.\n" "\tNotify interval is in seconds. Default is 30 seconds.\n" "\tDefault pid file is '%s'.\n" "\tDefault config file is '%s'.\n" "\tWith -d miniupnpd will run as a standard program.\n" #if defined(USE_PF) || defined(USE_IPF) "\t-L sets packet log in pf and ipf on.\n" #endif "\t-S sets \"secure\" mode : clients can only add mappings to their own ip\n" "\t-U causes miniupnpd to report system uptime instead " "of daemon uptime.\n" #ifdef ENABLE_NATPMP "\t-N enable NAT-PMP functionality.\n" #endif "\t-B sets bitrates reported by daemon in bits per second.\n" "\t-w sets the presentation url. Default is http address on port 80\n" #ifdef USE_PF "\t-q sets the ALTQ queue in pf.\n" "\t-T sets the tag name in pf.\n" #endif #ifdef ENABLE_NFQUEUE "\t-Q sets the queue number that is used by NFQUEUE.\n" "\t-n sets the name of the interface(s) that packets will arrive on.\n" #endif "\t-A use following syntax for permission rules :\n" "\t (allow|deny) (external port range) ip/mask (internal port range)\n" "\texamples :\n" "\t \"allow 1024-65535 192.168.1.0/24 1024-65535\"\n" "\t \"deny 0-65535 0.0.0.0/0 0-65535\"\n" "\t-h prints this help and quits.\n" "", argv[0], pidfilename, DEFAULT_CONFIG); return 1; }
/// // CTimedText::Init() // void CTimedText::Init( TIMED_TEXT_INIT_STRUCT const &initData ) { // initialize the internally needed info InitInternalInfo(); // // extract the data from the initialization struct // // Visual Characteristics ASSERT( initData.text ); m_text = initData.text; m_color = ( initData.color & 0x00FFFFFF ); // get the text's position, cause we may change this // as it scrolls m_text->GetPosition( &m_posX, &m_posY ); // use dropped shadow? if ( initData.useDroppedShadow ) { SETFLAG( m_flags, CTIMEDTEXTFLAG_USE_DROPPED_SHADOW ); } else { CLEARFLAG( m_flags, CTIMEDTEXTFLAG_USE_DROPPED_SHADOW ); } if ( initData.clipRect ) { m_clipRect = *initData.clipRect; SETFLAG( m_flags, CTIMEDTEXTFLAG_USE_CLIP_RECT ); } else { // clear the clip rect if we're not using it m_clipRect.x = m_clipRect.y = m_clipRect.height = m_clipRect.width = 0; CLEARFLAG( m_flags, CTIMEDTEXTFLAG_USE_CLIP_RECT ); } m_textLen = strlen( m_text->GetText() ); m_numberOfLinesBeforeScroll = initData.numberOfLinesBeforeScroll; // Timing Information m_initialDelay = initData.initialDelay; ASSERT( m_initialDelay >= 0 ); m_characterDelay = initData.characterDelay; ASSERT( m_characterDelay >= 0 ); m_lineDelay = initData.lineDelay; ASSERT( m_lineDelay >= 0 ); m_scrollTime = initData.scrollTime; ASSERT( m_scrollTime >= 0 ); m_completeDelay = initData.completeDelay; ASSERT( m_completeDelay >= 0 ); m_fadeTime = initData.fadeTime; ASSERT( m_fadeTime >= 0 ); // // Sound Information // // get character complete sound name (if any) if ( initData.textDisplaySound && initData.textDisplaySound[ 0 ] != '\0' ) { strncpy( m_textDisplaySoundName, initData.textDisplaySound, TIMED_TEXT_SOUND_NAME_LENGTH ); } // get scrolling sound name (if any) if ( initData.scrollSound && initData.scrollSound[ 0 ] != '\0' ) { strncpy( m_scrollSoundName, initData.scrollSound, TIMED_TEXT_SOUND_NAME_LENGTH ); } #ifdef TIMEDTEXT_DEBUG { // make sure strings terminate int i; LTBOOL charEndFound = LTFALSE; LTBOOL scrollEndFound = LTFALSE; for ( i = 0, charEndFound = LTFALSE, scrollEndFound = LTFALSE; ( ( i < TIMED_TEXT_SOUND_NAME_LENGTH ) && ( ( !charEndFound ) || ( !scrollEndFound ) ) ); ++i ) { if ( ( !charEndFound ) && ( m_textDisplaySoundName[ i ] == '\0' ) ) { charEndFound = LTTRUE; } if ( ( !scrollEndFound ) && ( m_scrollSoundName[ i ] == '\0' ) ) { scrollEndFound = LTTRUE; } } // assert if no end if found ASSERT( charEndFound ); ASSERT( scrollEndFound ); } #endif // TIMEDTEXT_DEBUG // now we're in the text writing phase m_state = CTIMEDTEXT_INITIAL_DELAY; }
/// // CTimedText::Update() // void CTimedText::Update() { // bail if we are not set up (current state == none), // or if we are finished // or if we are not running if ( ( m_state == CTIMEDTEXT_NONE ) || ( m_state == CTIMEDTEXT_FINISHED ) || !TESTFLAG( m_flags, CTIMEDTEXTFLAG_RUNNING ) ) { return; } // update the elapsed time float currentUpdateTime; currentUpdateTime = g_pLTClient->GetTime(); m_timeElapsed += currentUpdateTime - m_prevUpdateTime; m_prevUpdateTime = currentUpdateTime; // check if we're just waiting to start if ( m_state == CTIMEDTEXT_INITIAL_DELAY ) { if ( m_timeElapsed < m_initialDelay ) { // still waiting, don't do anything return; } // no longer waiting, change to next state m_state = CTIMEDTEXT_TIMING; m_timeElapsed = m_timeElapsed - m_initialDelay; } // determine how much text we've done so far float processedTime; if ( m_state == CTIMEDTEXT_TIMING ) { // the processed time is important for timing text // because it determines what position we have reached // in the text (how much text is revealed) processedTime = ( m_charactersProcessed - m_nNewlinesProcessed ) * m_characterDelay; // Get the minimum of number of lines before scrolling // or the number of lines we've processed. This will be // the number of line delays to use. float numberOfNonScrolls; if ( m_linesProcessed > ( m_numberOfLinesBeforeScroll - 1 ) ) { numberOfNonScrolls = (float)( m_numberOfLinesBeforeScroll - 1 ); } else { numberOfNonScrolls = m_linesProcessed; } // add the proper number of line delays to the processed time processedTime += numberOfNonScrolls * m_lineDelay; // determine how many times we've scrolled so far float numberOfScrolls; numberOfScrolls = m_linesProcessed - (float)( m_numberOfLinesBeforeScroll - 1 ); if ( numberOfScrolls > 0 ) { processedTime += numberOfScrolls * m_scrollTime; } } else { // the other states are easier cause they are // just straight timers and we don't have to know // where we are in the text processedTime = m_timeElapsed; } // determine where we currently are in the text int textPos; textPos = m_charactersProcessed; ASSERT( textPos <= m_textLen ); // // bring our text up to date // // while processed time is less than true elapsed time while (processedTime <= m_timeElapsed ) { // *check state if ( m_state == CTIMEDTEXT_TIMING ) { if ( TESTFLAG( m_flags, CTIMEDTEXTFLAG_SCROLLING ) ) { // // the text is currently scrolling // float timeLeft; timeLeft = m_timeElapsed - m_scrollRefTime; uint8 textHeight; textHeight = m_text->GetCharScreenHeight(); if ( timeLeft < m_scrollTime ) { // move text to proper position m_posY = m_scrollRefPosY - ( textHeight * timeLeft / m_scrollTime ); #ifdef TIMED_TEXT_FADE_TOP_LINE // fade top line by appropiate amount for ( int ii = m_nLineFadeCharStart; ii < m_nLineFadeCharEnd; ++ii ) { g_pLTClient->GetDrawPrim()->SetALPHA( &m_text->GetPolys()[ ii ], ( uint8 ) ( ALPHA_OPAQUE - ALPHA_OPAQUE * timeLeft / m_scrollTime ) ); } #endif // TIMED_TEXT_FADE_TOP_LINE break; } else { // move text to the final position m_posY = m_scrollRefPosY - textHeight; // increment the current time processedTime = m_scrollRefTime + m_scrollTime; // update our line count ++m_linesProcessed; #ifdef TIMED_TEXT_FADE_TOP_LINE // fade line completely for ( int ii = m_nLineFadeCharStart; ii < m_nLineFadeCharEnd; ++ii ) { g_pLTClient->GetDrawPrim()->SetALPHA( &m_text->GetPolys()[ ii ], 0 ); } // the next line to fade starts where this one ended m_nLineFadeCharStart = m_nLineFadeCharEnd; #endif // TIMED_TEXT_FADE_TOP_LINE // reset our references m_scrollRefPosY = 0; m_scrollRefTime = 0; // remember the new line height m_fLineYPos = m_text->GetPolys()[ textPos ].verts[ 0 ].y; // done scrolling CLEARFLAG( m_flags, CTIMEDTEXTFLAG_SCROLLING ); } } // if we are done displaying the text else if ( textPos >= m_textLen ) { // reset time elapsed m_timeElapsed -= processedTime; processedTime = 0; if ( m_hTextDisplaySound != LTNULL ) { // kill the looping typing sound g_pLTClient->SoundMgr()->KillSound( m_hTextDisplaySound ); m_hTextDisplaySound = LTNULL; } // switch current state to complete m_state = CTIMEDTEXT_COMPLETE; ASSERT( m_timeElapsed >= 0 ); } // else if the text at the current position is a linefeed else if ( ( m_text->GetPolys()[ textPos ].verts[ 0 ].y - m_fLineYPos ) > 0.5f ) { // The only way we know we have a new line is if the polygon // has a different y value than the one before it. if ( m_linesProcessed >= ( m_numberOfLinesBeforeScroll - 1 ) ) { if ( !TESTFLAG( m_flags, CTIMEDTEXTFLAG_SCROLLING ) ) { // set up to scroll SETFLAG( m_flags, CTIMEDTEXTFLAG_SCROLLING ); m_scrollRefTime = processedTime; m_scrollRefPosY = m_posY; #ifdef TIMED_TEXT_FADE_TOP_LINE // to fade the top line of text, start from where // the line starts and scan for end of line for ( m_nLineFadeCharEnd = m_nLineFadeCharStart + 1; ( ( m_text->GetPolys()[ m_nLineFadeCharEnd ].verts[ 0 ].y - m_text->GetPolys()[ m_nLineFadeCharStart ].verts[ 0 ].y ) < 0.5f ); ++m_nLineFadeCharEnd ) { } #endif // TIMED_TEXT_FADE_TOP_LINE if ( m_hTextDisplaySound != LTNULL ) { // kill the looping typing sound g_pLTClient->SoundMgr()->KillSound( m_hTextDisplaySound ); m_hTextDisplaySound = LTNULL; } // play the scroll sound if ( m_scrollSoundName[ 0 ] != '\0' ) { g_pClientSoundMgr->PlayInterfaceSound( m_scrollSoundName ); } } } else { // increment the current time processedTime += m_lineDelay; // update our line count ++m_linesProcessed; m_fLineYPos = m_text->GetPolys()[ textPos ].verts[ 0 ].y; if ( m_hTextDisplaySound != LTNULL ) { // kill the looping typing sound g_pLTClient->SoundMgr()->KillSound( m_hTextDisplaySound ); m_hTextDisplaySound = LTNULL; } } } else if ( m_text->GetText()[ textPos ] == '\n' ) { // Ignore newlines completely. We want to keep track of them so // we know where we are in the text, but we won't do any // of the timing delay calculations with them. ++m_charactersProcessed; ++m_nNewlinesProcessed; ++textPos; } // else if the text at the current position is printable else { // play the sound if ( ( m_hTextDisplaySound == LTNULL ) && ( m_textDisplaySoundName[ 0 ] != '\0' ) ) { m_hTextDisplaySound = g_pClientSoundMgr->PlayInterfaceSound( m_textDisplaySoundName, ( PLAYSOUND_LOOP | PLAYSOUND_GETHANDLE | PLAYSOUND_CLIENT ) ); } // update our character count ++m_charactersProcessed; ++textPos; // increment the current time processedTime += m_characterDelay; } } // else if complete else if ( m_state == CTIMEDTEXT_COMPLETE ) { // *check to switch to next state // if current time greater than delay if ( processedTime >= m_completeDelay ) { // reset time elapsed m_timeElapsed -= processedTime; processedTime = 0; // switch current state to fade m_state = CTIMEDTEXT_FADING; } else { break; } } // else if fading else if ( m_state == CTIMEDTEXT_FADING ) { // *check to switch to next state // if current time is greater than fade time if ( processedTime >= m_fadeTime ) { // switch current state to finished m_state = CTIMEDTEXT_FINISHED; } else { //TEMP: turn the text to black break; } } // else if finished else { CLEARFLAG( m_flags, ( CTIMEDTEXTFLAG_RUNNING | CTIMEDTEXTFLAG_DISPLAY ) ); // DONE break; } } #ifdef TIMEDTEXT_DEBUG // put this here to make debugging easier // if a lot of time is spent in the debugger // in the update loop, we won't count that // time against the timed text m_prevUpdateTime = g_pLTClient->GetTime(); #endif // TIMEDTEXT_DEBUG }
/* init phase : * 1) read configuration file * 2) read command line arguments * 3) daemonize * 4) check and write pid file * 5) set startup time stamp * 6) compute presentation URL * 7) set signal handlers */ static int init(int argc, char * * argv) { int i; int pid; int debug_flag = 0; int verbose_flag = 0; int options_flag = 0; struct sigaction sa; const char * presurl = NULL; const char * optionsfile = "/etc/minidlna.conf"; char mac_str[13]; char * string, * word; enum media_types type; char * path; char buf[PATH_MAX]; char ip_addr[INET_ADDRSTRLEN + 3] = {'\0'}; char log_str[72] = "general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn"; char *log_level = NULL; /* first check if "-f" option is used */ for(i=2; i<argc; i++) { if(0 == strcmp(argv[i-1], "-f")) { optionsfile = argv[i]; options_flag = 1; break; } } /* set up uuid based on mac address */ if( getsyshwaddr(mac_str, sizeof(mac_str)) < 0 ) { DPRINTF(E_OFF, L_GENERAL, "No MAC address found. Falling back to generic UUID.\n"); strcpy(mac_str, "554e4b4e4f57"); } strcpy(uuidvalue+5, "4d696e69-444c-164e-9d41-"); strncat(uuidvalue, mac_str, 12); getfriendlyname(friendly_name, FRIENDLYNAME_MAX_LEN); runtime_vars.port = -1; runtime_vars.notify_interval = 895; /* seconds between SSDP announces */ runtime_vars.root_container = NULL; /* read options file first since * command line arguments have final say */ if(readoptionsfile(optionsfile) < 0) { /* only error if file exists or using -f */ if(access(optionsfile, F_OK) == 0 || options_flag) DPRINTF(E_ERROR, L_GENERAL, "Error reading configuration file %s\n", optionsfile); } else { for(i=0; i<num_options; i++) { switch(ary_options[i].id) { case UPNPIFNAME: for( string = ary_options[i].value; (word = strtok(string, ",")); string = NULL ) { if(n_lan_addr < MAX_LAN_ADDR) { if(getifaddr(word, ip_addr, sizeof(ip_addr)) >= 0) { if( *ip_addr && parselanaddr(&lan_addr[n_lan_addr], ip_addr) == 0 ) if(n_lan_addr < MAX_LAN_ADDR) n_lan_addr++; } } else { DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n", MAX_LAN_ADDR, word); } } break; case UPNPLISTENING_IP: if(n_lan_addr < MAX_LAN_ADDR) { if(parselanaddr(&lan_addr[n_lan_addr], ary_options[i].value) == 0) n_lan_addr++; } else { DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n", MAX_LAN_ADDR, ary_options[i].value); } break; case UPNPPORT: runtime_vars.port = atoi(ary_options[i].value); break; case UPNPPRESENTATIONURL: presurl = ary_options[i].value; break; case UPNPNOTIFY_INTERVAL: runtime_vars.notify_interval = atoi(ary_options[i].value); break; case UPNPSERIAL: strncpyt(serialnumber, ary_options[i].value, SERIALNUMBER_MAX_LEN); break; case UPNPMODEL_NAME: strncpyt(modelname, ary_options[i].value, MODELNAME_MAX_LEN); break; case UPNPMODEL_NUMBER: strncpyt(modelnumber, ary_options[i].value, MODELNUMBER_MAX_LEN); break; case UPNPFRIENDLYNAME: strncpyt(friendly_name, ary_options[i].value, FRIENDLYNAME_MAX_LEN); break; case UPNPMEDIADIR: type = ALL_MEDIA; char * myval = NULL; switch( ary_options[i].value[0] ) { case 'A': case 'a': if( ary_options[i].value[0] == 'A' || ary_options[i].value[0] == 'a' ) type = AUDIO_ONLY; case 'V': case 'v': if( ary_options[i].value[0] == 'V' || ary_options[i].value[0] == 'v' ) type = VIDEO_ONLY; case 'P': case 'p': if( ary_options[i].value[0] == 'P' || ary_options[i].value[0] == 'p' ) type = IMAGES_ONLY; myval = index(ary_options[i].value, '/'); case '/': path = realpath(myval ? myval:ary_options[i].value, buf); if( !path ) path = (myval ? myval:ary_options[i].value); if( access(path, F_OK) != 0 ) { DPRINTF(E_ERROR, L_GENERAL, "Media directory \"%s\" not accessible! [%s]\n", path, strerror(errno)); break; } struct media_dir_s * this_dir = calloc(1, sizeof(struct media_dir_s)); this_dir->path = strdup(path); this_dir->type = type; if( !media_dirs ) { media_dirs = this_dir; } else { struct media_dir_s * all_dirs = media_dirs; while( all_dirs->next ) all_dirs = all_dirs->next; all_dirs->next = this_dir; } break; default: DPRINTF(E_ERROR, L_GENERAL, "Media directory entry not understood! [%s]\n", ary_options[i].value); break; } break; case UPNPALBUMART_NAMES: for( string = ary_options[i].value; (word = strtok(string, "/")); string = NULL ) { struct album_art_name_s * this_name = calloc(1, sizeof(struct album_art_name_s)); int len = strlen(word); if( word[len-1] == '*' ) { word[len-1] = '\0'; this_name->wildcard = 1; } this_name->name = strdup(word); if( !album_art_names ) { album_art_names = this_name; } else { struct album_art_name_s * all_names = album_art_names; while( all_names->next ) all_names = all_names->next; all_names->next = this_name; } } break; case UPNPDBDIR: path = realpath(ary_options[i].value, buf); if( !path ) path = (ary_options[i].value); make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); if( access(path, F_OK) != 0 ) { DPRINTF(E_FATAL, L_GENERAL, "Database path not accessible! [%s]\n", path); break; } strncpyt(db_path, path, PATH_MAX); break; case UPNPLOGDIR: path = realpath(ary_options[i].value, buf); if( !path ) path = (ary_options[i].value); make_dir(path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); if( access(path, F_OK) != 0 ) { DPRINTF(E_FATAL, L_GENERAL, "Log path not accessible! [%s]\n", path); break; } strncpyt(log_path, path, PATH_MAX); break; case UPNPLOGLEVEL: log_level = ary_options[i].value; break; case UPNPINOTIFY: if( (strcmp(ary_options[i].value, "yes") != 0) && !atoi(ary_options[i].value) ) CLEARFLAG(INOTIFY_MASK); break; case ENABLE_TIVO: if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) ) SETFLAG(TIVO_MASK); break; case ENABLE_DLNA_STRICT: if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) ) SETFLAG(DLNA_STRICT_MASK); break; case ROOT_CONTAINER: switch( ary_options[i].value[0] ) { case '.': runtime_vars.root_container = NULL; break; case 'B': case 'b': runtime_vars.root_container = BROWSEDIR_ID; break; case 'M': case 'm': runtime_vars.root_container = MUSIC_ID; break; case 'V': case 'v': runtime_vars.root_container = VIDEO_ID; break; case 'P': case 'p': runtime_vars.root_container = IMAGE_ID; break; default: DPRINTF(E_ERROR, L_GENERAL, "Invalid root container! [%s]\n", ary_options[i].value); break; } break; case UPNPMINISSDPDSOCKET: minissdpdsocketpath = ary_options[i].value; break; default: DPRINTF(E_ERROR, L_GENERAL, "Unknown option in file %s\n", optionsfile); } } } if( log_path[0] == '\0' ) { if( db_path[0] == '\0' ) strncpyt(log_path, DEFAULT_LOG_PATH, PATH_MAX); else strncpyt(log_path, db_path, PATH_MAX); } if( db_path[0] == '\0' ) strncpyt(db_path, DEFAULT_DB_PATH, PATH_MAX); /* command line arguments processing */ for(i=1; i<argc; i++) { if(argv[i][0]!='-') { DPRINTF(E_ERROR, L_GENERAL, "Unknown option: %s\n", argv[i]); } else if(strcmp(argv[i], "--help")==0) { runtime_vars.port = 0; break; } else switch(argv[i][1]) { case 't': if(i+1 < argc) runtime_vars.notify_interval = atoi(argv[++i]); else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 's': if(i+1 < argc) strncpyt(serialnumber, argv[++i], SERIALNUMBER_MAX_LEN); else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'm': if(i+1 < argc) strncpyt(modelnumber, argv[++i], MODELNUMBER_MAX_LEN); else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'p': if(i+1 < argc) runtime_vars.port = atoi(argv[++i]); else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'P': if(i+1 < argc) pidfilename = argv[++i]; else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'd': debug_flag = 1; case 'v': verbose_flag = 1; break; case 'L': SETFLAG(NO_PLAYLIST_MASK); break; case 'w': if(i+1 < argc) presurl = argv[++i]; else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'a': if(i+1 < argc) { int address_already_there = 0; int j; i++; for(j=0; j<n_lan_addr; j++) { struct lan_addr_s tmpaddr; parselanaddr(&tmpaddr, argv[i]); if(0 == strcmp(lan_addr[j].str, tmpaddr.str)) address_already_there = 1; } if(address_already_there) break; if(n_lan_addr < MAX_LAN_ADDR) { if(parselanaddr(&lan_addr[n_lan_addr], argv[i]) == 0) n_lan_addr++; } else { DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n", MAX_LAN_ADDR, argv[i]); } } else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'i': if(i+1 < argc) { int address_already_there = 0; int j; i++; if( getifaddr(argv[i], ip_addr, sizeof(ip_addr)) < 0 ) { DPRINTF(E_FATAL, L_GENERAL, "Required network interface '%s' not found.\n", argv[i]); } for(j=0; j<n_lan_addr; j++) { struct lan_addr_s tmpaddr; parselanaddr(&tmpaddr, ip_addr); if(0 == strcmp(lan_addr[j].str, tmpaddr.str)) address_already_there = 1; } if(address_already_there) break; if(n_lan_addr < MAX_LAN_ADDR) { if(parselanaddr(&lan_addr[n_lan_addr], ip_addr) == 0) n_lan_addr++; } else { DPRINTF(E_ERROR, L_GENERAL, "Too many listening ips (max: %d), ignoring %s\n", MAX_LAN_ADDR, argv[i]); } } else DPRINTF(E_ERROR, L_GENERAL, "Option -%c takes one argument.\n", argv[i][1]); break; case 'f': i++; /* discarding, the config file is already read */ break; case 'h': runtime_vars.port = 0; // triggers help display break; case 'R': snprintf(buf, sizeof(buf), "rm -rf %s/files.db %s/art_cache", db_path, db_path); if( system(buf) != 0 ) DPRINTF(E_WARN, L_GENERAL, "Failed to clean old file cache.\n"); break; case 'V': printf("Version " MINIDLNA_VERSION "\n"); exit(0); break; default: DPRINTF(E_ERROR, L_GENERAL, "Unknown option: %s\n", argv[i]); } } /* If no IP was specified, try to detect one */ if( n_lan_addr < 1 ) { if( (getsysaddr(ip_addr, sizeof(ip_addr)) < 0) && (getifaddr("eth0", ip_addr, sizeof(ip_addr)) < 0) && (getifaddr("eth1", ip_addr, sizeof(ip_addr)) < 0) ) { DPRINTF(E_OFF, L_GENERAL, "No IP address automatically detected!\n"); } if( *ip_addr && parselanaddr(&lan_addr[n_lan_addr], ip_addr) == 0 ) { n_lan_addr++; } } if( (n_lan_addr==0) || (runtime_vars.port<=0) ) { DPRINTF(E_ERROR, L_GENERAL, "Usage:\n\t" "%s [-d] [-v] [-f config_file]\n" "\t\t[-a listening_ip] [-p port]\n" /*"[-l logfile] " not functionnal */ "\t\t[-s serial] [-m model_number] \n" "\t\t[-t notify_interval] [-P pid_filename]\n" "\t\t[-w url] [-R] [-V] [-h]\n" "\nNotes:\n\tNotify interval is in seconds. Default is 895 seconds.\n" "\tDefault pid file is %s.\n" "\tWith -d minidlna will run in debug mode (not daemonize).\n" "\t-w sets the presentation url. Default is http address on port 80\n" "\t-h displays this text\n" "\t-R forces a full rescan\n" "\t-L do note create playlists\n" "\t-V print the version number\n", argv[0], pidfilename); return 1; } if( verbose_flag ) { strcpy(log_str+65, "debug"); log_level = log_str; } else if( !log_level ) { log_level = log_str; } if(debug_flag) { pid = getpid(); log_init(NULL, log_level); } else { pid = daemonize(); #ifdef READYNAS log_init("/var/log/upnp-av.log", log_level); #else if( access(db_path, F_OK) != 0 ) make_dir(db_path, S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); sprintf(buf, "%s/minidlna.log", log_path); log_init(buf, log_level); #endif } if(checkforrunning(pidfilename) < 0) { DPRINTF(E_ERROR, L_GENERAL, "MiniDLNA is already running. EXITING.\n"); return 1; } set_startup_time(); /* presentation url */ if(presurl) strncpyt(presentationurl, presurl, PRESENTATIONURL_MAX_LEN); else strcpy(presentationurl, "/"); /* set signal handler */ memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = sigterm; if (sigaction(SIGTERM, &sa, NULL)) { DPRINTF(E_FATAL, L_GENERAL, "Failed to set SIGTERM handler. EXITING.\n"); } if (sigaction(SIGINT, &sa, NULL)) { DPRINTF(E_FATAL, L_GENERAL, "Failed to set SIGINT handler. EXITING.\n"); } if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) { DPRINTF(E_FATAL, L_GENERAL, "Failed to ignore SIGPIPE signals. EXITING.\n"); } writepidfile(pidfilename, pid); return 0; }
//update homie PUBLIC RETCODE HomieUpdate(hHOMIE homie, hGMAP map) { int lastIndX = homie->indX; int lastIndY = homie->indY; if((TESTFLAGS(homie->status, HOMIE_MOVING) || TESTFLAGS(homie->status, HOMIE_STUCK)) && !TESTFLAGS(homie->status, HOMIE_DEAD)) { float orient[eMaxPt]; OBJGetOrientation(homie->obj, orient); orient[eX] *= -1; int nextIndX = homie->indX + (orient[eX] <= 0 ? (orient[eX] < 0 ? -1 : 0) : 1); int nextIndY = homie->indY + (orient[eZ] <= 0 ? (orient[eZ] < 0 ? -1 : 0) : 1); hTILE nextTile; //check to see if next ind is out of bound if(nextIndX < 0 || nextIndX >= map->indSizeX || nextIndY < 0 || nextIndY >= map->indSizeY) return HomieStop(homie, map); //get next tile nextTile = &CELL(map->tiles, nextIndY, nextIndX, map->indSizeX); assert(nextTile); //check if occupied if(TESTFLAGS(nextTile->status, TILE_OCCUPIED)) return HomieStop(homie, map); //check if next tile is a wall or etc... switch(nextTile->type) { case eTILE_WALL: return HomieStop(homie, map); case eTILE_BOUNCE: //inverse direction orient[eZ] *= -1; OBJSetOrientation(homie->obj, orient[eX],orient[eY],orient[eZ]); homie->indX = nextIndX; homie->indY = nextIndY; return RETCODE_SUCCESS; case eTILE_DOOR: if(nextTile->homieType == homie->type) //destroy this door (change to floor) { float tileLoc[eMaxPt]; OBJGetLoc(nextTile->obj, tileLoc); float floorLoc[eMaxPt]; OBJDestroy(&nextTile->obj); //look for a floor tile hTILE theTile = MapGetTile(map, eTILE_FLOOR); if(theTile) { OBJGetLoc(theTile->obj, floorLoc); nextTile->tileID = theTile->tileID; nextTile->obj = OBJDuplicate(theTile->obj, OBJGetID(theTile->obj), tileLoc[eX], floorLoc[eY], tileLoc[eZ]); } else { OBJGetLoc(map->floor.obj, floorLoc); nextTile->tileID = map->floor.tileID; nextTile->obj = OBJDuplicate(map->floor.obj, OBJGetID(map->floor.obj), tileLoc[eX], floorLoc[eY], tileLoc[eZ]); } nextTile->homieType = map->floor.homieType; nextTile->status = map->floor.status; nextTile->type = map->floor.type; } else return HomieStop(homie, map); break; } //otherwise, move homie! OBJMovLoc(homie->obj, orient[eX], 0, orient[eZ]); if(TESTFLAGS(homie->status, HOMIE_STUCK)) { SETFLAG(homie->status, HOMIE_MOVING); CLEARFLAG(homie->status, HOMIE_STUCK); hTILE tile = &CELL(map->tiles, homie->indY, homie->indX, map->indSizeX); CLEARFLAG(tile->status, TILE_OCCUPIED); } float homieLoc[eMaxPt]; OBJGetLoc(homie->obj, homieLoc); float tileLoc[eMaxPt]; OBJGetLoc(nextTile->obj, tileLoc); //check to see if homie has reached the next tile if((orient[eX] > 0 && homieLoc[eX] >= tileLoc[eX]) || (orient[eX] < 0 && homieLoc[eX] <= tileLoc[eX]) || (orient[eZ] > 0 && homieLoc[eZ] >= tileLoc[eZ]) || (orient[eZ] < 0 && homieLoc[eZ] <= tileLoc[eZ])) { homie->indX = nextIndX; homie->indY = nextIndY; //determine course of action with specific tile if(nextTile->homieType == eHOMIE_NOCOLOR || nextTile->homieType == homie->type) { switch(nextTile->type) { case eTILE_ARROW_N: OBJSetLoc(homie->obj, tileLoc[eX], homieLoc[eY], tileLoc[eZ]); OBJSetOrientation(homie->obj, 0, 0, -GCFGGetHomieSpd()); return RETCODE_SUCCESS; case eTILE_ARROW_S: OBJSetLoc(homie->obj, tileLoc[eX], homieLoc[eY], tileLoc[eZ]); OBJSetOrientation(homie->obj, 0, 0, GCFGGetHomieSpd()); return RETCODE_SUCCESS; case eTILE_ARROW_E: OBJSetLoc(homie->obj, tileLoc[eX], homieLoc[eY], tileLoc[eZ]); OBJSetOrientation(homie->obj, -GCFGGetHomieSpd(), 0, 0); return RETCODE_SUCCESS; case eTILE_ARROW_W: OBJSetLoc(homie->obj, tileLoc[eX], homieLoc[eY], tileLoc[eZ]); OBJSetOrientation(homie->obj, GCFGGetHomieSpd(), 0, 0); return RETCODE_SUCCESS; case eTILE_TELEPORT: { hTILE theTile, lastTile; //look for another teleport tile similar to this for(int row = 0; row < map->indSizeY; row++) { for(int col = 0; col < map->indSizeX; col++) { theTile = &CELL(map->tiles, row, col, map->indSizeX); if((col != nextIndX || row != nextIndY) && !TESTFLAGS(theTile->status, TILE_OCCUPIED) && theTile->type == eTILE_TELEPORT && (theTile->homieType == nextTile->homieType)) { //teleport homie here OBJGetLoc(theTile->obj, tileLoc); OBJSetLoc(homie->obj, tileLoc[eX], homieLoc[eY], tileLoc[eZ]); lastTile = &CELL(map->tiles, lastIndY, lastIndX, map->indSizeX); CLEARFLAG(lastTile->status, TILE_OCCUPIED); homie->indX = col; homie->indY = row; return RETCODE_SUCCESS; } } } } return RETCODE_SUCCESS; case eTILE_HOLE: { CLEARFLAG(homie->status, HOMIE_MOVING); SETFLAG(homie->status, HOMIE_DEAD); float tileLoc[eMaxPt]; OBJGetLoc(nextTile->obj, tileLoc); float floorLoc[eMaxPt]; OBJDestroy(&nextTile->obj); //look for a floor tile hTILE theTile = MapGetTile(map, eTILE_FLOOR); if(theTile) { OBJGetLoc(theTile->obj, floorLoc); nextTile->tileID = theTile->tileID; nextTile->obj = OBJDuplicate(theTile->obj, OBJGetID(theTile->obj), tileLoc[eX], floorLoc[eY], tileLoc[eZ]); } else { OBJGetLoc(map->floor.obj, floorLoc); nextTile->tileID = map->floor.tileID; nextTile->obj = OBJDuplicate(map->floor.obj, OBJGetID(map->floor.obj), tileLoc[eX], floorLoc[eY], tileLoc[eZ]); } nextTile->homieType = map->floor.homieType; nextTile->status = map->floor.status; nextTile->type = map->floor.type; } return RETCODE_SUCCESS; } } } } return RETCODE_SUCCESS; }