Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program hangs with no output

Tags:

c

daemon

I've been poring over this program for ages, and have no idea why it doesn't work. I'm reasonably sure it's doing everything right but instead of actually working it just hangs indefinitely after printing the first prompt, and I just can't figure out why. I'm pretty much at my wit's end now, so if any can suggest what I'm doing wrong, I'd be much obliged...

It's C99, and you'll need the mhash library to compile it (uses for the CRC32 calculation). It's pretty portable but I developed it on Linux. Do not run in a VM!

#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <mhash.h>

/* WARNING: Do not debug this program. Halting on breakpoints at the wrong
 * time can be extremely hazardous. YOU HAVE BEEN WARNED. */

/* Structures used to define our layout. Note the careful use of volatile;
 * we don't want the compiler optimising away part of the invocation. */

typedef struct
{
    const char name[7];       /* sigil at focus */
    volatile int target;      /* summoning point */
    volatile char invocation; /* current char of invocation */
} focus_t;

typedef struct node
{
    const char name[4];   /* name of node */
    focus_t* center;      /* points to the evocation focus */
    struct node* cw;      /* clockwise binding ring */
    struct node* ccw;     /* counterclockwise binding ring */
    struct node* star;    /* next node of star */
    const char* linkname; /* name of star linkage */
    volatile uint32_t angel; /* protective angel for this node */
} node_t;

/* The pentacle nodes are circularly linked in both directions to form
 * a binding perimeter. In addition, they are singly linked to form a
 * classic 'daemon trap' five-pointed star. Each node points towards the
 * evocation focus (but not the other way around!) to enforce the geometry
 * we want. The design is based heavily on the Pentagram of Solomon. */

struct
{
    focus_t focus;
    node_t node[5];
}
S =
{
    /* None of the symbols for the pentacle are in Unicode. So we have to make
     * do with Latin transcriptions. */
    .focus = { "SOLUZEN", 0 },
    .node = {
        [0] = { "TE",   &S.focus, &S.node[1], &S.node[4], &S.node[2], "BELLONY" },
        [1] = { "TRA",  &S.focus, &S.node[2], &S.node[0], &S.node[3], "HALLIY" },
        [2] = { "GRAM", &S.focus, &S.node[3], &S.node[1], &S.node[4], "HALLIZA" },
        [3] = { "MA",   &S.focus, &S.node[4], &S.node[2], &S.node[0], "ABDIA" },
        [4] = { "TON",  &S.focus, &S.node[0], &S.node[3], &S.node[1], "BALLATON" }
    }
};

/* Name of spirit to summon --- rot13'd for safety.
 * (#65 from Crowley's translation of SHEMHAMPHORASH.)
 * This is Andrealphus, he that has dominion over menusuration, astronomy and
 * geometry. He seems fairly non-threatening. */

const char spiritname[] = "NAQERNYCUHF";
int rot13(int c) { return 'A' + (((c - 'A') + 13) % 26); }

/* We invoke the following names around the circle as a protective measure.
 * Strictly these should be in Hebrew script, but as the computer is a dumb
 * instrument we're relying on the symbolism rather than the actual literal
 * meaning themselves. Plus, working in RTL is a pain. */

const char* angels[] = {
        "Kether", "Eheieh", "Metatron", "Chaioth ha-Qadesh",
        "Rashith ha-Gilgalim", "Chokmah", "Jah", "Ratziel", "Auphanim",
        "Masloth", "Binah", "Jehovah Elohim", "Tzaphkiel", "Aralim",
        "Shabbathai", "Chesed", "El", "Tzadkiel", "Chasmalim", "Tzadekh",
        "Geburah", "Elohim Gibor", "Khamael", "Seraphim", "Madim",
        "Tiphareth", "Eloah Va-Daath", "Raphael", "Malachim", "Shemesh",
        "Netzach", "Jehovah Sabaoth", "Haniel", "Elohim", "Nogah", "Hod",
        "Elohim Sabaoth", "Michael", "Beni Elohim", "Kokab", "Yesod",
        "Shaddai El Chai", "Gabriel", "Cherubim", "Levanah"
};
const int angelcount = sizeof(angels)/sizeof(*angels);

/* Place the next angel on the pentacle. */

static void updatepentacle()
{
    static int angelnode = 0;
    static int angelindex = 0;

    const char* angel = angels[angelindex++];
    angelindex %= angelcount;

    /* Hash the angel's name to reduce its essence to 32 bits (which lets us
     * copy the angel bodily into the pentacle node. */

    uint32_t angelhash;
    MHASH td = mhash_init(MHASH_CRC32);
    mhash(td, angel, strlen(angel));
    mhash_deinit(td, &angelhash);

    S.node[angelnode].angel = angelhash;
    angelnode = (angelnode + 1) % 5;
}

int main(int argc, const char* argv[])
{
    /* Lock the evocation into memory, to prevent it from being paged out
     * while the spirit has manifested --- which would be bad. */

    int e = mlock(&S, sizeof(S));
    if (e != 0)
    {
        fprintf(stderr, "Unable to lock evocation, refusing to run\n");
        exit(1);
    }

    /* Actually perform the invocation: continually cycle the spirit's
     * name into the evocation focus (while maintaining our pentacle
     * integrity!) until something shows up in the target of the
     * evocation focus. */

    printf("Summoning...\n");
    do
    {
        for (int i = 0; i < sizeof(spiritname)-1; i++)
        {
            S.focus.invocation = rot13(spiritname[i]);
            updatepentacle();
            usleep(100); /* don't CPU-starve our spirit */
        }
    }
    while (S.focus.target == 0);
    printf("Summoning successful! %d\n", S.focus.target);

    /* Our spirit's arrived! Dismiss it immediately by using a null
     * invocation. Keep going until the evocation focus remains empty.
     * FIXME: a particularly mean spirit might find a way to hide. Until
     * we can sort this out, only summon relatively benign ones. This is
     * probably safe anyway, as when the process terminates the spirit's
     * address space will be nuked, taking the spirit with it. */

    printf("Dismissing...\n");
    do
    {
        S.focus.target = 0;
        for (int i = 0; i < 1000; i++)
        {
            S.focus.invocation = 0;
            updatepentacle();
        }
    }
    while (S.focus.target != 0);

    printf("Done.\n");
    return 0;
}

Incidentally, shouldn't there be a goetic tag?

Edit: Sorry I haven't gotten back earlier --- after I posted my query last night I ran some more tests and then my computer started making funny burning smells which didn't go away when I switched it off, so I spent the rest of the night tearing it down trying to find which part was faulty. (Didn't find anything.) I'm going to grab some sleep and get back to you. Thanks for the replies!

Edit: I'm posting this from a web café. My house has burnt down. Don't have time to post more but have to warn you: do not run this program for any reason! Really! Not joking! Have to go now, must find sanctuary somewhere---

Edit: 𝕳𝖔𝖈 𝖘𝖙𝖚𝖑𝖙𝖚𝖘 𝖒𝖊𝖚𝖘 𝖊𝖘𝖙. 𝕹𝖔𝖓 𝖎𝖓𝖙𝖊𝖗𝖕𝖔𝖓𝖊 𝖖𝖚𝖎 𝖓𝖔𝖓 𝖎𝖓𝖙𝖊𝖑𝖑𝖎𝖌𝖊𝖗𝖊.

like image 734
aleister.d.kelley Avatar asked Mar 31 '11 23:03

aleister.d.kelley


1 Answers

Crux sacra sit mihi lux!

Nunquam draco sit mihi dux.

Vade retro Satana!

Nunquam suade mihi vana!

Sunt mala quae libas.

Ipse venena bibas!

like image 140
Andrea Spadaccini Avatar answered Oct 05 '22 23:10

Andrea Spadaccini