|
a/src/utils/pathut.cpp |
|
b/src/utils/pathut.cpp |
|
... |
|
... |
24 |
#include <unistd.h>
|
24 |
#include <unistd.h>
|
25 |
#include <fcntl.h>
|
25 |
#include <fcntl.h>
|
26 |
#include <sys/param.h>
|
26 |
#include <sys/param.h>
|
27 |
#include <pwd.h>
|
27 |
#include <pwd.h>
|
28 |
#include <math.h>
|
28 |
#include <math.h>
|
|
|
29 |
#include <errno.h>
|
29 |
#include <sys/types.h>
|
30 |
#include <sys/types.h>
|
|
|
31 |
#include <sys/file.h>
|
|
|
32 |
#include <sys/stat.h>
|
|
|
33 |
|
30 |
// Let's include all files where statfs can be defined and hope for no
|
34 |
// Let's include all files where statfs can be defined and hope for no
|
31 |
// conflict...
|
35 |
// conflict...
|
32 |
#ifdef HAVE_SYS_MOUNT_H
|
36 |
#ifdef HAVE_SYS_MOUNT_H
|
33 |
#include <sys/mount.h>
|
37 |
#include <sys/mount.h>
|
34 |
#endif
|
38 |
#endif
|
|
... |
|
... |
450 |
out = url_encode(in, 7);
|
454 |
out = url_encode(in, 7);
|
451 |
}
|
455 |
}
|
452 |
return true;
|
456 |
return true;
|
453 |
}
|
457 |
}
|
454 |
|
458 |
|
|
|
459 |
|
|
|
460 |
Pidfile::~Pidfile()
|
|
|
461 |
{
|
|
|
462 |
if (m_fd >= 0)
|
|
|
463 |
::close(m_fd);
|
|
|
464 |
m_fd = -1;
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
pid_t Pidfile::read_pid()
|
|
|
468 |
{
|
|
|
469 |
int fd = ::open(m_path.c_str(), O_RDONLY);
|
|
|
470 |
if (fd == -1)
|
|
|
471 |
return (pid_t)-1;
|
|
|
472 |
|
|
|
473 |
char buf[16];
|
|
|
474 |
int error;
|
|
|
475 |
int i = read(fd, buf, sizeof(buf) - 1);
|
|
|
476 |
error = errno;
|
|
|
477 |
::close(fd);
|
|
|
478 |
if (i <= 0)
|
|
|
479 |
return (pid_t)-1;
|
|
|
480 |
buf[i] = '\0';
|
|
|
481 |
char *endptr;
|
|
|
482 |
pid_t pid = strtol(buf, &endptr, 10);
|
|
|
483 |
if (endptr != &buf[i])
|
|
|
484 |
return (pid_t)-1;
|
|
|
485 |
return pid;
|
|
|
486 |
}
|
|
|
487 |
|
|
|
488 |
int Pidfile::flopen()
|
|
|
489 |
{
|
|
|
490 |
const char *path = m_path.c_str();
|
|
|
491 |
int operation = LOCK_EX | LOCK_NB;
|
|
|
492 |
if ((m_fd = ::open(path, O_RDWR|O_CREAT, 0644)) == -1) {
|
|
|
493 |
m_reason = "Open failed";
|
|
|
494 |
return -1;
|
|
|
495 |
}
|
|
|
496 |
if (flock(m_fd, operation) == -1) {
|
|
|
497 |
int serrno = errno;
|
|
|
498 |
(void)::close(m_fd);
|
|
|
499 |
errno = serrno;
|
|
|
500 |
m_reason = "flock failed";
|
|
|
501 |
return -1;
|
|
|
502 |
}
|
|
|
503 |
if (ftruncate(m_fd, 0) != 0) {
|
|
|
504 |
/* can't happen [tm] */
|
|
|
505 |
int serrno = errno;
|
|
|
506 |
(void)::close(m_fd);
|
|
|
507 |
errno = serrno;
|
|
|
508 |
m_reason = "ftruncate failed";
|
|
|
509 |
return -1;
|
|
|
510 |
}
|
|
|
511 |
return 0;
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
pid_t Pidfile::open()
|
|
|
515 |
{
|
|
|
516 |
if (flopen() < 0) {
|
|
|
517 |
return read_pid();
|
|
|
518 |
}
|
|
|
519 |
return (pid_t)0;
|
|
|
520 |
}
|
|
|
521 |
|
|
|
522 |
int Pidfile::write_pid()
|
|
|
523 |
{
|
|
|
524 |
/* truncate to allow multiple calls */
|
|
|
525 |
if (ftruncate(m_fd, 0) == -1) {
|
|
|
526 |
m_reason = "ftruncate failed";
|
|
|
527 |
return -1;
|
|
|
528 |
}
|
|
|
529 |
char pidstr[20];
|
|
|
530 |
sprintf(pidstr, "%u", int(getpid()));
|
|
|
531 |
lseek(m_fd, 0, 0);
|
|
|
532 |
if (::write(m_fd, pidstr, strlen(pidstr)) != (ssize_t)strlen(pidstr)) {
|
|
|
533 |
m_reason = "write failed";
|
|
|
534 |
return -1;
|
|
|
535 |
}
|
|
|
536 |
return 0;
|
|
|
537 |
}
|
|
|
538 |
|
|
|
539 |
int Pidfile::close()
|
|
|
540 |
{
|
|
|
541 |
return ::close(m_fd);
|
|
|
542 |
}
|
|
|
543 |
|
|
|
544 |
int Pidfile::remove()
|
|
|
545 |
{
|
|
|
546 |
return unlink(m_path.c_str());
|
|
|
547 |
}
|
|
|
548 |
|
455 |
#else // TEST_PATHUT
|
549 |
#else // TEST_PATHUT
|
456 |
|
550 |
|
457 |
#include <iostream>
|
551 |
#include <iostream>
|
458 |
using namespace std;
|
552 |
using namespace std;
|
459 |
|
553 |
|
|
... |
|
... |
520 |
for (it = matched.begin(); it != matched.end();it++) {
|
614 |
for (it = matched.begin(); it != matched.end();it++) {
|
521 |
cout << *it << endl;
|
615 |
cout << *it << endl;
|
522 |
}
|
616 |
}
|
523 |
#endif
|
617 |
#endif
|
524 |
|
618 |
|
525 |
#if 1
|
619 |
#if 0
|
526 |
if (argc != 1) {
|
620 |
if (argc != 1) {
|
527 |
fprintf(stderr, "Usage: fsocc: trpathut <path>\n");
|
621 |
fprintf(stderr, "Usage: fsocc: trpathut <path>\n");
|
528 |
exit(1);
|
622 |
exit(1);
|
529 |
}
|
623 |
}
|
530 |
string path = *argv++;argc--;
|
624 |
string path = *argv++;argc--;
|
|
... |
|
... |
535 |
fprintf(stderr, "fsocc failed\n");
|
629 |
fprintf(stderr, "fsocc failed\n");
|
536 |
return 1;
|
630 |
return 1;
|
537 |
}
|
631 |
}
|
538 |
printf("pc %d, megabytes %ld\n", pc, blocks);
|
632 |
printf("pc %d, megabytes %ld\n", pc, blocks);
|
539 |
#endif
|
633 |
#endif
|
|
|
634 |
|
|
|
635 |
#if 1
|
|
|
636 |
Pidfile pidfile("/tmp/pathutpidfile");
|
|
|
637 |
pid_t pid;
|
|
|
638 |
if ((pid = pidfile.open()) != 0) {
|
|
|
639 |
cerr << "open failed. reason: " << pidfile.getreason() <<
|
|
|
640 |
" return " << pid << endl;
|
|
|
641 |
exit(1);
|
|
|
642 |
}
|
|
|
643 |
pidfile.write_pid();
|
|
|
644 |
sleep(10);
|
|
|
645 |
pidfile.close();
|
|
|
646 |
pidfile.remove();
|
|
|
647 |
#endif
|
|
|
648 |
|
540 |
return 0;
|
649 |
return 0;
|
541 |
}
|
650 |
}
|
542 |
|
651 |
|
543 |
#endif // TEST_PATHUT
|
652 |
#endif // TEST_PATHUT
|
544 |
|
653 |
|