void delAddress(const char * ifname, const char * ipaddr) { char tmpaddr[42]; strncpy(tmpaddr,ipaddr,sizeof(tmpaddr)); strtok(tmpaddr,"/"); // Remove possible prefix length. if (timestamp) stamptime(); if(osVersionInfo.dwMajorVersion < 6){ // < Windows Vista (i.e., Windows XP; check if this command is ok for Windows Server 2003 too). char * substr; execProcess(NULL,"netsh interface ipv6 delete address \"%s\" %s",if_name,tmpaddr); if (timestamp) stamptime(); substr = strtok(NULL,"/"); if(substr == NULL){ // A prefix length is not specified. // Use a 64 bit prefix strcat(tmpaddr,"/64"); execProcess(NULL,"netsh interface ipv6 delete route %s \"%s\"",tmpaddr,if_name); } else { execProcess(NULL,"netsh interface ipv6 delete route %s \"%s\"",ipaddr,if_name); } } else{ strtok(tmpaddr,"/"); // Remove possible prefix length. execProcess(NULL,"netsh interface ipv6 delete address \"%s\" %s",if_name,tmpaddr); } }
int main(){ int child1 = fork(); if(child1 == 0){ target = getppid(); execProcess(); sleep(1); exit(0); } int child2 = fork(); if(child2 == 0){ target = child1; execProcess(); kill(child1, SIGUSR1); sleep(1); exit(0); } target = child2; execProcess(); puts("En attente..."); sleep(10); waitpid(child1, NULL, 0); waitpid(child2, NULL, 0); puts("Père : terminé"); }
int checkForFile(char* filename) { /* * T(n) = O(n) for ls_buffer.lenth == n */ char ls_buffer[MAXDATASIZE]; execProcess("/bin/ls", "ls", ls_buffer); // printf("**processClient/processCheck: ls_buffer: %s", ls_buffer); // search results of ls for match to filename // Current implementation assumes ls_buffer words are delimited by newline char int has_file = 0; char * token_ptr; token_ptr = strtok(ls_buffer,"\n"); while (token_ptr != NULL) { // printf ("**processClient/processCheck:searchloop\n %s\n", token_ptr); if (strcmp(filename, token_ptr) == 0) { has_file = 1; return has_file; } token_ptr = strtok (NULL, "\n"); } return has_file; }
void addLoWPANRoute(const char * ifname, const char * net, const char * gw) { DWORD exitCode = -1; if (timestamp) stamptime(); execProcess(&exitCode,"netsh interface ipv6 add route %s/64 \"%s\" %s", net, if_name, gw); if(exitCode==0){ clean_route = true; } else { if (timestamp) stamptime(); fprintf(stderr, "WARNING: subprocess exited with code %ld\n", exitCode); } }
void addAddress(const char * ifname, const char * ipaddr) { DWORD exitCode = -1; if(osVersionInfo.dwMajorVersion < 6){ // < Windows Vista (i.e., Windows XP; check if this command is ok for Windows Server 2003 too). char * substr; char tmpaddr[44]; strncpy(tmpaddr,ipaddr,sizeof(tmpaddr)); strtok(tmpaddr,"/"); // Remove possible prefix length. if (timestamp) stamptime(); execProcess(&exitCode,"netsh interface ipv6 add address \"%s\" %s",if_name,tmpaddr); substr = strtok(NULL,"/"); if(substr == NULL){ // A prefix length is not specified. // Use a 64 bit prefix strcat(tmpaddr,"/64"); if (timestamp) stamptime(); execProcess(NULL,"netsh interface ipv6 add route %s \"%s\"",tmpaddr,if_name); } else { if (timestamp) stamptime(); execProcess(NULL,"netsh interface ipv6 add route %s \"%s\"",ipaddr,if_name); } } else{ if (timestamp) stamptime(); execProcess(&exitCode,"netsh interface ipv6 add address \"%s\" %s",if_name,ipaddr); } if(exitCode==0){ clean_addr = true; } else { if (timestamp) stamptime(); fprintf(stderr, "WARNING: subprocess exited with code %ld\n", exitCode); } }
void addNeighbor(const char * ifname, const char * neighb, const char * neighb_mac) { DWORD exitCode = -1; if(osVersionInfo.dwMajorVersion >= 6){ if (timestamp) stamptime(); execProcess(&exitCode,"netsh interface ipv6 add neighbor \"%s\" %s \"%s\"", if_name, neighb, neighb_mac); if(exitCode==0){ clean_neighb = true; } else { if (timestamp) stamptime(); fprintf(stderr, "WARNING: subprocess exited with code %ld\n", exitCode); } } }
int checkForFile(char* filename) { /* * This was an attempt to use grep to search for the file. * Using this would req. grep args. to be entered seperatly * which would involve changing the signature of the execProcess() * and subsequent methods to take optional params (variadic) */ // char grep_out[MAXDATASIZE]; // grep_out[0] = 0; // // char grep_search[MAXDATASIZE]; // strcpy(grep_search, "grep . -name "); // strcat(grep_search, filename); // printf("** processClient/processCheck: grep_search: %s\n", grep_search); // execProcess("/usr/bin/grep", grep_search, grep_out); // printf("** processClient/processCheck: grep_out= %s\n", grep_out); // // if(grep_out[0] == 0) // strcpy(out_buffer, "File not found"); // else // strcpy(out_buffer, "File found"); /* * T(n) = O(n) for ls_buffer.lenth == n */ char ls_buffer[MAXDATASIZE]; execProcess("/bin/ls", "ls", ls_buffer); printf("**processClient/processCheck: ls_buffer: %s", ls_buffer); // search results of ls for match to filename // Current implementation assumes ls_buffer words are delimited by newline char int has_file = 0; char * token_ptr; token_ptr = strtok(ls_buffer,"\n"); while (token_ptr != NULL) { printf ("**processClient/processCheck:searchloop\n %s\n", token_ptr); if (strcmp(filename, token_ptr) == 0) { has_file = 1; return has_file; } token_ptr = strtok (NULL, "\n"); } return has_file; }
void delNeighbor(const char * ifname, const char * neighb) { if (timestamp) stamptime(); execProcess(NULL,"netsh interface ipv6 delete neighbor \"%s\" %s", if_name, neighb); }
void delLoWPANRoute(const char * ifname, const char * net) { if (timestamp) stamptime(); execProcess(NULL,"netsh interface ipv6 delete route %s/64 \"%s\"", net, if_name); }
bool ZHttpServer::startServer(quint16 port) { if(m_tcpServer->isListening()) return true; if(!m_tcpServer->listen(QHostAddress::Any, port)){ qWarning() << "HttpServer: error: " << m_tcpServer->errorString(); return false; }else{ qWarning() << "HttpServer: OK"; } connect(m_tcpServer, &QTcpServer::newConnection, [this]{ QTcpSocket *socket = m_tcpServer->nextPendingConnection(); qWarning() << "HttpServer: new connect:" << socket->peerAddress().toString() << socket->peerName() << socket->peerPort(); connect(socket, &QTcpSocket::readyRead, [socket, this]{ HttpInfo info(socket->readAll()); qWarning() << info.url(); const QByteArray &query = info.url().query().toUtf8(); QMap<QByteArray, QByteArray> command_map; QFileInfo fileInfo(sysroot + info.url().path()); if (fileInfo.isFile() && fileInfo.isExecutable()) { execProcess((fileInfo.fileName() + " " + info.url().query(QUrl::FullyDecoded)).toLatin1(), socket); return; } if(!query.isEmpty()) { QByteArrayList commands = query.split('&'); qWarning() << "HttpServer: command:" << commands; for(const QByteArray &comm : commands) { if(comm.isEmpty()) continue; const QByteArrayList &tmp_list = comm.split('='); if(tmp_list.count() != 2 || tmp_list.first().isEmpty()) { socket->write(messagePackage("", "text/Html", HttpInfo::BadRequest, QString("Grammatical errors: \"%1\"").arg(QString(comm)))); socket->close(); return; } command_map[tmp_list.first()] = tmp_list.last(); } } if(command_map.value(ACTION) == ACTION_EXEC) { execProcess(QUrl::fromPercentEncoding(command_map.value(COMMAND)), socket); } else { QPointer<QTcpSocket> socket_pointer = socket; readFile(info.url(), socket); if (socket_pointer) socket->close(); } }); connect(socket, &QTcpSocket::disconnected, [socket]{ qWarning() << "HttpServer: disconnected: " << socket->peerAddress().toString() << socket->peerName() << socket->peerPort(); socket->deleteLater(); }); }); return true; }
void processList(char* out_buffer) { execProcess("/bin/ls", "ls", out_buffer); }
deBool deProcess_start (deProcess* process, const char* commandLine, const char* workingDirectory) { pid_t pid = 0; int pipeIn[2] = { -1, -1 }; int pipeOut[2] = { -1, -1 }; int pipeErr[2] = { -1, -1 }; int statusPipe[2] = { -1, -1 }; if (process->state == PROCESSSTATE_RUNNING) { deProcess_setError(process, "Process already running"); return DE_FALSE; } else if (process->state == PROCESSSTATE_FINISHED) { deProcess_cleanupHandles(process); process->state = PROCESSSTATE_NOT_STARTED; } if (pipe(pipeIn) < 0 || pipe(pipeOut) < 0 || pipe(pipeErr) < 0 || pipe(statusPipe) < 0) { deProcess_setErrorFromErrno(process, "pipe() failed"); closePipe(pipeIn); closePipe(pipeOut); closePipe(pipeErr); closePipe(statusPipe); return DE_FALSE; } pid = fork(); if (pid < 0) { deProcess_setErrorFromErrno(process, "fork() failed"); closePipe(pipeIn); closePipe(pipeOut); closePipe(pipeErr); closePipe(statusPipe); return DE_FALSE; } if (pid == 0) { /* Child process. */ /* Close unused endpoints. */ close(pipeIn[1]); close(pipeOut[0]); close(pipeErr[0]); close(statusPipe[0]); /* Set status pipe to close on exec(). That way parent will know that exec() succeeded. */ if (fcntl(statusPipe[1], F_SETFD, FD_CLOEXEC) != 0) dieLastError(statusPipe[1], "Failed to set FD_CLOEXEC"); /* Map stdin. */ if (pipeIn[0] != STDIN_FILENO && dup2(pipeIn[0], STDIN_FILENO) != STDIN_FILENO) dieLastError(statusPipe[1], "dup2() failed"); close(pipeIn[0]); /* Stdout. */ if (pipeOut[1] != STDOUT_FILENO && dup2(pipeOut[1], STDOUT_FILENO) != STDOUT_FILENO) dieLastError(statusPipe[1], "dup2() failed"); close(pipeOut[1]); /* Stderr. */ if (pipeErr[1] != STDERR_FILENO && dup2(pipeErr[1], STDERR_FILENO) != STDERR_FILENO) dieLastError(statusPipe[1], "dup2() failed"); close(pipeErr[1]); /* Doesn't return. */ execProcess(commandLine, workingDirectory, statusPipe[1]); } else { /* Parent process. */ /* Check status. */ { char errBuf[256]; int result = 0; close(statusPipe[1]); while ((result = read(statusPipe[0], errBuf, 1)) == -1) if (errno != EAGAIN && errno != EINTR) break; if (result > 0) { /* Read full error msg. */ int errPos = 1; while (errPos < DE_LENGTH_OF_ARRAY(errBuf)) { result = read(statusPipe[0], errBuf+errPos, 1); if (result == -1) break; /* Done. */ errPos += 1; } /* Make sure str is null-terminated. */ errBuf[errPos] = 0; /* Close handles. */ close(statusPipe[0]); closePipe(pipeIn); closePipe(pipeOut); closePipe(pipeErr); /* Run waitpid to clean up zombie. */ waitpid(pid, &result, 0); deProcess_setError(process, errBuf); return DE_FALSE; } /* Status pipe is not needed. */ close(statusPipe[0]); } /* Set running state. */ process->pid = pid; process->state = PROCESSSTATE_RUNNING; /* Stdin, stdout. */ close(pipeIn[0]); close(pipeOut[1]); close(pipeErr[1]); process->standardIn = deFile_createFromHandle(pipeIn[1]); process->standardOut = deFile_createFromHandle(pipeOut[0]); process->standardErr = deFile_createFromHandle(pipeErr[0]); if (!process->standardIn) close(pipeIn[1]); if (!process->standardOut) close(pipeOut[0]); if (!process->standardErr) close(pipeErr[0]); } return DE_TRUE; }