int main(int ac, const char** av) { JavaVMInitArgs vmArgs; vmArgs.version = JNI_VERSION_1_2; vmArgs.nOptions = 1; vmArgs.ignoreUnrecognized = JNI_TRUE; const char* class_ = 0; const char* jar = 0; int argc = 0; const char** argv = 0; const char* classpath = "."; for (int i = 1; i < ac; ++i) { if (strcmp(av[i], "-cp") == 0 or strcmp(av[i], "-classpath") == 0) { if (i + 1 == ac) usageAndExit(av[0]); classpath = av[++i]; } else if (strcmp(av[i], "-jar") == 0) { if (i + 1 == ac) usageAndExit(av[0]); jar = av[++i]; } else if (strncmp(av[i], "-X", 2) == 0 or strncmp(av[i], "-D", 2) == 0) { ++ vmArgs.nOptions; } else if (strcmp(av[i], "-client") == 0 or strcmp(av[i], "-server") == 0) { // ignore } else { if (jar == 0) { class_ = av[i++]; } if (i < ac) { argc = ac - i; argv = av + i; i = ac; } } } if (jar) { classpath = jar; class_ = mainClass(jar); if (class_ == 0) { fprintf(stderr, "Main-Class manifest header not found in %s\n", jar); exit(-1); } } #ifdef BOOT_LIBRARY ++ vmArgs.nOptions; #endif #ifdef BOOT_IMAGE vmArgs.nOptions += 2; #endif #ifdef BOOT_BUILTINS ++ vmArgs.nOptions; #endif RUNTIME_ARRAY(JavaVMOption, options, vmArgs.nOptions); vmArgs.options = RUNTIME_ARRAY_BODY(options); unsigned optionIndex = 0; #ifdef BOOT_IMAGE vmArgs.options[optionIndex++].optionString = const_cast<char*>("-Davian.bootimage=bootimageBin"); vmArgs.options[optionIndex++].optionString = const_cast<char*>("-Davian.codeimage=codeimageBin"); #endif #ifdef BOOT_LIBRARY vmArgs.options[optionIndex++].optionString = const_cast<char*>("-Davian.bootstrap=" BOOT_LIBRARY); #endif #ifdef BOOT_BUILTINS vmArgs.options[optionIndex++].optionString = const_cast<char*>("-Davian.builtins=" BOOT_BUILTINS); #endif #define CLASSPATH_PROPERTY "-Djava.class.path=" unsigned classpathSize = strlen(classpath); unsigned classpathPropertyBufferSize = sizeof(CLASSPATH_PROPERTY) + classpathSize; RUNTIME_ARRAY(char, classpathPropertyBuffer, classpathPropertyBufferSize); memcpy(RUNTIME_ARRAY_BODY(classpathPropertyBuffer), CLASSPATH_PROPERTY, sizeof(CLASSPATH_PROPERTY) - 1); memcpy(RUNTIME_ARRAY_BODY(classpathPropertyBuffer) + sizeof(CLASSPATH_PROPERTY) - 1, classpath, classpathSize + 1); vmArgs.options[optionIndex++].optionString = RUNTIME_ARRAY_BODY(classpathPropertyBuffer); for (int i = 1; i < ac; ++i) { if (strncmp(av[i], "-X", 2) == 0 or strncmp(av[i], "-D", 2) == 0) { vmArgs.options[optionIndex++].optionString = const_cast<char*>(av[i]); } } if (class_ == 0) { usageAndExit(av[0]); } JavaVM* vm; void* env; JNI_CreateJavaVM(&vm, &env, &vmArgs); JNIEnv* e = static_cast<JNIEnv*>(env); jclass c = 0; if (not e->ExceptionCheck()) { c = e->FindClass(class_); } if (jar) { free(const_cast<char*>(class_)); } if (not e->ExceptionCheck()) { jmethodID m = e->GetStaticMethodID(c, "main", "([Ljava/lang/String;)V"); if (not e->ExceptionCheck()) { jclass stringClass = e->FindClass("java/lang/String"); if (not e->ExceptionCheck()) { jobjectArray a = e->NewObjectArray(argc, stringClass, 0); if (not e->ExceptionCheck()) { for (int i = 0; i < argc; ++i) { e->SetObjectArrayElement(a, i, e->NewStringUTF(argv[i])); } e->CallStaticVoidMethod(c, m, a); } } } } int exitCode = 0; if (e->ExceptionCheck()) { exitCode = -1; e->ExceptionDescribe(); } vm->DestroyJavaVM(); return exitCode; }
int main( int argc, char *argv[] ) { signal( SIGPIPE, SIG_IGN ); std::string pipeFile = "/var/run/SomeUniqueName.sock"; std::string pidFile;// = "/var/run/SocketForwarder.pid"; int port = -1; bool isVerbose = false; bool foregroundDaemon = false; openlog( "socket_protector", LOG_PID | LOG_NOWAIT | LOG_CONS | LOG_PERROR, LOG_DAEMON ); std::vector<std::string> subCommand; for ( int a = 1; a < argc; ++a ) { std::string curarg = argv[a]; if ( curarg == "-v" || curarg == "-verbose" || curarg == "--verbose" ) isVerbose = true; else if ( curarg == "-f" || curarg == "-foreground" || curarg == "--foreground" ) foregroundDaemon = true; else if ( curarg == "-?" || curarg == "-h" || curarg == "-help" || curarg == "--help" ) { usageAndExit( argv[0], NULL, 0 ); } else if ( curarg == "-pid-file" || curarg == "--pid-file" ) { ++a; if ( a == argc ) usageAndExit( argv[0], "Invalid arguments", -1 ); pidFile = argv[a]; } else if ( curarg == "--" ) { for ( ++a; a < argc; ++a ) subCommand.push_back( std::string( argv[a] ) ); if ( subCommand.empty() ) usageAndExit( argv[0], "Missing sub-daemon arguments", -1 ); if ( subCommand[0][0] != '/' ) { // if stuff isn't in the execution path, like a relative // path, execvp will fail. Expand it out to a full path subCommand[0] = fixPath( subCommand[0] ); } break; } else if ( curarg[0] == '-' ) { usageAndExit( argv[0], "Unknown command line argument", -1 ); } else if ( port == -1 ) { long tmp = strtol( curarg.data(), NULL, 10 ); if ( tmp <= 0 || tmp > 65535 ) usageAndExit( argv[0], "Invalid port specification", -1 ); port = static_cast<int>( tmp ); } else usageAndExit( argv[0], "Invalid arguments", -1 ); } if ( port == -1 ) { usageAndExit( argv[0], "Missing port argument", -1 ); } if ( port < 1024 && geteuid() != 0 ) { syslog( LOG_ERR, "Attempt to use privileged port can only be done running as root" ); return -1; } // We need to wait for our child process from the backgrounding // process to be up and running or it's hard to see what is // wrong. so set up a named semaphore to wait on that in the // main thread, although we only need to do that in the case where // we are going to detach std::auto_ptr<Semaphore> startup; std::auto_ptr<SocketServer> servPtr; try { if ( ! foregroundDaemon ) { std::stringstream name; name << "SocketProtector" << getpid(); startup.reset( new Semaphore( name.str() ) ); pid_t detpid = fork(); if ( detpid < 0 ) { syslog( LOG_CRIT, "Unable to fork detached process" ); } if ( detpid != 0 ) { // wait for child to become active startup->wait(); // child is up... // PARENT EXIT exit( 0 ); } // this is now the child process, become session leader to // truly detach pid_t sid = setsid(); if ( sid < 0 ) syslog( LOG_CRIT, "Unable to become session leader" ); Daemon::closeFileDescriptors(); } else { Daemon::closeFileDescriptors( 3 ); } // Daemonization causes all open files to be closed, so we have to // restart syslogging afterwards openlog( "safe_daemon", LOG_PID | LOG_NOWAIT | LOG_CONS | LOG_PERROR, LOG_DAEMON ); int p = LOG_INFO; if ( isVerbose ) p = LOG_DEBUG; ( void )setlogmask( LOG_UPTO( p ) ); PID pf( pidFile ); syslog( LOG_NOTICE, "pid %d starting...", getpid() ); setSignalHandlers(); servPtr.reset( new SocketServer( subCommand, static_cast<uint16_t>( port ) ) ); theSocketServer = servPtr.get(); // ok, we're at a point where we are going to run, so // let the parent process know so it can continue allowing us // to detach... if ( startup.get() != NULL ) { startup->post(); startup.reset(); } syslog( LOG_DEBUG, "socket server starting..." ); theSocketServer->run(); syslog( LOG_DEBUG, "shutdown requested..." ); servPtr.reset(); theSocketServer = NULL; } catch ( std::exception &e ) { syslog( LOG_CRIT, "Unhandled exception: %s", e.what() ); } catch ( ... ) { syslog( LOG_CRIT, "Unhandled unknown exception" ); } // failsafe in case we got an exception... if ( startup.get() != NULL ) startup->post(); return 0; }
int main (int argc, char **argv) { char *diskType = "auto"; char *imagefilename = NULL; char *mountpoint = NULL; int debug = 0; int foreground = 0; char c; int i; char *differencing[DIFFERENCING_MAX]; int differencingLen = 0; extern char *optarg; extern int optind, optopt; // // *** Parse the command line options *** // processName = argv[0]; while ((c = getopt (argc, argv, GETOPT_ARGS)) != -1) { switch (c) { case 'r': readonly = 1; break; case 'g': foreground = 1; break; case 'v': verbose = 1; break; case 'a': allowall = 1; break; case 'w': allowall = 1; allowallw = 1; break; case 't': diskType = (char *) optarg; break; // ignored if OLDAPI case 's': if (differencingLen == DIFFERENCING_MAX) usageAndExit ("Too many differencing disks"); differencing[differencingLen++] = (char *) optarg; break; case 'f': imagefilename = (char *) optarg; break; case 'd': foreground = 1; debug = 1; break; case 'h': usageAndExit (NULL); case '?': usageAndExit ("Unknown option"); } } // // *** Validate the command line *** // if (argc != optind + 1) usageAndExit ("a single mountpoint must be specified"); mountpoint = argv[optind]; if (!mountpoint) usageAndExit ("no mountpoint specified"); if (!imagefilename) usageAndExit ("no image chosen"); if (stat (imagefilename, &VDfile_stat) < 0) usageAndExit ("cannot access imagefile"); if (access (imagefilename, F_OK | R_OK | ((!readonly) ? W_OK : 0)) < 0) usageAndExit ("cannot access imagefile"); for (i = 0; i < differencingLen; i++) if (access (differencing[i], F_OK | R_OK | ((readonly) ? 0 : W_OK)) < 0) usageAndExit ("cannot access differencing imagefile %s", differencing[i]); #define IS_TYPE(s) (strcmp (s, diskType) == 0) if (! (IS_TYPE ("auto") || IS_TYPE ("VDI") || IS_TYPE ("VMDK") || IS_TYPE ("VHD") || IS_TYPE ("auto"))) usageAndExit ("invalid disk type specified"); if (strcmp ("auto", diskType) == 0 && detectDiskType (&diskType, imagefilename) < 0) return 1; // // *** Open the VDI, parse the MBR + EBRs and connect to the fuse service *** // if (RT_FAILURE (VDInterfaceAdd (&vdError, "VD Error", VDINTERFACETYPE_ERROR, &vdErrorCallbacks, NULL, &pVDifs))) usageAndExit ("invalid initialisation of VD interface"); if (RT_FAILURE (VDCreate (&vdError, VDTYPE_HDD, &hdDisk))) usageAndExit ("invalid initialisation of VD interface"); DISKopen (diskType, imagefilename); for (i = 0; i < differencingLen; i++) { char *diffType; char *diffFilename = differencing[i]; detectDiskType (&diffType, diffFilename); DISKopen (diffType, diffFilename); } initialisePartitionTable (); myuid = geteuid (); mygid = getegid (); fuse_opt_add_arg (&fuseArgs, "vdfuse"); { char fsname[strlen (imagefilename) + 12]; strcpy (fsname, "-ofsname=\0"); strcat (fsname, imagefilename); fuse_opt_add_arg (&fuseArgs, fsname); } fuse_opt_add_arg (&fuseArgs, "-osubtype=vdfuse"); fuse_opt_add_arg (&fuseArgs, "-o"); fuse_opt_add_arg (&fuseArgs, (allowall) ? "allow_other" : "allow_root"); if (foreground) fuse_opt_add_arg (&fuseArgs, "-f"); if (debug) fuse_opt_add_arg (&fuseArgs, "-d"); fuse_opt_add_arg (&fuseArgs, mountpoint); return fuse_main (fuseArgs.argc, fuseArgs.argv, &fuseOperations #if FUSE_USE_VERSION >= 26 , NULL #endif ); }
int main(int argc, const char** argv) { if (argc < 7 or argc > 10) { usageAndExit(argv[0]); } unsigned alignment = 1; if (argc > 7) { alignment = atoi(argv[7]); } bool writable = false; bool executable = false; for (int i = 8; i < argc; ++i) { if (strcmp("writable", argv[i]) == 0) { writable = true; } else if (strcmp("executable", argv[i]) == 0) { executable = true; } else { usageAndExit(argv[0]); } } uint8_t* data = 0; unsigned size; int fd = open(argv[1], O_RDONLY); if (fd != -1) { struct stat s; int r = fstat(fd, &s); if (r != -1) { #ifdef WIN32 HANDLE fm; HANDLE h = (HANDLE) _get_osfhandle (fd); fm = CreateFileMapping( h, NULL, PAGE_READONLY, 0, 0, NULL); data = static_cast<uint8_t*>(MapViewOfFile( fm, FILE_MAP_READ, 0, 0, s.st_size)); CloseHandle(fm); #else data = static_cast<uint8_t*> (mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0)); #endif size = s.st_size; } close(fd); } bool success = false; if (data) { FileOutputStream out(argv[2]); if (out.isValid()) { success = writeObject (data, size, &out, argv[3], argv[4], argv[5], argv[6], alignment, writable, executable); } else { fprintf(stderr, "unable to open %s\n", argv[2]); } #ifdef WIN32 UnmapViewOfFile(data); #else munmap(data, size); #endif } else { perror(argv[0]); } return (success ? 0 : -1); }