Front Page

Deamonizing

Um einen Prozess zu einem Daemon zu machen, müssen mehrere Dinge geschehen:

Das detachen vom controlling terminal und das setzen der process-group kann durch setsid() (siehe man 2 setsid) zusammengefasst werden.

Weiteres hierzu: http://www.linuxprofilm.com/articles/linux-daemon-howto.html#ss5.5

C++ Tips

Copying file data / Reading whole file into a string

The iostreams streambuf object is itself streamable. Sending it into another stream will copy the complete stream (that is until eof) into the target stream:

 std::ifstream inFile ("foo.txt")
 std::ofstream outFile ("bar.txt")

 outFile << inFile.rdbuf();

will copy the complete file. This may also be used to efficiently read a complete stream into a string:

 std::ifstream inFile ("foo.txt");
 std::stringstream temp;

 temp << inFile.rdbuf()
  // Now access the file contents via temp.str()

fork/exec

iptables steuern

Sollte per libitc aus dem iptables/netfilter projekt gehen. Befindet sich im iptables-dev Paket

Sonstiges Zeugs

DIA digramme nach PNG umwandeln

Das blöde ist, das die größe ja nicht fest sein soll, andererseits die 'native' größe der Diagramme in PNG aber viel zu klein ist. Und die Zielgröße muss exakt vom aspekt stimmen sonst macht dia beim export mist.

Ich exportiere das Bild also zunächst nach EPS und hole mir dort die BoundingBox. Aus der kann ich dann mittels das Skalierfaktors die gewünschte Zielgröße berechnen. Uff ...

 dia2png() {
     local src
     local dst
     local dpi
     src="$1"; dst="$2"; dpi="$3"
     dia -s $(dia -e /proc/self/fd/1 -t eps "$src" | \
                  awk '/^%%BoundingBox:/{print $4,$5}' | \
                  dc -f- -e"$dpi*72/r$dpi*72/n[x]nn") \
         -e "$dst" -t png "$src"
 }

XSLT Fragment to remove superflous whitespace from the beginning of 'pre' tags

The following only works on the assumption, that text-nodes are normalized (that is, there are no two adjacent text nodes in the source document tree). It also depends on some of the EXSLT enhancements. This template will work with libxml (xsltproc), if the std:replace function implementation from EXSLT is included into the template.

  <xsl:template match="pre">
    <xsl:variable name="values">
      <fn:values>
        <xsl:for-each select="str:split(str:concat(.),'&amp;#x0A;')">
          <fn:value><xsl:value-of select="string-length(substring-before(.,str:split(normalize-space(.),' ')))"/></fn:value>
        </xsl:for-each>
      </fn:values>
    </xsl:variable>
    <xsl:variable name="indent">
      <xsl:apply-templates mode="min" select="exsl:node-set($values)"/>
    </xsl:variable>
    <xsl:variable name="content">
      <xsl:apply-templates mode="repl"/>
    </xsl:variable>
    <pre>
      <xsl:for-each select="@*"><xsl:copy/></xsl:for-each>
      <xsl:apply-templates mode="trunc" select="exsl:node-set($content)">
        <xsl:with-param name="indent" select="number($indent)"/>
      </xsl:apply-templates>
    </pre>
  </xsl:template>

  <xsl:template match="fn:values" mode="min">
    <xsl:value-of select="math:min(fn:value)"/>
  </xsl:template>

  <xsl:template match="*" mode="repl">
    <xsl:copy>
      <xsl:for-each select="@*"><xsl:copy/></xsl:for-each>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()" mode="repl">
    <xsl:variable name="nl"><fn:nl/></xsl:variable>
    <xsl:copy-of select="str:replace(.,'&amp;#x0A;',$nl)"/>
  </xsl:template>

  <xsl:template match="*" mode="trunc">
    <xsl:copy>
      <xsl:for-each select="@*"><xsl:copy/></xsl:for-each>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()[preceding-sibling::*[1][self::fn:nl]]" mode="trunc">
    <xsl:value-of select="substring(.,$indent+1)"/>
  </xsl:template>

  <xsl:template match="fn:nl" mode="trunc">
    <xsl:text>&amp;#x0A;</xsl:text>
  </xsl:template>