Exemple #1
0
static void save_pid ( const char *pid_file )
{
    FILE *fp;
    if ( access (pid_file, F_OK) == 0 )
    {
        if ( ( fp = fopen (pid_file, "r") ) != NULL )
        {
            char buffer[1024];
            if ( fgets (buffer, sizeof (buffer ), fp) != NULL )
            {
                unsigned int pid;
                if ( safe_strtoul (buffer, &pid) && kill (( pid_t ) pid, 0) == 0 )
                {
                    fprintf (stderr, "WARNING: The pid file contained the following (running) pid: %u\n", pid);
                }
            }
            fclose (fp);
        }
    }

    /* Create the pid file first with a temporary name, then
     * atomically move the file to the real name to avoid a race with
     * another process opening the file to read the pid, but finding
     * it empty.
     */
    char tmp_pid_file[1024];
    snprintf (tmp_pid_file, sizeof (tmp_pid_file ), "%s.tmp", pid_file);

    if ( ( fp = fopen (tmp_pid_file, "w") ) == NULL )
    {
        vperror ("Could not open the pid file %s for writing", tmp_pid_file);
        return;
    }

    fprintf (fp, "%ld\n", ( long ) getpid ());
    if ( fclose (fp) == - 1 )
    {
        vperror ("Could not close the pid file %s", tmp_pid_file);
    }

    if ( rename (tmp_pid_file, pid_file) != 0 )
    {
        vperror ("Could not rename the pid file from %s to %s",
                 tmp_pid_file, pid_file);
    }
}
Exemple #2
0
static void remove_pidfile ( const char *pid_file )
{
    if ( pid_file == NULL )
        return;

    if ( unlink (pid_file) != 0 )
    {
        vperror ("Could not remove the pid file %s", pid_file);
    }

}
Exemple #3
0
static int sasl_server_userdb_checkpass(sasl_conn_t *conn,
                                        void *context,
                                        const char *user,
                                        const char *pass,
                                        unsigned passlen,
                                        struct propctx *propctx)
{
    size_t unmlen = strlen(user);
    if ((passlen + unmlen) > (MAX_ENTRY_LEN - 4)) {
        fprintf(stderr,
                "WARNING: Failed to authenticate <%s> due to too long password (%d)\n",
                user, passlen);
        return SASL_NOAUTHZ;
    }

    FILE *pwfile = fopen(memcached_sasl_pwdb, "r");
    if (pwfile == NULL) {
        if (settings.verbose) {
            vperror("WARNING: Failed to open sasl database <%s>",
                    memcached_sasl_pwdb);
        }
        return SASL_NOAUTHZ;
    }

    char buffer[MAX_ENTRY_LEN];
    bool ok = false;

    while ((fgets(buffer, sizeof(buffer), pwfile)) != NULL) {
        if (memcmp(user, buffer, unmlen) == 0 && buffer[unmlen] == ':') {
            /* This is the correct user */
            ++unmlen;
            if (memcmp(pass, buffer + unmlen, passlen) == 0 &&
                (buffer[unmlen + passlen] == ':' || /* Additional tokens */
                 buffer[unmlen + passlen] == '\n' || /* end of line */
                 buffer[unmlen + passlen] == '\r'|| /* dos format? */
                 buffer[unmlen + passlen] == '\0')) { /* line truncated */
                ok = true;
            }

            break;
        }
    }
    (void)fclose(pwfile);
    if (ok) {
        return SASL_OK;
    }

    if (settings.verbose) {
        fprintf(stderr, "INFO: User <%s> failed to authenticate\n", user);
    }

    return SASL_NOAUTHZ;
}
Exemple #4
0
static enum test_return test_vperror(void) {
    int rv = 0;
    int oldstderr = dup(STDERR_FILENO);
    char tmpl[sizeof(TMP_TEMPLATE)+1];
    int newfile;
    char buf[80] = { 0 };
    FILE *efile;
    char *prv;
    char expected[80] = { 0 };

    strncpy(tmpl, TMP_TEMPLATE, sizeof(TMP_TEMPLATE)+1);

    newfile = mkstemp(tmpl);
    assert(newfile > 0);
    rv = dup2(newfile, STDERR_FILENO);
    assert(rv == STDERR_FILENO);
    rv = close(newfile);
    assert(rv == 0);

    errno = EIO;
    vperror("Old McDonald had a farm.  %s", "EI EIO");

    /* Restore stderr */
    rv = dup2(oldstderr, STDERR_FILENO);
    assert(rv == STDERR_FILENO);


    /* Go read the file */
    efile = fopen(tmpl, "r");
    assert(efile);
    prv = fgets(buf, sizeof(buf), efile);
    assert(prv);
    fclose(efile);

    unlink(tmpl);

    snprintf(expected, sizeof(expected),
             "Old McDonald had a farm.  EI EIO: %s\n", strerror(EIO));

    /*
    fprintf(stderr,
            "\nExpected:  ``%s''"
            "\nGot:       ``%s''\n", expected, buf);
    */

    return strcmp(expected, buf) == 0 ? TEST_PASS : TEST_FAIL;
}