void KPty::setCTty() { Q_D(KPty); // Setup job control ////////////////////////////////// // Become session leader, process group leader, // and get rid of the old controlling terminal. setsid(); // make our slave pty the new controlling terminal. #ifdef TIOCSCTTY ioctl(d->slaveFd, TIOCSCTTY, 0); #else // __svr4__ hack: the first tty opened after setsid() becomes controlling tty ::close(KDE_open(d->ttyName, O_WRONLY, 0)); #endif // make our new process group the foreground group on the pty int pgrp = getpid(); #if defined(_POSIX_VERSION) || defined(__svr4__) tcsetpgrp(d->slaveFd, pgrp); #elif defined(TIOCSPGRP) ioctl(d->slaveFd, TIOCSPGRP, (char *)&pgrp); #endif }
void ShuttleThread::run() { kDebug() << "------- STARTING SHUTTLE: " << m_device; const int fd = KDE_open((char *) m_device.toUtf8().data(), O_RDONLY); if (fd < 0) { fprintf(stderr, "Can't open Jog Shuttle FILE DESCRIPTOR\n"); return;; } EV ev; if (ioctl(fd, EVIOCGRAB, 1) < 0) { fprintf(stderr, "Can't get exclusive access on Jog Shuttle FILE DESCRIPTOR\n"); return;; } while (!stop_me) { if (read(fd, &ev, sizeof(ev)) < 0) { fprintf(stderr, "Failed to read event from Jog Shuttle FILE DESCRIPTOR\n"); } handle_event(ev); } close(fd); }
void TarArch::createTmp() { if ( compressed ) { if ( !QFile::exists(tmpfile) ) { QString strUncompressor = getUnCompressor(); // at least lzop doesn't want to pipe zerosize/nonexistent files QFile originalFile( m_filename ); if ( strUncompressor != "gunzip" && strUncompressor !="bunzip2" && ( !originalFile.exists() || originalFile.size() == 0 ) ) { QFile temp( tmpfile ); temp.open( IO_ReadWrite ); temp.close(); emit createTempDone(); return; } // the tmpfile does not yet exist, so we create it. createTmpInProgress = true; int f_desc = KDE_open(QFile::encodeName(tmpfile), O_CREAT | O_TRUNC | O_WRONLY, 0666); if (f_desc != -1) fd = fdopen( f_desc, "w" ); else fd = NULL; KProcess *kp = m_currentProcess = new KProcess; kp->clearArguments(); kdDebug(1601) << "Uncompressor is " << strUncompressor << endl; *kp << strUncompressor; KProcess::Communication flag = KProcess::AllOutput; if (strUncompressor == "lzop") { // setting up a pty for lzop, since it doesn't like stdin to // be /dev/null ( "no filename allowed when reading from stdin" ) // - but it used to work without this ? ( Feb 13, 2003 ) kp->setUsePty( KProcess::Stdin, false ); flag = KProcess::Stdout; *kp << "-d"; } *kp << "-c" << m_filename; connect(kp, SIGNAL(processExited(KProcess *)), this, SLOT(createTmpFinished(KProcess *))); connect(kp, SIGNAL(receivedStdout(KProcess*, char*, int)), this, SLOT(createTmpProgress( KProcess *, char *, int ))); connect( kp, SIGNAL(receivedStderr(KProcess*, char*, int)), this, SLOT(slotReceivedOutput(KProcess*, char*, int))); if (kp->start(KProcess::NotifyOnExit, flag ) == false) { KMessageBox::error(0, i18n("Unable to fork a decompressor")); emit sigOpen( this, false, QString::null, 0 ); } } else {
KDECORE_EXPORT int mkstemps (char* _template, int suffix_len) { static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; char *XXXXXX; int len; int count; int value; len = strlen (_template); if ((int) len < 6 + suffix_len || strncmp (&_template[len - 6 - suffix_len], "XXXXXX", 6)) return -1; XXXXXX = &_template[len - 6 - suffix_len]; value = rand(); for (count = 0; count < 256; ++count) { int v = value; int fd; /* Fill in the random bits. */ XXXXXX[0] = letters[v % 62]; v /= 62; XXXXXX[1] = letters[v % 62]; v /= 62; XXXXXX[2] = letters[v % 62]; v /= 62; XXXXXX[3] = letters[v % 62]; v /= 62; XXXXXX[4] = letters[v % 62]; v /= 62; XXXXXX[5] = letters[v % 62]; fd = KDE_open (_template, O_RDWR|O_CREAT|O_EXCL, 0600); if (fd >= 0) /* The file does not exist. */ return fd; /* This is a random value. It is only necessary that the next TMP_MAX values generated by adding 7777 to VALUE are different with (module 2^32). */ value += 7777; } /* We return the null string if we can't find a unique file name. */ _template[0] = '\0'; return -1; }
bool KPty::openSlave() { Q_D(KPty); if (d->slaveFd >= 0) return true; if (d->masterFd < 0) { kWarning(175) << "Attempting to open pty slave while master is closed"; return false; } d->slaveFd = KDE_open(d->ttyName.data(), O_RDWR | O_NOCTTY); if (d->slaveFd < 0) { kWarning(175) << "Can't open slave pseudo teletype"; return false; } fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC); return true; }
void TarArch::updateArch() { if (compressed) { updateInProgress = true; int f_desc = KDE_open(QFile::encodeName(m_filename), O_CREAT | O_TRUNC | O_WRONLY, 0666); if (f_desc != -1) fd = fdopen( f_desc, "w" ); else fd = NULL; KProcess *kp = m_currentProcess = new KProcess; kp->clearArguments(); KProcess::Communication flag = KProcess::AllOutput; if ( getCompressor() == "lzop" ) { kp->setUsePty( KProcess::Stdin, false ); flag = KProcess::Stdout; } if ( !getCompressor().isNull() ) *kp << getCompressor() << "-c" << tmpfile; else *kp << "cat" << tmpfile; connect(kp, SIGNAL(receivedStdout(KProcess*, char*, int)), this, SLOT(updateProgress( KProcess *, char *, int ))); connect( kp, SIGNAL(receivedStderr(KProcess*, char*, int)), (Arch *)this, SLOT(slotReceivedOutput(KProcess*, char*, int))); connect(kp, SIGNAL(processExited(KProcess *)), this, SLOT(updateFinished(KProcess *)) ); if ( !fd || kp->start(KProcess::NotifyOnExit, flag) == false) { KMessageBox::error(0, i18n("Trouble writing to the archive...")); emit updateDone(); } } }
bool MultiSegmentCopyJob::checkLocalFile() { QString dest_orig = m_dest.path(); QString dest_part( dest_orig ); dest_part += QLatin1String(".part"); QByteArray _dest_part( QFile::encodeName(dest_part)); KDE_struct_stat buff_part; bool bPartExists = (KDE_stat( _dest_part.data(), &buff_part ) != -1); if(!bPartExists) { QByteArray _dest = QFile::encodeName(dest_part); int fd = -1; mode_t initialMode; if (m_permissions != -1) initialMode = m_permissions | S_IWUSR | S_IRUSR; else initialMode = 0666; fd = KDE_open(_dest.data(), O_CREAT | O_TRUNC | O_WRONLY, initialMode); if ( fd < 0 ) { kDebug(5001) << " error"; /* if ( errno == EACCES ) error( ERR_WRITE_ACCESS_DENIED, dest_part ); else error( ERR_CANNOT_OPEN_FOR_WRITING, dest_part );*/ return false; } else { close(fd); } } m_dest_part = m_dest; m_dest_part.setPath(dest_part); kDebug(5001) << "success"; return true; }
bool KPty::open() { Q_D(KPty); if (d->masterFd >= 0) return true; d->ownMaster = true; QByteArray ptyName; // Find a master pty that we can open //////////////////////////////// // Because not all the pty animals are created equal, they want to // be opened by several different methods. // We try, as we know them, one by one. #ifdef HAVE_OPENPTY char ptsn[PATH_MAX]; if (::openpty( &d->masterFd, &d->slaveFd, ptsn, 0, 0)) { d->masterFd = -1; d->slaveFd = -1; kWarning(175) << "Can't open a pseudo teletype"; return false; } d->ttyName = ptsn; #else #ifdef HAVE__GETPTY // irix char *ptsn = _getpty(&d->masterFd, O_RDWR|O_NOCTTY, S_IRUSR|S_IWUSR, 0); if (ptsn) { d->ttyName = ptsn; goto grantedpt; } #elif defined(HAVE_PTSNAME) || defined(TIOCGPTN) #ifdef HAVE_POSIX_OPENPT d->masterFd = ::posix_openpt(O_RDWR|O_NOCTTY); #elif defined(HAVE_GETPT) d->masterFd = ::getpt(); #elif defined(PTM_DEVICE) d->masterFd = KDE_open(PTM_DEVICE, O_RDWR|O_NOCTTY); #else # error No method to open a PTY master detected. #endif if (d->masterFd >= 0) { #ifdef HAVE_PTSNAME char *ptsn = ptsname(d->masterFd); if (ptsn) { d->ttyName = ptsn; #else int ptyno; if (!ioctl(d->masterFd, TIOCGPTN, &ptyno)) { char buf[32]; sprintf(buf, "/dev/pts/%d", ptyno); d->ttyName = buf; #endif #ifdef HAVE_GRANTPT if (!grantpt(d->masterFd)) goto grantedpt; #else goto gotpty; #endif } ::close(d->masterFd); d->masterFd = -1; } #endif // HAVE_PTSNAME || TIOCGPTN // Linux device names, FIXME: Trouble on other systems? for (const char* s3 = "pqrstuvwxyzabcde"; *s3; s3++) { for (const char* s4 = "0123456789abcdef"; *s4; s4++) { ptyName = QString().sprintf("/dev/pty%c%c", *s3, *s4).toAscii(); d->ttyName = QString().sprintf("/dev/tty%c%c", *s3, *s4).toAscii(); d->masterFd = KDE_open(ptyName.data(), O_RDWR); if (d->masterFd >= 0) { #ifdef Q_OS_SOLARIS /* Need to check the process group of the pty. * If it exists, then the slave pty is in use, * and we need to get another one. */ int pgrp_rtn; if (ioctl(d->masterFd, TIOCGPGRP, &pgrp_rtn) == 0 || errno != EIO) { ::close(d->masterFd); d->masterFd = -1; continue; } #endif /* Q_OS_SOLARIS */ if (!access(d->ttyName.data(),R_OK|W_OK)) // checks availability based on permission bits { if (!geteuid()) { struct group* p = getgrnam(TTY_GROUP); if (!p) p = getgrnam("wheel"); gid_t gid = p ? p->gr_gid : getgid (); chown(d->ttyName.data(), getuid(), gid); chmod(d->ttyName.data(), S_IRUSR|S_IWUSR|S_IWGRP); } goto gotpty; } ::close(d->masterFd); d->masterFd = -1; } } } kWarning(175) << "Can't open a pseudo teletype"; return false; gotpty: KDE_struct_stat st; if (KDE_stat(d->ttyName.data(), &st)) return false; // this just cannot happen ... *cough* Yeah right, I just // had it happen when pty #349 was allocated. I guess // there was some sort of leak? I only had a few open. if (((st.st_uid != getuid()) || (st.st_mode & (S_IRGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH))) && !d->chownpty(true)) { kWarning(175) << "chownpty failed for device " << ptyName << "::" << d->ttyName << "\nThis means the communication can be eavesdropped." << endl; } grantedpt: #ifdef HAVE_REVOKE revoke(d->ttyName.data()); #endif #ifdef HAVE_UNLOCKPT unlockpt(d->masterFd); #elif defined(TIOCSPTLCK) int flag = 0; ioctl(d->masterFd, TIOCSPTLCK, &flag); #endif d->slaveFd = KDE_open(d->ttyName.data(), O_RDWR | O_NOCTTY); if (d->slaveFd < 0) { kWarning(175) << "Can't open slave pseudo teletype"; ::close(d->masterFd); d->masterFd = -1; return false; } #if (defined(__svr4__) || defined(__sgi__) || defined(Q_OS_SOLARIS)) // Solaris uses STREAMS for terminal handling. It is possible // for the pty handling modules to be left off the stream; in that // case push them on. ioctl(fd, I_FIND, ...) is documented to return // 1 if the module is on the stream already. { static const char *pt = "ptem"; static const char *ld = "ldterm"; if (ioctl(d->slaveFd, I_FIND, pt) == 0) ioctl(d->slaveFd, I_PUSH, pt); if (ioctl(d->slaveFd, I_FIND, ld) == 0) ioctl(d->slaveFd, I_PUSH, ld); } #endif #endif /* HAVE_OPENPTY */ fcntl(d->masterFd, F_SETFD, FD_CLOEXEC); fcntl(d->slaveFd, F_SETFD, FD_CLOEXEC); return true; } bool KPty::open(int fd) { #if !defined(HAVE_PTSNAME) && !defined(TIOCGPTN) kWarning(175) << "Unsupported attempt to open pty with fd" << fd; return false; #else Q_D(KPty); if (d->masterFd >= 0) { kWarning(175) << "Attempting to open an already open pty"; return false; } d->ownMaster = false; # ifdef HAVE_PTSNAME char *ptsn = ptsname(fd); if (ptsn) { d->ttyName = ptsn; # else int ptyno; if (!ioctl(fd, TIOCGPTN, &ptyno)) { char buf[32]; sprintf(buf, "/dev/pts/%d", ptyno); d->ttyName = buf; # endif } else { kWarning(175) << "Failed to determine pty slave device for fd" << fd; return false; } d->masterFd = fd; if (!openSlave()) { d->masterFd = -1; return false; } return true; #endif } void KPty::closeSlave() { Q_D(KPty); if (d->slaveFd < 0) return; ::close(d->slaveFd); d->slaveFd = -1; }
/* antlarr: KDE 4.0: make the first parameter "const QString &" */ bool KConfigINIBackEnd::writeConfigFile(QString filename, bool bGlobal, bool bMerge) { // is the config object read-only? if(pConfig->isReadOnly()) return true; // pretend we wrote it KEntryMap aTempMap; QFile *mergeFile = (bMerge ? new QFile(filename) : 0); bool bEntriesLeft = getEntryMap(aTempMap, bGlobal, mergeFile); delete mergeFile; if(bFileImmutable) return true; // pretend we wrote it // OK now the temporary map should be full of ALL entries. // write it out to disk. // Check if file exists: int fileMode = -1; bool createNew = true; KDE_struct_stat buf; if(KDE_stat(QFile::encodeName(filename), &buf) == 0) { if(buf.st_uid == getuid()) { // Preserve file mode if file exists and is owned by user. fileMode = buf.st_mode & 0777; } else { // File is not owned by user: // Don't create new file but write to existing file instead. createNew = false; } } KSaveFile *pConfigFile = 0; FILE *pStream = 0; if(createNew) { pConfigFile = new KSaveFile(filename, 0600); if(pConfigFile->status() != 0) { delete pConfigFile; return bEntriesLeft; } if(!bGlobal && (fileMode == -1)) fileMode = mFileMode; if(fileMode != -1) { fchmod(pConfigFile->handle(), fileMode); } pStream = pConfigFile->fstream(); } else { // Open existing file. // We use open() to ensure that we call without O_CREAT. int fd = KDE_open(QFile::encodeName(filename), O_WRONLY | O_TRUNC); if(fd < 0) { return bEntriesLeft; } pStream = KDE_fdopen(fd, "w"); if(!pStream) { close(fd); return bEntriesLeft; } } writeEntries(pStream, aTempMap); if(pConfigFile) { bool bEmptyFile = (ftell(pStream) == 0); if(bEmptyFile && ((fileMode == -1) || (fileMode == 0600))) { // File is empty and doesn't have special permissions: delete it. ::unlink(QFile::encodeName(filename)); pConfigFile->abort(); } else { // Normal case: Close the file pConfigFile->close(); } delete pConfigFile; } else { fclose(pStream); } return bEntriesLeft; }