i32 core_cleanup() { socket_cleanup(); thread_cleanup(); debug_cleanup(); return RDPERROR_SUCCESS; }
/* * create a socket on sock_path and accept one client * require that connected client have UID of peer_uid * if authentication succeds, pass current process' * stdin and stdout, then wait for client to tell us to quit. */ void start_server ( char* sock_path, int sock_path_len, int peer_uid ) { int sock_serv, sock_client; sock_serv = socket_create( sock_path, sock_path_len ); if( sock_serv < 0 ) { LOGE( "start_server: sock_serv error" ); goto error; } sock_client = socket_accept( sock_serv ); if( sock_client < 0 ) { LOGE( "start_server: sock_client error, sock=%s",sock_path ); goto error; } struct ucred credentials; int ucred_length = sizeof( struct ucred ); if( getsockopt( sock_client, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length ) ) { LOGE( "start_server: couldn't obtain peer's credentials" ); goto error; } if( peer_uid != credentials.uid ) { LOGE( "start_server: authentication error, expected uid %d, but found %d", peer_uid, credentials.uid ); goto error; } if( socket_send_sdtio( sock_client ) != 0 ) { LOGE( "sending stdio failed" ); goto error; } // gpg-agent and the real pinentry are now communicating // but our process must stay alive until they're finished // so we can exit with the actual return code int rc = socket_wait( sock_client ); close( sock_client ); close( sock_serv ); socket_cleanup( sock_path ); exit( rc ); error: close( sock_client ); close( sock_serv ); socket_cleanup( sock_path ); exit( EXIT_FAILURE ); }
void HttpConnection::on(BufferedSocketListener::ModeChange) noexcept { socket_cleanup(false); fly_fire1(HttpConnectionListener::Complete(), this, currentUrl #ifdef RIP_USE_CORAL , BOOLSETTING(CORAL) && coralizeState != CST_NOCORALIZE #endif ); #ifdef RIP_USE_CORAL coralizeState = CST_DEFAULT; #endif }
void HttpConnection::on(BufferedSocketListener::Failed, const string & aLine) noexcept { socket_cleanup(false); #ifdef RIP_USE_CORAL if (SETTING(CORAL) && coralizeState == CST_DEFAULT) { fly_fire1(HttpConnectionListener::Retried(), this, coralizeState == CST_CONNECTED); coralizeState = CST_NOCORALIZE; dcdebug("Coralized address failed, retrying : %s\n", currentUrl.c_str()); downloadFile(currentUrl); return; } coralizeState = CST_DEFAULT; #endif fly_fire1(HttpConnectionListener::Failed(), this, aLine + " (" + currentUrl + ")"); }
int main(int argc, char* argv[]) { tcpserver_t tcpserver; tcpserver_handler_t tcphandler; tcphandler.onerror = OnTcpError; tcphandler.onconnected = OnTcpConnected; socket_init(); g_thdpool = thread_pool_create(2, 1, 64); tcpserver = tcpserver_start(NULL, 10000, &tcphandler, NULL); http_proxy_find(OnFindProxy, NULL); while('q' != getchar()) { } tcpserver_stop(tcpserver); thread_pool_destroy(g_thdpool); socket_cleanup(); return 0; }
static void AtExit(void) { sync_state = SHUTDOWN; Event("UPDATEDB", 0, NULL); Event("SHUTDOWN", 0, NULL); uplink_cleanup("Exiting",1); unlink (CfgSettings.pidfile); socket_cleanup(); //destroy all outgoing socket stuff. DestAccList(); #ifdef HAVE_PTHREAD dest_all_threads(); #endif #ifdef USE_DMALLOC dmalloc_shutdown(); #endif #ifdef HAVE_GNUTLS gnutls_global_deinit (); #endif destroy_event_list(); database_deinit(); close_log(); if (Omega) free(Omega); return; }
void rtmp_server_publish_test(const char* flv) { int r; struct rtmp_server_handler_t handler; memset(&handler, 0, sizeof(handler)); handler.send = rtmp_server_send; //handler.oncreate_stream = rtmp_server_oncreate_stream; //handler.ondelete_stream = rtmp_server_ondelete_stream; //handler.onplay = rtmp_server_onplay; //handler.onpause = rtmp_server_onpause; //handler.onseek = rtmp_server_onseek; handler.onpublish = rtmp_server_onpublish; handler.onscript = rtmp_server_onscript; handler.onvideo = rtmp_server_onvideo; handler.onaudio = rtmp_server_onaudio; socket_init(); socklen_t n; struct sockaddr_storage ss; socket_t s = socket_tcp_listen(NULL, 1935, SOMAXCONN); socket_t c = socket_accept(s, &ss, &n); s_flv = flv_writer_create(flv); rtmp_server_t* rtmp = rtmp_server_create(&c, &handler); static unsigned char packet[8 * 1024 * 1024]; while ((r = socket_recv(c, packet, sizeof(packet), 0)) > 0) { r = rtmp_server_input(rtmp, packet, r); } rtmp_server_destroy(rtmp); flv_writer_destroy(s_flv); socket_close(c); socket_close(s); socket_cleanup(); }
int main(int argc, char *argv[]) { struct stat st; static int socket_serv_fd = -1; char buf[64], shell[PATH_MAX], *result, debuggable[PROPERTY_VALUE_MAX]; char enabled[PROPERTY_VALUE_MAX], build_type[PROPERTY_VALUE_MAX]; char root_settings[PROPERTY_VALUE_MAX]; int i, dballow; mode_t orig_umask; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--command")) { if (++i < argc) { su_to.command = argv[i]; } else { usage(); } } else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--shell")) { if (++i < argc) { strncpy(shell, argv[i], sizeof(shell)); shell[sizeof(shell) - 1] = 0; } else { usage(); } } else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) { printf("%s\n", VERSION); exit(EXIT_SUCCESS); } else if (!strcmp(argv[i], "-V")) { printf("%d\n", VERSION_CODE); exit(EXIT_SUCCESS); } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { usage(); } else if (!strcmp(argv[i], "-") || !strcmp(argv[i], "-l") || !strcmp(argv[i], "--login")) { ++i; break; } else { break; } } if (i < argc-1) { usage(); } if (i == argc-1) { struct passwd *pw; pw = getpwnam(argv[i]); if (!pw) { su_to.uid = atoi(argv[i]); } else { su_to.uid = pw->pw_uid; } } if (from_init(&su_from) < 0) { deny(); } property_get("ro.debuggable", debuggable, "0"); property_get(ROOT_ACCESS_PROPERTY, enabled, ROOT_ACCESS_DEFAULT); property_get("ro.build.type", build_type, ""); property_get(ROOT_SETTINGS_PROPERTY, root_settings, ""); orig_umask = umask(027); // Root Settings-specific behavior if (strcmp("1", root_settings) == 0) { // only allow su on debuggable builds if (strcmp("1", debuggable) != 0) { LOGE("Root access is disabled on non-debug builds"); deny(); } // enforce persist.sys.root_access on non-eng builds if (strcmp("eng", build_type) != 0 && (atoi(enabled) & 1) != 1 ) { LOGE("Root access is disabled by system setting - enable it under settings -> developer options"); deny(); } // disallow su in a shell if appropriate if (su_from.uid == AID_SHELL && (atoi(enabled) == 1)) { LOGE("Root access is disabled by a system setting - enable it under settings -> developer options"); deny(); } } if (su_from.uid == AID_ROOT || su_from.uid == AID_SHELL) allow(shell, orig_umask); if (stat(REQUESTOR_DATA_PATH, &st) < 0) { PLOGE("stat"); deny(); } if (st.st_gid != st.st_uid) { LOGE("Bad uid/gid %d/%d for Superuser Requestor application", (int)st.st_uid, (int)st.st_gid); deny(); } if (mkdir(REQUESTOR_CACHE_PATH, 0770) >= 0) { chown(REQUESTOR_CACHE_PATH, st.st_uid, st.st_gid); } setgroups(0, NULL); setegid(st.st_gid); seteuid(st.st_uid); LOGE("sudb - Opening database"); db = database_init(); if (!db) { LOGE("sudb - Could not open database, prompt user"); // if the database could not be opened, we can assume we need to // prompt the user dballow = DB_INTERACTIVE; } else { LOGE("sudb - Database opened"); dballow = database_check(db, &su_from, &su_to); // Close the database, we're done with it. If it stays open, // it will cause problems sqlite3_close(db); db = NULL; LOGE("sudb - Database closed"); } switch (dballow) { case DB_DENY: deny(); case DB_ALLOW: allow(shell, orig_umask); case DB_INTERACTIVE: break; default: deny(); } socket_serv_fd = socket_create_temp(); if (socket_serv_fd < 0) { deny(); } signal(SIGHUP, cleanup_signal); signal(SIGPIPE, cleanup_signal); signal(SIGTERM, cleanup_signal); signal(SIGABRT, cleanup_signal); atexit(cleanup); if (send_intent(&su_from, &su_to, socket_path, -1, 0) < 0) { deny(); } if (socket_receive_result(socket_serv_fd, buf, sizeof(buf)) < 0) { deny(); } close(socket_serv_fd); socket_cleanup(); result = buf; if (!strcmp(result, "DENY")) { deny(); } else if (!strcmp(result, "ALLOW")) { allow(shell, orig_umask); } else { LOGE("unknown response from Superuser Requestor: %s", result); deny(); } deny(); return -1; }
void cmd_quit(int socket) { socket_cleanup(); exit(0); }
/*! * @brief: clean dynamic func and wsa socket * @author: xbw * @date: 2014_4_12 */ void s_socket_clean() { socket_cleanup(); socket_func_clean(); }
void sigterm_handler(int s) { socket_cleanup(); exit(0); }
//Save the book.xml file to flash and reboot the machine. void reboot_wiz() { socket_cleanup(); execl("/bin/sh", "sh", "reboot.sh", NULL); exit(0); }
static void cleanup(void) { socket_cleanup(); }
static void cleanup_signal(int sig) { socket_cleanup(); exit(sig); }
static void cleanup(void) { socket_cleanup(su_ctx); }
int main() { int listen_fd, client_fd, nfds; uint64_t buf_size, t; socklen_t size; struct sockaddr_vm my_addr = {0}, their_addr; int vmci_address_family; fd_set read_fds; unsigned int cid; uint8_t buf[BUFSIZE]; socket_startup(); if ((vmci_address_family = VMCISock_GetAFValue()) < 0) { fprintf(stderr, "VMCISock_GetAFValue failed: %d. You probably need root privileges\n", vmci_address_family); goto cleanup; } if ((listen_fd = socket(vmci_address_family, SOCK_STREAM, 0)) == -1) { perror("socket"); goto cleanup; } /* * SO_VMCI_BUFFER_SIZE – Default size of communicating buffers; 65536 bytes if not set. * SO_VMCI_BUFFER_MIN_SIZE – Minimum size of communicating buffers; defaults to 128 bytes. * SO_VMCI_BUFFER_MAX_SIZE – Maximum size of communicating buffers; defaults to 262144 bytes. */ buf_size = 32768; /* reduce buffer to above size and check */ if (setsockopt(listen_fd, vmci_address_family, SO_VMCI_BUFFER_SIZE, (void *)&buf_size, sizeof(buf_size)) == -1) { perror("setsockopt"); goto close; } size = sizeof(t); if (getsockopt(listen_fd, vmci_address_family, SO_VMCI_BUFFER_SIZE, (void *)&t, &size) == -1) { perror("getsockopt"); goto close; } if (t != buf_size) { fprintf(stderr, "SO_VMCI_BUFFER_SIZE not set to size requested.\n"); goto close; } my_addr.svm_family = vmci_address_family; my_addr.svm_cid = VMADDR_CID_ANY; my_addr.svm_port = VMADDR_PORT_ANY; if (bind(listen_fd, (struct sockaddr *) &my_addr, sizeof(my_addr)) == -1) { perror("bind"); goto close; } if ((cid = VMCISock_GetLocalCID()) == (unsigned int)-1) { fprintf(stderr, "VMCISock_GetLocalCID failed\n"); } else { fprintf(stderr, "server cid: %u\n", cid); } size = sizeof(my_addr); if (getsockname(listen_fd, (struct sockaddr *)&my_addr, &size) == -1) { perror("getsockname"); goto close; } fprintf(stderr, "server (cid, port): %u:%u\n", my_addr.svm_cid, my_addr.svm_port); for (;;) { if (listen(listen_fd, CONNECTION_BACKLOG) == -1) { perror("listen"); goto close; } size = sizeof(their_addr); if ((client_fd = accept(listen_fd, (struct sockaddr *) &their_addr, &size)) == -1) { perror("accept"); goto close; } fprintf(stderr, "client connected\n"); FD_ZERO(&read_fds); FD_SET(client_fd, &read_fds); nfds = client_fd + 1; if (select(nfds, &read_fds, NULL, NULL, NULL) == -1) { perror("select"); goto close; } if (FD_ISSET(client_fd, &read_fds)) { ssize_t s; if ((s = recv(client_fd, (void*)buf, sizeof(buf), 0)) < 0) { fprintf(stderr, "recv failed: %s\n", strerror(errno)); } else { int i; fprintf(stderr, "recved %lld bytes\n", (long long int)s); for (i=0; i<s; i++) { putc(buf[i], stderr); } putc('\n', stderr); } } close(client_fd); } close: socket_close(listen_fd); cleanup: socket_cleanup(); return 0; }
void HttpConnection::on(BufferedSocketListener::Line, const string & aLine) noexcept { if (!ok) { dcdebug("%s\n", aLine.c_str()); if (aLine.find("200") == string::npos) { if (aLine.find("301") != string::npos || aLine.find("302") != string::npos) { moved302 = true; } else { socket_cleanup(false); #ifdef RIP_USE_CORAL if (SETTING(CORAL) && coralizeState != CST_NOCORALIZE) { fly_fire1(HttpConnectionListener::Retried(), this, coralizeState == CST_CONNECTED); coralizeState = CST_NOCORALIZE; dcdebug("HTTP error with Coral, retrying : %s\n", currentUrl.c_str()); downloadFile(currentUrl); return; } #endif fly_fire1(HttpConnectionListener::Failed(), this, aLine + " (" + currentUrl + ")"); #ifdef RIP_USE_CORAL coralizeState = CST_DEFAULT; #endif return; } } ok = true; } else if (moved302 && Util::findSubString(aLine, "Location") != string::npos) { dcassert(m_http_socket); socket_cleanup(false); string location302 = aLine.substr(10, aLine.length() - 11); // make sure we can also handle redirects with relative paths // if (Util::isHttpLink(location302)) [-] IRainman fix: support for part-time redirect URL. { if (location302[0] == '/') // [!] IRainman fix { string proto, query, fragment; Util::decodeUrl(currentUrl, proto, server, port, file, query, fragment); string tmp = "http://" + server; if (port != 80) tmp += ':' + Util::toString(port); location302 = tmp + location302; } else { string::size_type i = currentUrl.rfind('/'); dcassert(i != string::npos); location302 = currentUrl.substr(0, i + 1) + location302; } } if (location302 == currentUrl) { fly_fire1(HttpConnectionListener::Failed(), this, "Endless redirection loop: " + currentUrl); return; } fly_fire1(HttpConnectionListener::Redirected(), this, location302); #ifdef RIP_USE_CORAL coralizeState = CST_DEFAULT; #endif downloadFile(location302); } else if (aLine == "\x0d") { m_http_socket->setDataMode(size); } else if (Util::findSubString(aLine, "Content-Length") != string::npos) { size = Util::toInt(aLine.substr(16, aLine.length() - 17)); } else if (Util::findSubString(aLine, "Content-Encoding") != string::npos) { if (aLine.substr(18, aLine.length() - 19) == "x-bzip2") fly_fire1(HttpConnectionListener::TypeBZ2(), this); } }
static void cleanup_signal(int sig) { socket_cleanup(su_ctx); exit(128 + sig); }
static void cleanup(void) { socket_cleanup(); if (db) sqlite3_close(db); }