Thursday 30 March 2017

sudo su

Following the instructions to do some work on some Linux servers I've been using sudo su quite a lot lately. Sure I know what the sudo command and the su command do, but it was not so clear to me how both commands combined work, so I was starting to feel a bit like a monkey each time I typed it. I finally understand what is exactly going, so no more monkey feeling :-)

The facts:
When running sudo su I just type the password of my current user (that is declared in the sudoers file) and now I will be running as root in the shell (without having typed the root password that su should request).

The how:

  • When invoking su without passing a user as parameter it will be just the same as doing su root
  • su root should ask me for the root password, unless that the su command is already being run as root. That's the magic part.
  • When running sudo if my current user is declared in the sudoers file I will be asked for his password, and the command that I'm tryng to launch will run as root. So the su command will be run already as root, and hence I will not ask for the root password.

Friday 17 March 2017

More on Explicit and Implicit conversions

There's something that I should add regarding my previous post about implicit conversions. As implicit conversions are managed by the compiler that inserts a call to the static method where you have defined the implicit conversion in your class, it will only work when the type of the variable referencing to your object is right that type, not a base one. I mean, for the example of the other day, where our Person class had defined an implicit conversion to String, the last line will not compile:

var p1 = new Person("Xuan");
string name = p1;
Console.WriteLine(name);

Console.WriteLine(DoFormat(p1));

Object o1 = p1;
//this line does not compile, "Cannot implicitly convert type 'object' to 'string'
//name = o1;

For explicit conversions, we'll get the problem at runtime. Let's say that we write this code:

 public class Animal
 {
  public string Name {get;set;}
  
  public Animal(string name)
  {
   this.Name = name;
  }
  
  public static explicit operator String(Animal a)
  {
      return a.Name;
  }
 }
 
 public static void Main(string[] args)
  {
   Console.WriteLine("started");
   
   var a1 = new Animal("ABC");
   string name = (string) a1;
   Console.WriteLine(name);

   Object o1 = a1;
   //this line crashes at runtime
   //we have an Object variable, not an Animal, so it will not set a call to the Conversion operator, but a call to a real cast, that will fail at runtime. 
   name = (string) o1;
   
   //the line below does not compile
   //"as" operator does not work for explicit conversions, it's only the cast operator
   //name = a1 as String;

  }
 }

For explicit conversions we use the cast operator, that as we know can mean 2 very different things, a "type hint" or a conversion. In this case the compiler sees that the Object class does not define any explicit conversion to string, so will assume that this cast means a "type hint" here, so it will translate it into a castclass IL instruction. At execution time the cast will fail and we'll get an exception:
System.InvalidCastException: Unable to cast object of type 'ExplicitConversions.Animal' to type 'System.String'

One additional point to note regarding explicit conversions, the as operator can not be used for invoking user defined explicit conversions. So this is one more point where it differs from the cast operator.

Friday 10 March 2017

Implicit Conversions

I've done quite a C# (re)discovery today, Implicit Conversions (aka coercion). They feel a bit confusing, but can be quite useful in some cases. This reminds me of some years ago when I found out that the cast operator could be used not just as a hint to the compiler (believe "Monsieur the Compiler" this object is not just a Book, but an SpecializedBook), but as a way to force a conversion into a different object (an explicit conversion). At that time I had already seen the existence of implicit conversions, but had not paid them attention.

Until today, if I had seen some code like this I would have not believed that it could compile:

  public static string DoFormat(string st)
  {
   return "{" + st + "}";
  }
  
  public static void Main(string[] args)
  {
   var p1 = new Person("Xuan");
   string name = p1;
   
   DoFormat(p1);
  }

Obviously a Person is not a String, so that assignment or that method call should not be allowed. The only way to do that work is by converting a Person object into a String object. But, I can not see any call to any sort of Convert method, so what?
Well, the thing is that it will work if your Person class implements an Implicit conversion to String, like this:

public class Person
 {
  public string Name {get;set;}
  public Person(string name)
  {
   this.Name = name;
  }
  
  public static implicit operator String(Person p)
  {
      return p.Name;
  }
 }

In that case the compiler will insert a call to the conversion method on its own. Just take a look into the decompiled code:

Thursday 2 March 2017

Presidential Elections in France

In 2 months time France will be holding presidential elections. Since I live here I have not voted in the Spanish elections and have not paid too much attention to them, basically there was not any political party that I could relate to and give my support. I can not vote in France, but if I could I would do it for sure. There's not any remaining candidate in the run for the French presidency that I can relate to at all, I would vote just out of panic for other of the candidates.

On one side I'm terrified by "Madame Le Pen". It's not a "the Nazis are coming" kind of thing. Indeed I've said in other occasions that I don't really consider them a far-right party, I just consider them a shit-party. I don't think they are a risk for normal/decent French citizens of non French ancestry (those who are not "Gaulois" or "Français de souche") regardless of their skin color. Honestly I think the descents of the East-Asian, non-muslim black African or Spanish/Portuguese/Italian migrations have nothing to fear in terms of being questioned as "not French enough". For "recent migrants" like me the sickening "National Priority policy" that they want to put in place (employers will have to pay extra taxes if they hire a non-French worker) will hurt for sure, and they are an attack to my elemental idea that "decent people" must be free to settle wherever they want in this fucking planet (as long as there is a cultural compatibility and a willingness for integration/assimilation). There are points in their program that I pretty much support, like the hardening of the laws against criminality and the enforcement of the Republican law in the bad neighbourhoods, cracking down on Islamist scum (with deprival of nationality, expulsions...), radical defense of Laïcité as a sacred value of the French Republic... Indeed, these points that should be common to any French party (well, except the far-left, we all know those idiots no longer live in the real world, just in a utopic fantasy of cultural relativity) seem to be only present in the FN program.

With what I'm saying you could be wondering why I have such a distate for the FN. Well, above all they want to break the European Union. If they were to win the elections they'll do a referendum, that would probably end up with France quitting the EU, which for sure would mean that the EU would no longer make any sense. I can not imagine a world without European Union. I can not imagine a world where I would need a visa to go to Paris or Berlin. I can not imagine a world where I would be just a citizen of a middle size country with no chances to have a say in a world dominated by other superpowers: USA-UK, Russia, Chine, India... I can not imagine a world where Germany and France could go into war again because of Alsace-Lorraine, a world where France could decide to annex Wallonia and where Germany could decide to annex Austria...

Secondly, the economical policy would be a real disaster. Closing borders, ultra-protectionist measures, the lost of valuable foreign work labor (yes, they would also manage to get rid of some crap that just pollute the country, but the average would not be good). Indeed, closing the country to the positive external influences will destroy the French identity, cause for me France is built on the mix of different, compatible peoples with a common desire for Liberté, Egalité et Laicité.

After years trying (and mainly succeeding) to clean its face and look as a modern, moderate party (the "dediabolisation" process), in the last months they are showing again their worst aspects. They are deeply pro-Trump, pro-Putine... and last week "Madame Le Pen" did some of their most stupid performances (not at the same level of her parent classical "dérapages", but not far). She was in Lebanon and refused to wear a scarf for meeting the grand mutif. Come on, you know how little I like Islam, how I consider that only soft interpretations of Islam can find a place in Europe (and always under strict surveillance), and how little appreciation I have for religious scarfs. If Le Pen were meeting the mutif in France it would be totally inacceptable that he demanded her to wear a scarf (or taking off a crucifix if she wore one, that I don't think so), but se was in Lebanon (a country that has been partially Muslim, partially Christian for centuries)!!! When years ago she compared the mobs of Muslims were cutting down streets in some French cities to pray with the Nazi occupation, I found the comparison pretty accurate and I pretty appreciated the bollocks to overtly say that. But what she did in Lebanon can be compared to the kind of thinking that gave place to the bad parts of colonialism. The same as we have the right (and the moral duty) to prevent women from wearing a full veil in our countries, I think they have the right to ask us to wear a scarf when we are in their countries, and we should do so if we want to keep some sort of basic relation (or just don't go there, that is probably the best choice).

What pretty scares me is that even with this recent "return to the basics" they are not loosing supporters, right the contrary. I guess people are so desperate because of the other lame alternatives that they just don't care.

So, what about the alternatives? The socialist leader, Benoit Hamon, also terrifies me. He's the perfect reminder of how stupid and unrealistic the "so called left" can be. Hopefully he has no chances at all, cause he would be driving France to the economical chaos (basic income for everyone!!! in a country where already too many people make a living of the state), and cultural chaos (his naivety with regards to Islamists, they just need tolerance and support as they have suffered so much becasue of the evil white christian Europe... put him in the list of Islamocollaborationists)

Regarding Fillon, I quite don't care about his scandals, I'll just say that he is an old fashioned devote Christian that does not give a shit about Laicite and that enjoys bullfighting!!!

Finally we have Macron. On the shit side he does not give a shit about Laicite either (but I hope he will not be so stupid to fall into Islamocollaborationism) and last week he did some lame statements calling colonialism a crime against humanity (yep, that stupid white european sense of culpability is not only patrimony of the left...) and he hardly has anything that could be considered a program... But on the good side I think he is not Eurosceptic and he would be pretty good for the economy. Sure workers would lose rights, but now that Paris, Berlin, Viena, Amsterdam fight to entice those companies moving away from London's city, Macron's business friendly stance could manage to bring many of those companies to Paris.

It's sad to have reached this point. I would have loved a situation where Sarkozy and Valls were the main candidates (and where the FN would have just around 10% of the vote, not enough to be dangerous but enough to force the other candidates to take into account some of their proposals regarding Islam and criminality). Both Sarkozy and Valls had made mistakes in the past, but both of them are strong men, both seemed firm defendants now of Republican values (I remember how Sarkozy strongly defended Charlie Hebdo a long while ago, at the time of the first Mohamet cartoons, and Valls was in favour of banning the burkini this summer) and both wanted to crack down on criminality and put the French law back in the Quartiers Sensibles.

All in all, it's depressing to say this, but one can only hope to see Macron elected as president next spring.